Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std.variant.Algebraic should support duplicate types #9941

Open
dlangBugzillaToGithub opened this issue Nov 5, 2012 · 1 comment
Open

std.variant.Algebraic should support duplicate types #9941

dlangBugzillaToGithub opened this issue Nov 5, 2012 · 1 comment

Comments

@dlangBugzillaToGithub
Copy link

sandford reported this on 2012-11-05T11:50:07Z

Transfered from https://issues.dlang.org/show_bug.cgi?id=8962

CC List

  • tetyys

Description

As std.variant.Algebraic implements a bounded discriminated union, it should allow for duplicate types. i.e.

alias Algebraic!(string,string) A;

// Construction using: this(T)(T value, size_t id)
A a0 = A("First" ,0);
A a1 = A("Second",1);

// The type's id should be assessable by an ID member function that returns
// a size_t
switch(a0.id) {
    case 0:
        // The first int type
        break;
    case 1:
        // The second int type
        break;
    default:
        break;
}

// Similar to std.typecons.Tuple, member functions following the format of _#
// should provide a streamlined way of testing the ID and retrieving 
// the Algebraic's value
assert(a0._0 == "First"); 
// Which is equivalent to 
enforce(a0.id == 0);
assert(a0.get!string == "First");

// Similar to actual unions and std.typecons.Tuple, named members should be 
// supported. Assignment to named members is allowed i.e.

alias Algebraic!(int,"x", int,"y") B;
B b = B(10,0);

assert(b.id ==  0);
assert(b._0 == 10);
assert(b.x  == 10);

b.y = 5;
assert(b.id == 1);
assert(b._1 == 5);
assert(b.y  == 5);
@dlangBugzillaToGithub
Copy link
Author

tetyys commented on 2017-04-09T07:41:35Z

> As std.variant.Algebraic implements a bounded discriminated union, it should allow for duplicate types

Algebraic data type restricts to a closed set of possible types, how is it gonna restrict it to same type twice? I think the actual bug here is that Algebraic doesn't say anything when you pass two same types into it. But it still restricts storage to that one type normally.

@LightBender LightBender removed the P4 label Dec 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants