www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Code failing unknown reason out of memory, also recursive types

reply Mr.Bingo <Bingo Namo.com> writes:
import std.stdio;


union Vector(T, size_t N = size_t.max)
{
	import std.range, std.typecons, std.meta, std.algorithm, 
std.conv, std.math;
	static if (N == size_t.max)		// For size_t.max sets N to be 
infinite/dynamic;
	{
		mixin("Tuple!("~"T,".repeat(N).join()~") data;");
		 property size_t Length() { return rect.length; }

		 property double norm(size_t n = 2)()
		{
			return (iota(0,data.length).map!(a => 
data[a].pow(n))).pow(1/cast(double)n);
		}

	}
	else
	{
		mixin("Tuple!("~"T,".repeat(N).join()~") data;");
		 property size_t Length() { return N; }


		 property double norm(size_t n = 2)()
		{
			mixin("return ("~(iota(0,N).map!(a => 
"data["~to!string(a)~"].pow(n)").join("+"))~").pow(1/cast(double)n);");
		}

	}

	auto opDispatch(string s, Args...)(Args v)
		if (s.length > 1 && s[0] == 'x')
		{
			static if (N == size_t.max)
				if (data.length < to!int(s[1..$]))
					for(int i = 0; i < to!int(s[1..$]) - data.length; i++) data 
~= 0;

			static if (Args.length == 0)
				mixin(`return data[`~s[1..$]~`];`);
			else static if (Args.length == 1)
				mixin(`data[`~s[1..$]~`] = v[0];  `);			
		}




	alias data this;
}

void main()
{
	import std.math, std.variant;

	Vector!(Algebraic!(Vector!int, int)) v;
	//v.x1 = 3;
	//v.x2 = 4;
	//v.x3 = 5;
	//writeln(v.x3);	
	//writeln(v.norm);
}

Trying to create a vector of vectors where any entry can be 
another vector of vectors or an int.
Jun 25 2018
parent reply rikki cattermole <rikki cattermole.co.nz> writes:
Let me get this straight, you decided to max out your memory address 
space /twice over/ before you hit run time, and think that this would be 
a good idea?
Jun 25 2018
parent Mr.Bingo <Bingo Namo.com> writes:
On Monday, 25 June 2018 at 14:41:28 UTC, rikki cattermole wrote:
 Let me get this straight, you decided to max out your memory 
 address space /twice over/ before you hit run time, and think 
 that this would be a good idea?
Well, that cause was suppose to allocate a dynamic array instead of a tuple. Somehow it got reverted. Works when allocating the dynamic array. How bout the compiler predict how big a variable is going to be allocated and if it exceeds memory then give an error instead of an out of memory error. If it would have gave me a line number I would have saw the problem immediately.
Jun 25 2018