__traits getMember is context sensetive?

ketmar via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jun 14 03:10:51 PDT 2015


On Sun, 14 Jun 2015 09:46:54 +0000, JDemler wrote:

> On Sunday, 14 June 2015 at 05:52:00 UTC, ketmar wrote:
>> oh, seems that i managed to make everything even less understandable...
> 
> Your code works perfectly and makes at least some sense to me.
> Thank you.
> 
> If i understand it correctly: __traits-time = templateinstatiation-time
> = pragma-time before ctfe-time?

not quite right. there is no dedicated "CTFE time". compiler triggers CTFE 
when it needs to, i.e. when it sees something like this:

  enum myval = myfunction(); // or mytemplate!(...)

i.e. when it need a value in compile time. the interpreter is invoked, it 
evaluates (interprets) the given code (function or template 
instantiation), and then it returns result (or raises an error).

so CTFE can occur at any stage of semantic analysis.

to be clear: compiler goes (roughly) thru this stages:

1. parsing and building AST.

2. semantic analysis.

3. generating code.

at the stage (1) only syntax is checked, and source is converted to 
internal representation (AST).

at the state (2) compiler tries to actually make sense of your code. it 
deducing types, checking type correctness and such. if it need to 
instantiate some template, it does that here. it does semantic for each 
function (note: not for templates or eponymous templates!), one by one.

and here compiler may step on something that needs CTFE. it invokes 
interpreter on this stage. so there is no dedicated "CTFE time", compiler 
does it while it busy with semantic analysis, on demand. this way it 
avoids evaluating CTFE values that never used, which is good for compile 
speed. ;-)

pragmas and traits are executed by the very same CTFE engine, on demand 
too. i.e. when compiler hits pragma or traits during semantic analysis, 
compiler executes that.

overal semantic stage is more complex, of course, but i hope you get the 
idea.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20150614/9c97c372/attachment.sig>


More information about the Digitalmars-d-learn mailing list