Stack overflow / recursive expansion with alias this

Namespace rswhite4 at googlemail.com
Mon Apr 23 14:29:17 PDT 2012


I have this code:

import std.stdio;

struct NotNull(T : Object) {
private:
	T _value;

public:
	@disable
	this();
	
	// constructs with a runtime not null check (via assert())
	this(T value) {
		assert(value !is null);
		
		this._value = value;
	}
	
	@disable
	this(typeof(null)); // the null literal can be caught at compile 
time

	T _get() {
		return this._value;
	}
	
	const(T) _get() const {
		return this._value;
	}
	
	alias _get this;
}

NotNull!(T) assumeNotNull(T : Object)(T t) {
	return NotNull!(T)(t);
}

@property
NotNull!(T) makeNotNull(T : Object)() {
	T t = new T();
	
	return assumeNotNull(t);
}

class Foo {
	NotNull!(Foo) _convert() {
		return assumeNotNull(this);
	}
	
	alias _convert this;
}

void main() {

}

and get the error "recursive expansion". Can anybody explain that 
to me?

I reduce the Code to this simple example

class A {
private:
	B _b;

public:
	this(B b) {
		assert(b !is null);
		
		this._b = b;
	}
	
	B get() {
		return this._b;
	}
	
	alias get this;
}

class B {
public:
	A get() {
		return new B(this);
	}
	
	alias get this;
}

void main() {

}

And therefore i get the same error, as if i wrote "return 
NotNull!(Foo)(this);" instead of "return assumeNotNull(this);", 
in the "_convert" method of NotNull. The Output is "Stack 
overflow". I think that comes from recursive calls which fills 
the stack? Is that a bug?



More information about the Digitalmars-d-learn mailing list