typedef void A(B b);
void call(B b) {};
void doStuff(A a) {};
main() {
doStuff(call);
}
--
Consider asking HOWTO questions at Stack Overflow: http://stackoverflow.com/tags/dart
I still don't think we need sugar for mixin applications (other than possibly removing the requirement for "extends Object"). And then for function typedefs, why not avoid the extra keyword (typedef), and just declare them as top-level abstract methods using either of:
I'm probably missing something, but I believe the following:
typedef X = Object with Y, Z;can be written as:
class X extends Object with Y, Z {}
abstract C A(B b);
C A(B b);
I'm probably missing something, but I believe the following:...typedef X = Object with Y, Z;can be written as:...class X extends Object with Y, Z {}There is a difference between the two. The later extends "Object with Y,Z" with ... an implicit constructor.For this code there is no real visible difference, but take this example:class C {final int x;C() : x = 0;C.x(this.x);}class X {void print() { print("something"); }}class D1 extends C with X {}typedef D2 = C with X;then D1 has only one constructor, the default implicit constructor, which delegates to "C with X"'s unnamed constructor, which again delegates to C's constructor C(), but D2 has two constructors.You can write "new D2.x(4)", but not "new D1.x(4)".
The trick is that D1 is not itself a mixin application. It is a class that extends a mixin application.Really? The spec says that "mixin application has forwarding constructors for all superclass constructors". This is actually a quote from the changelog in the spec, but looking at 9.1, I don't see how this only applies to a mixin application in the form of a type alias. If I read correctly, new D1.x(4) should be possible here. Where am I mistaken?
typedef int myFunc(int a, int b);
class Xxxx {
myFunc f;
Xxxx(this.f);
}
class Xxxx {
int f(int a, int b);
Xxxx(this.f);
}class Xxxx {
var f : (a : int, b : int) => int
}
> May be, I did a wrong usage of typedef, but I used it as a workaround to define type of attribute as a method.
It's not a workaround, it's the only way to do that.
> class Xxxx {
> int f(int a, int b);
>
> Xxxx(this.f);
> }
You actually can't, as here, f is an abstract method. This discrepancy between parameter types and variable types is exactly why I'm longing for a special syntax for function types. I've actually sent an email about this exact thing to the list a long time ago, but I've never created a bug report. Today, it's probably already too late.
LT
--
> 1) kernel objects: linux use ints all around.. but if you would want to annotate the int for diferent types of objects
> eg:
> typedef int file_t;
> typedef int sock_t;
> then you use it in you func declarations:
> sock_t sock_open();
> instead of opaque
> int sock_open();
Except that you can't do this in Dart. You are supposed to define new types by defining new classes.
> 2) sugar for function callbacks .. typedef void(...) mycallback ;
>
> now if you dont have a typedef and you have a function/method with this callback as a parameters (maybe more than once)
> you are doomed.. :)
Funny, because people are declaring callbacks as parameters all the time in Dart, and they usually don't use typedefs for that. Just look at Future.then or Stream.listen (but there is a ton of other examples).
But yes, this is doable, and I'm doing it sometimes too. Usually when I want to store the callback into a variable and I want that variable to have a type annotation. For that, there's no other way. If we had a syntax for function types, I wouldn't use typedefs at all.
LT