post 2.071 mixin template & import rules

captaindet via Digitalmars-d digitalmars-d at puremagic.com
Thu Jun 30 22:42:30 PDT 2016


the way import declarations work has changed quite a bit with 2.071. two 
aspects have been written up nicely by steve schveighoffer ( 
http://www.schveiguy.com/blog/2016/03/import-changes-in-d-2-071 ). 
breaking as they might be for some, they are (a) a mere enforcement of 
the rules as they were always set out and (b) a change in look-up order 
preventing hijacking/overshadowing.

it appears that there had been an even more radical change as well with 
respect to the way mixin templates work. this has not been properly 
communicated yet. at least i could not find a write-up and a related 
thread conveyed rather guesswork than knowledge. ( 
https://forum.dlang.org/post/nl2bgi$2oim$1@digitalmars.com )

from this thread i tried to distill the gist below... so that if my 
understanding is confirmed by the language devs, it may serve as an 
explanation of changes complementary to steve's blog post.

----

mixin templates - https://dlang.org/spec/template-mixin.html :
"A TemplateMixin takes an arbitrary set of declarations from the body of 
a TemplateDeclaration and inserts them into the current context."

as of the the specs https://dlang.org/spec/module.html imports are 
declarations too and before 2.071 the mixin template design spec above 
would work for them just the same. as in:

```
mixin template AddImp (){
	import core.thread;
}
mixin AddImp;
```

this would import core.thread just alright into the current scope. 
regardless where the instantiation happens, module level as well as 
within classes etc. (note that the mixin template definition can be in 
another module and itself be constructed by more mixin templates or 
string mixins.)

especially if

```
module a;
mixin template AddImp (){
	public import core.thread;
}
```

and

```
module b;
mixin AddImp;
```

ie on module level, then in module b core.thread as a whole would be 
visible/accessible/imported.

apparently starting with 2.071 import declarations are not treated as 
declarations in the sense of mixin templates anymore. meaning that whole 
module imports (private and public) in mixin template definitions are 
not inserted into the instantiating scope anymore. neither if 
instantiated on module level, nor if in a class etc. it is not that the 
look-up order has changed for them, they are blatantly ignored now.

(and this is what i am not happy with it. it would be more consisting if 
the imports would be inserted in the current scope just as they were 
before but with the local definitions having precedence - similar to how 
issue 10378 was fixed - just as it would be if you actually wrote 
"import core.thread;" instead of "mixin AddImp;" also, it would not 
leave me with a ton of code that needs rewriting.)

on the other hand, if not a whole module is imported but individual 
symbols via selective import, it still works the same as before. eg:

```
module a;
mixin template AddImp (){
	public import core.thread : Thread;
}
```

and

```
module b;
mixin AddImp;
```

will make Thread available in all of module b.

----

please correct my view of things if i got it wrong.

thanks

/det


More information about the Digitalmars-d mailing list