-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rename usingnamespace
to mixin
#9861
Comments
I like This feels a little bit bike-sheddy, but the feature's implemented and good names are really important. If we take a name for a concept with which people are familiar (like |
More bike-shedding here, but how about |
I've never used a language with a
|
1 for |
I think that |
@benrob0329 I think the goal at some point was to have a long word for it to discourage its use and not encourage it though |
@Guigui220D Do you know why it's use would be discouraged? The intended use-case seems reasonable enough, though I suppose it does add some hidden complexity. |
What about |
How about calling it mixindeclarations ? Or mixindeclarationsof ? |
I like the logic of |
I like |
But the decls aren't imported ... not any more; they are added ("mixed in") to the container and available to its users (by qualifying the decls with the name of the container), but they aren't available to the code within it (without qualification). BTW, there seems to be a bug ... although the symbols are not imported, the duplicate definition checker thinks they are, so you can't define the symbols that you've mixed in. Edit: |
Based on the comments here I couldn't quite grasp why fn SA(comptime Self: type) type {
return struct {
a: u8 = 80,
pub fn add(self: Self, b: u8) u8 {
return self.a b;
}
};
}
const SB = struct {
c: u8 = 80,
pub usingnamespace SA(@This());
}; This example produces a compile error To me the word
I still like the choice of |
Because I didn't see this above I'll propose: |
|
|
@nektro The term |
My suggestion of
The problem isn't so much with |
I agree with @jibal's assessment here. In fact, it took me the better part of an hour now (of reading the docs as well as the various Github issues I found here and on DuckDuckGo) to realize that, contrary to its name, Even if the name will not be changed, it should at least be documented properly. |
May I suggest pub fn Mat3(comptime T: type) type {
return struct {
xx: T = 1, xy: T = 0, xz: T = 0,
yx: T = 0, yy: T = 1, yz: T = 0,
zx: T = 0, zy: T = 0, zz: T = 1,
pub derive
Mat3_Impl_Shared(T, @This());
pub derive if (T==f16 or T==f32 or T==f64 or T==f80 or T==f128)
Mat3_Impl_Float(T, @This()) else opaque{};
};
}
fn Mat3_Impl_Shared(comptime T: type, comptime Self: type) type {
return opaque {
const Index = enum {
xx, xy, xz,
yx, yy, yz,
zx, zy, zz,
};
pub fn compose(a: Self, b: Self) Self {
return .{
.xx = a.xx*b.xx a.xy*b.yx a.xz*b.zx,
.yx = a.yx*b.xx a.yy*b.yx a.yz*b.zx,
.zx = a.zx*b.xx a.zy*b.yx a.zz*b.zx,
.xy = a.xx*b.xy a.xy*b.yy a.xz*b.zy,
.yy = a.yx*b.xy a.yy*b.yy a.yz*b.zy,
.zy = a.zx*b.xy a.zy*b.yy a.zz*b.zy,
.xz = a.xx*b.xz a.xy*b.yz a.xz*b.zz,
.yz = a.yx*b.xz a.yy*b.yz a.yz*b.zz,
.zz = a.zx*b.xz a.zy*b.yz a.zz*b.zz,
};
}
pub fn cof(a: Self, index: Index) T {
return switch (index) {
.xx => a.zz*a.yy - a.yz*a.zy, .xy => a.zx*a.yz - a.yx*a.zz, .xz => a.zy*a.yx - a.yy*a.zx ,
.yx => a.xz*a.zy - a.zz*a.xy, .yy => a.xx*a.zz - a.zx*a.xz, .yz => a.xy*a.zx - a.zy*a.xx ,
.zx => a.yz*a.xy - a.xz*a.yy, .zy => a.yx*a.xz - a.xx*a.yz, .zz => a.yy*a.xx - a.xy*a.yx ,
};
}
pub fn det(a: Self) T {
return a.xx*a.cof(.xx)
a.xy*a.cof(.xy)
a.xz*a.cof(.xz) ;
}
};
}
fn Mat3_Impl_Float(comptime T: type, comptime Self: type) type {
return opaque {
pub fn inv(a: Self) ?Self {
const detA: T = a.det();
return if (detA == 0) null else .{
.xx=a.cof(.xx)/detA, .xy=a.cof(.yx)/detA, .xz=a.cof(.zx)/detA,
.yx=a.cof(.xy)/detA, .yy=a.cof(.yy)/detA, .yz=a.cof(.zy)/detA,
.zx=a.cof(.xz)/detA, .zy=a.cof(.yz)/detA, .zz=a.cof(.zz)/detA,
};
}
};
} |
I'm not found of it. "derive" doesn't convey the idea that it augments something. |
Well, it isn't quite augmenting the Type being referenced (since you can't immediately access it within), rather it's merging the declarations from it (as you can only access it in the final product). Words that properly conveys the shoehorning "tacked-on" sense would be ones like "Impute", "Imbue", or "Infuse" but those seem a tad overmuch. "Inject" might suit it but it seems to have a rather overloaded usage in other places, while "Imitate" only suggests behaviors are replicated and not the comets and vars.
|
But it's not ... please see the discussion above. The keyword has very odd semantics ... it exports the symbols from the referenced struct but it doesn't import them ... the code containing the keyword cannot use the symbols. That's the whole reason for this proposed change. In any case I can't see how "derive" or "deriving" give any indication of what it does, and suggests a relationship that may not hold at all. |
Since we're still bikeshedding, how about |
|
None of these suggestions is clearly better than |
|
|
Again, It might be helpful to look at #9629 to see the sorts of use cases that kept Andrew from removing the keyword altogether ... examples like
where it is used to combine decls from various namespaces into a new namespace. Most helpful though would be to stop bikeshedding this tiny corner of Zig and spend our time more fruitfully. And on that note I'm going to unsubscribe. |
How about "merge"? |
That would not be a good keyword to reserve, IMHO. |
usingnamespace is good as is |
Okay, brainstorming is going on: |
|
|
The semantics of
usingnamespace
were changed in #9618 as per proposal #9629. However, the keyword itself was not changed and it was decided to leave that for a separate proposal.To reiterate my comment on #9629, I think the originally proposed
includenamespace
isn't perfect as not everything in the target namespace is included, only the public declarations.I think using
mixin
as the new keyword would be more clear and concise. Mixins are already a well known concept in programming (wikipedia) and the revisedusingnamespace
semantics map quite well to the generally understood concept of a mixin.I also proposed using
includedecls
orincludedeclarations
in the original thread, however I find these ugly compared tomixin
and don't think they give a clearer idea of the semantics.As this is a rather zig-specific concept, people new to zig will almost certainly need to read the documentation to learn the exact semantics of this keyword no matter what we choose.
The text was updated successfully, but these errors were encountered: