Inference of auto storage classes for interface function return type

Timoses timosesu at gmail.com
Wed Jul 4 14:07:35 UTC 2018


How can I return inferred storage class from interface functions?
I can't use auto as return value in interface. Neither can I use 
"inout" as I don't pass a parameter.

	// Ref Type
	interface IRef
	{
		Ref opIndex(size_t idx) const;
	}
	class CRef : IRef
	{
		Ref[] a;
		this() immutable
		{ this.a = [new Ref()]; }
		Ref opIndex(size_t idx) const
		{ return a[idx]; } // Error: cannot implicitly convert 
expression this.a[idx] of type const(Ref) to app.Ref
	}
	class Ref{}

	void main()
	{
		auto a = new immutable(CRef)();
		auto s = a[3];
	}

For value types it works, I presume since they are passed by 
value, so the instance returned is an actual copy of the stored 
value.

         // Value Type
	interface IValue
	{
		Value opIndex(size_t idx) const;
	}
	class CValue : IValue
	{
		this() immutable { i = [Value()]; }
		Value[] i;
		Value opIndex(size_t idx) const
		{ return i[idx]; }
	}
	struct Value{}

However, for ref types this doesn't work.

Do I have to define two `opIndex` in the interface? One mutable, 
one for immutable type instances?


More information about the Digitalmars-d-learn mailing list