Dynamic vs Multiple types (from [misc] Announcing Dart Vector Math)

2,974 views
Skip to first unread message

A Matías Quezada

unread,
Mar 25, 2012, 11:00:25 AM3/25/12
to Ladislav Thon, John McCutchan, Seth Ladd, General Dart Discussion
2012/3/22 Ladislav Thon <lad...@gmail.com>
2) Coming from C++ I really miss being able to define multiple functions with the same name but different signatures, for example,

int foo(int a);
int foo(float a);

With Dart, I have to write:

int foo(Dynamic a)

I've seen Dynamic on a lot of places in your library. Almost always it isn't necessary (AFAIK, the only place where Dynamic can be needed is in a list of type arguments when instantiating a generic class) and you can skip the type entirely. In this case, it is totally fine to write int foo(a). If you don't know the return type, you can skip it too (for example, although the main() function has return type of void, I never write it).

Just to let you know. The need for runtime checking of argument types of course remains.

LT

I agree set a variable as Dynamic has no sense I don't like it, but I found a little confuse than if I can recive two different types with no other common superclass than Object it has no type.

I mean, I know Dynamic is the default type of variables with no explicit type, but If I want to make a library I want to be sure to add types to all the interface, maybe I can do this with a tool than looks for arguments without type. But what about those argument I cannot type because they have two possible types?

Example:

class DomNode {
  add(child) {
    if (child is DomNode)
      _addChild(child);

    else if (child is string)
      _addChild(new DomNode(child));

    else throw new Exception('Invalid argument type');
  }
}

When I see the interface of DomNode

interface DomNode {
  add(child);
}

I don't know if I forget to write the type of the argument of if it can have more than one type.

I mean, the problem I see is than with no-type argument we can mean two things: we forget or just dont want to write the type or than the method recives more than one type (I don't know which ones)

The only solution I imagine for this is horrible:

interface DomNode {
  add(DomNode|string child);
}

So I ask here if someone can see a better way to handle this.

Dirk Detering

unread,
Mar 25, 2012, 1:56:15 PM3/25/12
to A Matías Quezada, Seth Ladd, Ladislav Thon, John McCutchan, General Dart Discussion


Am 25.03.2012 17:00 schrieb "A Matías Quezada" <amat...@gmail.com>:

> The only solution I imagine for this is horrible:
>
> interface DomNode {
>   add(DomNode|string child);
> }
>

Interesting. Why do you consider this horrible?
I ask because union type is exactly the solution taken by Ceylon, IIRC.

KR
Det

Jay Young

unread,
Mar 25, 2012, 4:14:09 PM3/25/12
to mi...@dartlang.org, A Matías Quezada, Seth Ladd, Ladislav Thon, John McCutchan
Union types are also used to great effect in the Closure type system.

Ladislav Thon

unread,
Mar 25, 2012, 6:58:37 PM3/25/12
to Dirk Detering, Seth Ladd, John McCutchan, General Dart Discussion, A Matías Quezada


> > The only solution I imagine for this is horrible:
> >
> > interface DomNode {
> >   add(DomNode|string child);
> > }
> >
>
> Interesting. Why do you consider this horrible?
> I ask because union type is exactly the solution taken by Ceylon, IIRC.

Yes, this is not horrible at all, if you ask me. However, I think this is unlikely to get into Dart, considering even the absence of generic methods or nullable types (BTW, I tried to implement nullable types in the VM and I've got it somewhat working, will upload the code to GitHub in next few days. Implementing union types would be a little harder, but doable).

LT

Reply all
Reply to author
Forward
0 new messages