[Issue 20012] export inside mixin doesn't seem to work
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Wed Jul 24 23:04:33 UTC 2019
https://issues.dlang.org/show_bug.cgi?id=20012
Dennis <dkorpel at live.nl> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |dkorpel at live.nl
--- Comment #19 from Dennis <dkorpel at live.nl> ---
(In reply to Manu from comment #18)
> What we've learned here is that mixin is like some form of namespace, and
> that's NOT written on the tin, and definitely not what I have ever wanted.
That's true indeed for template mixins. Not for string mixins btw, which is why
extern(C) in string mixins does work like you want.
"The declarations in a mixin are placed in a nested scope and then ‘imported’
into the surrounding scope."
https://dlang.org/spec/template-mixin.html#mixin_scope
So this is not 'one weird case' where extern(C) works differently, it's really
that the rule shouldn't require the declaration to _be_ in global scope, but
_accessible_ from global scope, because then a user may want to call it from C.
Also, the spec says "C functions cannot be overloaded with another C function
with the same name." (https://dlang.org/spec/interfaceToC.html), but with
template mixins you can overload extern(C) functions currently.
If you need extern(C) functions for a pointer table, you can simply do:
```
mixin template foo() {
alias CFunc = extern(C) void function(int);
CFunc func = (int a) {printf("%d\n", a);};
}
```
No need to give it a D-mangled name when it's not supposed to be exposed.
> I bet almost nobody knows this, and they craft their mixins to work assuming
> they that they are inserted into the scope they're mixed into.
It does tend to be a surprise indeed. I first learned this when I wanted to
mixin common opBinary functions and found that the specialized opBinary
completely shadowed the mixed in ones. An alias or wrapper function needed to
be added to make it work. It might be too late to change mixin template
behavior though.
--
More information about the Digitalmars-d-bugs
mailing list