Closures are not comparable?--Always get the same result of the comparison operation of closure to itself - "false".Even when comparing with "identical" method.Is this normal?
Consider asking HOWTO questions at Stack Overflow: http://stackoverflow.com/tags/dart
Thanks. I did not know about it.It prevents to use callbacks as handles for himself.Because this will not work.//------var handle = callback;queue.enqueue (handle);/ / And laterqueue.indexOf (handle) / / is always -1
Also one similar question.I also do not understand runtime types are immutable or not?var type = String;var value = 'message';var valueRuntimeType = value.runtimeType;print('type: $type');print('valueRuntimeType: $valueRuntimeType');print('type.runtimeType: ${type.runtimeType}');print('valueRuntimeType.runtimeType: ${valueRuntimeType.runtimeType}');print('valueRuntimeType == type: ${valueRuntimeType == type}, may be true');print('valueRuntimeType.runtimeType == type.runtimeType: ${valueRuntimeType.runtimeType == type.runtimeType}');type: StringvalueRuntimeType: Stringtype.runtimeType: _TypevalueRuntimeType.runtimeType: _TypevalueRuntimeType == type: false, must be truevalueRuntimeType.runtimeType == type.runtimeType: true
Or is it written that this is it should be so?
var handle = callback;queue.enqueue (handle);/ / And laterqueue.indexOf (handle) / / is always -1
splitClosure2 = splitClosure1; assert(splitClosure1 == splitClosure2);
Please explain WHY this doesn't workvar handle = callback;queue.enqueue (handle);/ / And laterqueue.indexOf (handle) / / is always -1
--
> Any known motivation for rules like this?
What rules do you mean? If you closurize a function twice, you get two closures. And they are not equal.
Now maybe I'm a little imprecise here, as long ago, there were discussions about canonicalizing (you would say interning) some kinds of implicit closures (implicit closure = closure created by closurizing a function), specifically those created from top-level functions and static methods, and I don't remember what the current state is. Those closures would be equal, because they are identical. But again, I'm not exactly sure what the current state is.
Rule of thumb is: create the closure once, store it in a variable and use that variable. That's the first example that I said should work.
LT
Function is something that can be called and maps its input parameters (0 to n) to its result (0 to 1).
It may have side effects or not.
Closure is a totally orthogonal concept of some object "closing" about the scope in which it was created, carrying that around with it.
It must not even be a function. Could be an anonymous class' instance.
It is a misconception that came up at the birth time of Ruby and Groovy to call a function-like thing that (even if only potentially) encloses a scope a "closure" and use these terms interchangeable.
Am 14.03.2013 21:07 schrieb "Gilad Bracha" <gbr...@google.com>:
>
> Sorry to contradict, but the term closure has been used both as abstract description of closing over variables, and specifically for procedures/functions that do so long before Ruby or Groovy were so much as a glimmer in their designer's eye.
>
Thanks Gilad.
What was new with Ruby/Groovy IMHO was the fact that functions are called closures even if they _don't_ close over any variable, moving to the notion of "closure is a function that can be passed around".
That exactly led to the idea that "Groovy has closures, Java not", what in this form is not true.
Am 14.03.2013 21:12 schrieb "Alex Tatumizer" <tatu...@gmail.com>:
>
> @Dirk: the most unexpected fact for me is that context is incorporated into "closure" automatically.
> That is, when you write
> var f=str.substring
> There's an optical illusion that you extract attribute from str, like normal attribute.
Well, the latter is sadly true.
But the above expression simply is a function definition in point-free style.
That is
var f=str.substring
is equivalent to:
var f= (x,y) => str.substring(x,y)
It is not 'str' that gets factored, but the expression 'str.substring'.
Now here you indeed build a "context" (=surrounding function), which closes over str, but that is not necessarily the case, and not necessarily uses this style, and thus why I said "orthogonal".
At least I assume that Dart works here like Haskell?
I.e. in the tradition of:
var f = (string x) => x.length
var g = (int y) => y * 10
var h = g compose f //no parameter lists -> point free
Resulting semantically in
h = (string x) => g(f(x))
(Writing from my mobile so can't look things up now)
> BUT in fact, "str" gets factored into returned function value.
> Magic.
No. Simple transformation. Missing argument list on the right leads to additional parameter list on the left.
Some Functions are in fact closures, meaning that they carry their lexical environment, but not all of them. I'm not sure about renaming Function to Closure, I think it would make some situations more clear and some less.
@Seth, not to be misunderstood, I certainly LIKE the feature. This construct is a source of trouble in javascript, forces you to write f.call afterwards, and I keep forgetting the difference between f.call and f.apply, so need to go to mozilla reference every time :)
It's just that there's MAGIC there, and the magic has to be better emphasized in the tutorial. Especially the difference between javascript interpretation of Function and dart's.