State of emulating functions

72 views
Skip to first unread message

Alexandre Ardhuin

unread,
Nov 7, 2012, 12:09:32 PM11/7/12
to mi...@dartlang.org
Hi all,

Is the implementation of emulating functions completed ? I haven't found any open issue on this. The last one has been 'Fixed' today.

With Dart SDK version 0.2.2.1_r14458, I have tried to use the examples of the article ( https://www.dartlang.org/articles/emulating-functions/ ) but I face some problems. Here is the code :

class WannabeFunction {
  call(int a, int b) => a + b;
}
typedef BinaryFunction(a,b);
main(){
  var wf = new WannabeFunction();
  print(wf is BinaryFunction); // #1
 
print(Function.apply(wf, [1, 2])); // #2
  print(wf(3, 4)); // #3
}


The problems are :
#1 : display false instead of true
#2 : error occurs : 'WannabeFunction' is not assignable to 'Function'
#3 : error occurs : Exception: NoSuchMethodError: incorrect number of arguments passed to method named 'call'
Receiver: Instance of 'WannabeFunction'
Tried calling: call(3, 4)
Found: call(a, b)

I also tried to make WannabeFunction extends or implements Function, but I got the error : 'Function' can not be used as superinterface/superclass
BTW, it's strange that WannabeFunction doesn't need (anymore) to extend or implement Function.


Alexandre

Ladislav Thon

unread,
Nov 7, 2012, 12:28:03 PM11/7/12
to mi...@dartlang.org


> Is the implementation of emulating functions completed ?

I didn't hear about it in a long time and didn't try it myself, so I honestly don't know. I'm interested in that too!

> class WannabeFunction {
>   call(int a, int b) => a + b;
> }

I think it isn't called 'call' anymore... or maybe it is, there were some renamings lately and I can't remember now.

I'd advise you to look at the specification and if it is still called 'call', then file a bug (if there really are no open bugs about it anymore).

LT

Ladislav Thon

unread,
Nov 7, 2012, 1:36:54 PM11/7/12
to mi...@dartlang.org
OK, so I looked at it and I believe that your code is correct and that all your expectations are correct too. It should work -- but it looks like it isn't implemented yet. And the specification mentions that too in 14.5: call is not fully supported in the M1 release.

LT

Alexandre Ardhuin

unread,
Nov 7, 2012, 3:32:00 PM11/7/12
to mi...@dartlang.org
Thanks for having checked.

So, this functionality doesn't seem available for now.


2012/11/7 Ladislav Thon <lad...@gmail.com>
OK, so I looked at it and I believe that your code is correct and that all your expectations are correct too. It should work -- but it looks like it isn't implemented yet. And the specification mentions that too in 14.5: call is not fully supported in the M1 release.

LT

--
Consider asking HOWTO questions at Stack Overflow: http://stackoverflow.com/tags/dart
 
 

Gilad Bracha

unread,
Nov 7, 2012, 3:37:21 PM11/7/12
to General Dart Discussion
I think this is being put in as we speak. On the other hand, we may yet change it; since it isn't fully there, it won't break anything in M1. 
--
Cheers, Gilad

Alexandre Ardhuin

unread,
Nov 7, 2012, 4:21:24 PM11/7/12
to mi...@dartlang.org
From spec : "If a type I includes a method named call(), and the type of call() is the function type F, then I is considered to be a subtype of F.".
I find this implicit implementation of Function a little too magic. It is not easy to figure out that I can use a class only with a call method when I call Function.apply which requires a Function as first param.

I have one more question : is it legal to implement or extend Function ? ( https://www.dartlang.org/articles/emulating-functions/ doesn't show such example anymore and Dart Editor doesn't allow it )

Alexandre



2012/11/7 Gilad Bracha <gbr...@google.com>

Gilad Bracha

unread,
Nov 7, 2012, 4:46:38 PM11/7/12
to General Dart Discussion
On Wed, Nov 7, 2012 at 1:21 PM, Alexandre Ardhuin <alexandr...@gmail.com> wrote:
From spec : "If a type I includes a method named call(), and the type of call() is the function type F, then I is considered to be a subtype of F.".
I find this implicit implementation of Function a little too magic.

Fair enough. 
 
It is not easy to figure out that I can use a class only with a call method when I call Function.apply which requires a Function as first param.

I have one more question : is it legal to implement or extend Function ? ( https://www.dartlang.org/articles/emulating-functions/ doesn't show such example anymore and Dart Editor doesn't allow it )

It is legal.

--
Cheers, Gilad

Ladislav Thon

unread,
Nov 8, 2012, 12:21:25 AM11/8/12
to mi...@dartlang.org


> From spec : "If a type I includes a method named call(), and the type of call() is the function type F, then I is considered to be a subtype of F.".
> I find this implicit implementation of Function a little too magic. It is not easy to figure out that I can use a class only with a call method when I call Function.apply which requires a Function as first param.

If I remember correctly from yesterday's looking at the spec, it says explicitly that all function types implement Function.

So it's just two steps: you have a call method, therefore you are a subtype of a function type. And since you are a subtype of a function type, you also implement Function (or you are a subtype of Function).

LT

Reply all
Reply to author
Forward
0 new messages