Keyword to avoid not null references

Namespace rswhite4 at googlemail.com
Mon Apr 23 10:45:11 PDT 2012


On Monday, 23 April 2012 at 17:18:30 UTC, Namespace wrote:
> I thought that something like this
>
> // not_null_struct.d
>
> NotNull!(T) assumeNotNull(T : Object)(T t) {
> 	return NotNull!(T)(t);
> }
>
> @property
> NotNull!(T) makeNotNull(T : Object)() {
> 	T t = new T();
> 	
> 	return assumeNotNull(t);
> }
>
> // not_null.d which import not_null_struct.d
>
> NotNull!(Foo) _convert() {
> 	//return NotNull!(Foo)(this); // prints: Stack overflow
> 	return assumeNotNull(this);
> }
>
> alias _convert this;
>
> would allow me to convert Foo to NotNull!(Foo) implicit. But it 
> doesn't work. I get this error:
>
> not_null.d(37): Error: template instance 
> not_null_struct.assumeNotNull!(Foo) recursive expansion
>
> Line 37 is the return in the _convert method.
>
> Does anybody know why? I thought that would a smart idea...

I see, if i comment out "alias _get this;" in NotNull it works 
fine and i can implicit convert Foo to NotNull!(Foo).
But why doesn't it work shareable? In that case it would be 
nearly perfect for me.
Does anyone know how it could work? In my opinion both variants 
have to work shareable. Don't they?



More information about the Digitalmars-d-learn mailing list