www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Creating a List Type Using std.variant.algebraic

reply "Meta" <jared771 gmail.com> writes:
I'm trying to create a basic List type using Algebraic, but the 
compiler keeps complaining about recursive aliasing.

import std.variant;

struct Cons(T, U: List)
{
	public static opCall(T t, U u)
	{
	}
}

//Error
alias List = Algebraic!(typeof(null), Cons!(int, This));

void main()
{
	List l = Cons(1, Cons(2, Cons(3, null)));
}

I tried changing Cons like so:


struct Cons(T, U)
{
     //...
}

Then I get:
Error: struct f809.Cons cannot deduce function from argument types
!()(int, typeof(null)), candidates are: f809.Cons(T, U)

Error: struct std.variant.This unknown size
Error: struct std.variant.This unknown size
Error: variable f809.Cons!(int, This).Cons.opCall.u no definition 
of struct This

I'm not sure what else I could try. Does anyone have any advice 
on using this?
Apr 27 2014
next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Meta:

 I'm trying to create a basic List type using Algebraic, but the 
 compiler keeps complaining about recursive aliasing.
As stated in its docs, Algebraic is not yet finished and good for recursive data structures. But here I have put an usage example of that kind: http://rosettacode.org/wiki/Flatten_a_list#With_an_Algebraic_Data_Type Bye, bearophile
Apr 27 2014
parent reply "Meta" <jared771 gmail.com> writes:
On Sunday, 27 April 2014 at 20:22:12 UTC, bearophile wrote:
 Meta:

 I'm trying to create a basic List type using Algebraic, but 
 the compiler keeps complaining about recursive aliasing.
As stated in its docs, Algebraic is not yet finished and good for recursive data structures. But here I have put an usage example of that kind: http://rosettacode.org/wiki/Flatten_a_list#With_an_Algebraic_Data_Type Bye, bearophile
Is it necessary to use This[]? I tried changing it to This* and it blew up on me.
Apr 27 2014
parent reply "Meta" <jared771 gmail.com> writes:
On Sunday, 27 April 2014 at 20:38:37 UTC, Meta wrote:
 Is it necessary to use This[]? I tried changing it to This* and 
 it blew up on me.
I should specify. This did not work: alias T = Algebraic!(int, This*); void main() { auto l = T(1, new T(2, new T(3, null))); }
Apr 27 2014
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Meta:

 I should specify. This did not work:

 alias T = Algebraic!(int, This*);

 void main() {
 	auto l = T(1, new T(2, new T(3, null)));
 }
An Algebraic is a sum type, so you can't store two value in it, only one, an int or a T*. But this is not going to solve your problems... Bye, bearophile
Apr 27 2014
parent reply "Meta" <jared771 gmail.com> writes:
On Sunday, 27 April 2014 at 20:54:02 UTC, bearophile wrote:
 An Algebraic is a sum type, so you can't store two value in it, 
 only one, an int or a T*. But this is not going to solve your 
 problems...

 Bye,
 bearophile
Ah, you're right. I'm getting mixed up from my own initial example. Fiddling with it a bit brings me back around to your code: import std.variant; alias List = Algebraic!(int, This[]); void main() { List l = List([List(1), List([List(2)])]); } Which is absolutely ugly. The more I try to use Algebraic, the more I come to think that this is something that can't be done cleanly in a library, even in D.
Apr 27 2014
parent "bearophile" <bearophileHUGS lycos.com> writes:
Meta:

 The more I try to use Algebraic, the more I come to think that 
 this is something that can't be done cleanly in a library, even 
 in D.
Algebraic is currently unfinished and needs improvements. If it turns out it's not possible to implement it well in library code, we can find the minimal core language additions needed to implement it well, and ask for them :) D is not set in stone regarding additive changes. Bye, bearophile
Apr 27 2014
prev sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Meta:

 alias List = Algebraic!(typeof(null), Cons!(int, This));
Also your Cons seems a value type, like Algebraic itself. You have to avoid creating an infinite-size algebraic value. Bye, bearophile
Apr 27 2014
parent "Meta" <jared771 gmail.com> writes:
On Sunday, 27 April 2014 at 20:28:28 UTC, bearophile wrote:
 Meta:

 alias List = Algebraic!(typeof(null), Cons!(int, This));
Also your Cons seems a value type, like Algebraic itself. You have to avoid creating an infinite-size algebraic value. Bye, bearophile
Yes, it's hard to figure out Algebraic's particular kind of "magic".
Apr 27 2014