Equivalent of "union"

137 views
Skip to first unread message

Sasha Bermeister

unread,
Jan 30, 2013, 11:43:31 PM1/30/13
to mi...@dartlang.org
Is there an equivalent of "union" in Dart?

I'm guessing the equivalent of a "struct" is a class, but I'd like to know specifically if there's a nice way of defining a union (of different types).

e.g.

union {
   int as_int;
   bool as_bool;
}

Ladislav Thon

unread,
Jan 31, 2013, 12:42:32 AM1/31/13
to General Dart Discussion


> Is there an equivalent of "union" in Dart?

No. Do you really need it in a high-level language?

The way I see it, unions are useful for two things: implementing tagged records (which you already have in the form of classes) and playing with bits and bytes (low-level work which you don't want to do in Dart anyway, there are better languages for that).

LT

Lasse R.H. Nielsen

unread,
Jan 31, 2013, 1:30:08 AM1/31/13
to mi...@dartlang.org
Not directly. There are two type of unions: The tagged one where you store one of two values in the same memory, but you know which one it is and read only that one again, and the overlapping-bit-pattern one where the same memory contain either value, and you interpret the bits in one of two ways.

Since values have only one type (Dart is "strongly typed", for some definition of that), and you can test the type using "is" checks, you don't need a tagged union. You can just store two values in a "var" variable and check their type.
 
For overlapping bits, you might be able to use the scalar lists. They are really byte-arrays with different ways to read numerical values out of the bytes.

/L
-- 
Lasse R.H. Nielsen - l...@google.com  
'Faith without judgement merely degrades the spirit divine'
Google Denmark ApS - Frederiksborggade 20B, 1 sal - 1360 København K - Denmark - CVR nr. 28 86 69 84

William Hesse

unread,
Jan 31, 2013, 4:09:34 AM1/31/13
to General Dart Discussion
I think that the point is, that we don't have a union type annotation,
where we can note that a parameter should be one of two types. I know
I've seen cases where I want this, and I bet others have too.

I have seen where the type of an input parameter is checked against
two types, and dispatched to different code depending on the type. I
think we don't have a perfect solution for this at the moment. But
people wanting pattern matching have a requirement that extends this
one, so a solution for pattern matching on input parameters would
cover this case too.
> --
> Consider asking HOWTO questions at Stack Overflow:
> http://stackoverflow.com/tags/dart
>
>



--
William Hesse

Paul Brauner

unread,
Jan 31, 2013, 4:22:43 AM1/31/13
to General Dart Discussion
*sponsored link* http://demo.adts.googlecode.com/git/main.html *sponsored link*

Ladislav Thon

unread,
Jan 31, 2013, 4:41:32 AM1/31/13
to mi...@dartlang.org
I think that the point is, that we don't have a union type annotation,
where we can note that a parameter should be one of two types.  I know
I've seen cases where I want this, and I bet others have too.

I have seen where the type of an input parameter is checked against
two types, and dispatched to different code depending on the type.  I
think we don't have a perfect solution for this at the moment.  But
people wanting pattern matching have a requirement that extends this
one, so a solution for pattern matching on input parameters would
cover this case too.

Oh, if this is about union types and pattern matching, I'm +100 to that :-)

LT

George Moschovitis

unread,
Jan 31, 2013, 5:29:43 AM1/31/13
to mi...@dartlang.org
On Thursday, January 31, 2013 11:22:43 AM UTC+2, Paul Brauner (polux) wrote:
*sponsored link* http://demo.adts.googlecode.com/git/main.html *sponsored link*

way cool!
 
I think that the point is, that we don't have a union type annotation,

Union types are very useful, hey even the Closure compiler supports them ;-)

-g.

Bob Nystrom

unread,
Jan 31, 2013, 12:30:54 PM1/31/13
to General Dart Discussion
On Thu, Jan 31, 2013 at 1:09 AM, William Hesse <whe...@google.com> wrote:
I think that the point is, that we don't have a union type annotation,
where we can note that a parameter should be one of two types.  I know
I've seen cases where I want this, and I bet others have too.

Yup, me too.

Dart is the only optionally/gradually-typed language I know that doesn't have union types. StrongTalk, Typed Racket, Erlang's Dialyzer, the Closure Compiler, Pike, and even TypeScript if you consider their interface overloading thing all have a way to say "this method takes a Foo or Bar" and not just "this method takes whatever random supertype Foo and Bar may happen to share which is probably just Object".

- bob

Stephen Adams

unread,
Jan 31, 2013, 6:50:59 PM1/31/13
to General Dart Discussion
union {
   int as_int;
   bool as_bool;
}

As Lasse said, the dynamic language way of doing this is just to use a field with a more general type and check the type of the value.

In checked mode, the following class would ensure that you used the shared field correctly:

class U {
  var _value;
  int get as_int => _value;
  bool get as_bool => _value;
  set as_int(int i) { _value = i; }
  set as_bool(bool b) { _value = b; }
}




--
Reply all
Reply to author
Forward
0 new messages