digitalmars.D.learn - Creating a List Type Using std.variant.algebraic
- Meta (29/29) Apr 27 2014 I'm trying to create a basic List type using Algebraic, but the
- bearophile (7/9) Apr 27 2014 As stated in its docs, Algebraic is not yet finished and good for
- Meta (3/12) Apr 27 2014 Is it necessary to use This[]? I tried changing it to This* and
- Meta (6/8) Apr 27 2014 I should specify. This did not work:
- bearophile (6/11) Apr 27 2014 An Algebraic is a sum type, so you can't store two value in it,
- Meta (13/18) Apr 27 2014 Ah, you're right. I'm getting mixed up from my own initial
- bearophile (8/11) Apr 27 2014 Algebraic is currently unfinished and needs improvements. If it
- bearophile (5/6) Apr 27 2014 Also your Cons seems a value type, like Algebraic itself. You
- Meta (3/9) Apr 27 2014 Yes, it's hard to figure out Algebraic's particular kind of
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
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
On Sunday, 27 April 2014 at 20:22:12 UTC, bearophile wrote:Meta:Is it necessary to use This[]? I tried changing it to This* and it blew up on me.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
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
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
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, bearophileAh, 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
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
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
On Sunday, 27 April 2014 at 20:28:28 UTC, bearophile wrote:Meta:Yes, it's hard to figure out Algebraic's particular kind of "magic".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