Manifest constant class instances

Alex sascha.orlov at gmail.com
Sun Nov 4 22:23:18 UTC 2018


On Sunday, 4 November 2018 at 22:06:28 UTC, Alex wrote:
>>
>> To make this point clearer: this works, but the instance 
>> doesn't live at runtime:
>>
>> ```
>> class C { int foo() { return 123; } }
>> enum i = new C().foo();
>>
>> void main()
>> {
>>     import core.stdc.stdio;
>>     printf("%d\n", i); // i is a 123 literal
>> }
>> ```

Or even better:

´´´
class C
{
	int member;
	int foo()
	{
		member++;
		return 123 + member;
	}
}

enum i = u();
enum u = &(new C().foo);

void main()
{
     import std.stdio : writeln;
     writeln(i); 	// 124
	writeln(u());   // 125
	writeln(i); 	// 124
	writeln(u()); 	// 126
}
´´´


More information about the Digitalmars-d mailing list