Objects(from classes) at Compile time? no gc

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri May 16 07:13:31 PDT 2014


On Fri, 16 May 2014 02:31:18 -0400, Jacob Carlborg <doob at me.com> wrote:

> On 16/05/14 06:59, Taylor Hillegeist wrote:
>> The subject says it all really. i have this example:
>>
>> import core.memory;
>>
>> class fruit{
>>    int value=5;
>>    public int getvalue(){
>>      return value;
>>    }
>> }
>>
>> int main(string[] args) {
>>      GC.disable;
>>      static fruit myfruit;
>>      return myfruit.getvalue();
>> }
>>
>> Most of the smart people will see that i want the program to return 5
>> but I did something dumb and didn't put in the "new" statement?
>>
>> So my question is in longer words "Can I create instances of objects at
>> compile time?" and if not "why not, i could build something
>> (roughly)equivalent out of structs and functions and have it at compile
>> time?"
>
> If you create an immutable instance it's possible to create it at  
> compile time:
>
> int main(string[] args) {
>       GC.disable;
>       immutable fruit myfruit = new immutable(fruit);
>       pragma(msg, myfruit.getvalue); // will print 5 at compile time
>       return myfruit.getvalue();
> }
>
> Although, I don't know if it will allocate it during  runtime as well.

It will not.

-Steve


More information about the Digitalmars-d-learn mailing list