Troubles creating templated inout objects

Timoses timosesu at gmail.com
Wed Jul 11 12:55:35 UTC 2018


On Tuesday, 10 July 2018 at 18:01:59 UTC, Steven Schveighoffer 
wrote:
> You are overthinking :) inout typically is much easier than you 
> expect, until you need to create temporary structs or types 
> with inout members, then it becomes problematic.
>
> https://run.dlang.io/is/kosYuC
>
> I had to put in a static if, because your function doesn't work 
> once you get down to the array type. See the // fixme comment.

Ok, well that helped a tiny bit for the example.

I'm trying to reproduce the errors from my project. It's starting 
to get out of control : D. inout is on a rampage!

https://run.dlang.io/is/5TN7XX

I guess it's the same as for immutable initialization of arrays. 
I can't seem to find a proper response to this one..

	import std.traits;

	struct S
	{
		int[3] arr;
	}
	struct SS
	{
		S s;
	}

	interface I
	{
		inout(I) opIndex(size_t idx) inout;
	}

	class Test(T) : I
	{
		T member;

		this(inout T mem) inout
		{
			this.member = mem;
		}

		inout(Test!T) get() inout
		{
			return new inout Test!(Unqual!(typeof(member)))(member);
		}

		inout(I) opIndex(size_t idx) inout
		{
			static if (is(T == struct))
			{
				switch (idx)
				static foreach (index, t; T.tupleof)
				{
					case index:
						return new inout
							Test!(Unqual!(typeof(this.member.tupleof[index])))
										(this.member.tupleof[index]);
					default:
						return null;
				}
			}
			else
				return null;
		}
	}

	auto test(T)(inout T t)
	{
		return new inout Test!(Unqual!T)(t);
	}

	class TestA(T : T[])
	{
		Test!T[] arr;

                 // ERROR: Can't initialize inout variable in a 
for loop...
		this(inout(T[]) arr) inout
		{
			// 1: Nope
			foreach (mem; arr)
			    this.arr ~= test(mem);

			// 2: Nope
			//Test!T[] a;
			//foreach (mem; arr)
			//   a ~= test(mem);

			import std.algorithm : map;
			// 3: Nope
			// this.arr = arr.map!((e) => test(e)).array;
		}
	}


	void main()
	{
		auto ss = SS(S([1,2,3]));
		auto t = new const Test!SS(ss);
		auto ta = new const TestA!(Test!SS[])([t]);
	}



More information about the Digitalmars-d-learn mailing list