is my code to get CTFE instantiated object valid D ?

chmike via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun May 29 01:15:24 PDT 2016


On Sunday, 29 May 2016 at 06:49:42 UTC, chmike wrote:
> What is the right way to use it ?

I answer to my self after testing so that people looking for that 
info can find it here.


The right way would be

     immutable Category category_;
     Rebindable!(immutable Category) instance() { return 
rebindable(category_); }

it is equivalent and shorter (for the lazy) to write

     auto instance() { return rebindable(category_); }

The disadvantage of this form is that the reader of documentation 
generated automatically from the source file will only see this : 
auto instance()
and he won't know what this instance method effectively returns.


If we want a mutable reference that accept mutable and immutable 
Category objects, we should define this

   Rebindable!(const Category) x = Category.instance();
   x = new Category;

The concept of immutable and constness of D was the most 
difficult thing to learn because it is radically different from 
the const concept of C and C++. Now that I understood it and 
stopped getting hit with immutability and constness compiler 
errors, I start to like it.

So the only problem I see with Rebindable is the inefficiency of 
reference comparison in the generated assembly code with DMD64 D 
Compiler v2.071.0.
This will hopefully get fixed in next versions.

Thank you very much to everybody who took the time to help me 
learn D and answer my so many questions.


More information about the Digitalmars-d-learn mailing list