"Value class instance" pattern?

bearophile bearophileHUGS at lycos.com
Sun Jul 14 08:17:41 PDT 2013


Dicebot:

> Hm, actually naive scoped usage seems to work for me:
>
> ---------------------------------------
> import std.typecons;
>
> class B
> {
> 	byte a;
> }
>
> class A
> {
> 	typeof(scoped!B()) b = void;
> 	
> 	this()
> 	{
> 		b = scoped!B();
> 	}
> }

Good. If I comment away most tags it seems to work:


import std.typecons;

const class Foo {
     int x;
     this(int xx) const pure nothrow {
         this.x = x;
     }
}

/*const*/ class Bar {
     /*const*/ typeof(scoped!Foo(1)) f = void;

     this(int x) /*const pure nothrow*/ {
         // f = typeof(f)(x); // Can't be used.
         f = scoped!Foo(x);
     }
}

void main() {
     auto b = new Bar(10);
}


I will try it in my code to see if and how well it works.


I have seen code like this:

class Bar {
     const int[1000] x = void;
     this(int n) {
         x[] = n;
     }
}
void main() {}



That gives me:

test.d(2): Warning: const field with initializer should be 
static, __gshared, or an enum
test.d(4): Error: slice x[] is not mutable


Are those error messages right?

Bye,
bearophile


More information about the Digitalmars-d mailing list