unittest + struct ctorю + nested mixin template + alias

Steven Schveighoffer schveiguy at yahoo.com
Tue May 7 08:09:57 PDT 2013


On Tue, 07 May 2013 10:58:09 -0400, ref2401 <refactor24 at gmail.com> wrote:

> Version D 2.062
>
> class MyClass
> {}
>
> struct MyStruct(T)
> if (is(T == class))
> {
> 	string _message = "nothing";
>
> 	this(T obj)
> 	{
> 		if (obj is null){
> 			_message = "Null reference message";
> 		}
> 		else{
> 			_message = "All right message";
> 		}
> 	}
>
> 	mixin Foo!();
>
> 	private mixin template Foo()
> 	{
> 		void foo() {}
> 	}
> }
>
>
> unittest
> {
> 	alias MyStruct!MyClass StructType;
> 	auto inst = StructType(null);
>
> 	string msg = inst._message;		// Wrong value!
> }
>
>
> The behavior varies between executions: sometimes msg contains empty  
> string, in other times it holds "nothing".
> If StructType is replaced with MyStruct!MyClass then everything works as  
> expected.
> 	auto inst = MyStruct!MyClass(null);	// OK
>
> If declaration of the Foo template is moved out of struct MyStruct(T)  
> definition, everything works fine too.
> This strange behavior is observed only in unittest block, not when  
> executed in main().
> Am I doing something incorrectly or is this a bug?

How are you determining that _message is wrong?  I see no observable  
printouts or anything.

It may be that if you are using a debugger, the debugger can't handle the  
mixin and is looking at the wrong data, or maybe the compiler is  
outputting wrong debug info.

If I put this line in, it never asserts:

assert(msg == "Null reference message");

I think the compiler is outputting correct code.  I don't have 2.062, only  
the beta, and 2.061.  Tested with the latest beta.

-Steve


More information about the Digitalmars-d-learn mailing list