I am wondering about the difference in behaviour between using the return statement and defining a function with the pattern foo() = Expression
It's my understanding from: Funtions that a function body can be = Expression and return takes either nothing or an Expression
The reason I am asking is that by my understanding of the documentation the following shouldn't work:
map[int,int] foo(map[int,int] env) = env[0] = 1;
Which it doesn't (causing a parse error) since assignment is a Statement. However, if I slightly modify this function:
map[int,int] foo(map[int,int] env) { return env[0] = 1; }
This seems to work. I am confused because I though return should only take expressions. Also, it looks like from playing around on the REPL this assignment returns a Value so it would be nice be able to define functions that assign and return the modified map.
Maybe I am missing something here though.
Your understanding is fine. The return statement has some extra flexibility though to allow returning the value produced by selected statement kinds such as 'for' and assignment apparently too. See the syntax definition of the return statement in the Rascal grammar here
In future versions we may remove all restrictions and allow statements everywhere.
Related
I have seen a lot of tutorials using dot, while some use 2. What is the actual meaning of this?
Example,
Array().add()
Animation()..addListener(() {})
The .. operator is dart "cascade" operator. Useful for chaining operations when you don't care about the return value.
This is also dart solution to chainable functions that always return this
It is made so that the following
final foo = Foo()
..first()
..second();
Is strictly equals to this:
final foo = Foo();
foo.first();
foo.second();
Just to be a nitpicker, .. isn't actually an operator in Dart, just part of Dart's syntactic sugar.
In addition to the mentioned use of cascades for chaining calls to functions, you can also use it to access fields on the same object.
Consider this code, taken from the Dart documentation:
querySelector('#confirm') // Get an object.
..text = 'Confirm' // Use its members.
..classes.add('important')
..onClick.listen((e) => window.alert('Confirmed!'));
The first method call, querySelector(), returns a selector object. The code that follows the cascade notation operates on this selector object, ignoring any subsequent values that might be returned.
For more information about cascades, check out Dart's outstanding documentation!
I tried the documentation: http://ocmock.org/reference/#argument-constraints and I can't make work to return any value different of 0
My code is working with:
OCMStub([self.mockCurrentUser tipOptionSelected]).andReturn(0);
How can I do something like:
OCMStub([self.mockCurrentUser tipOptionSelected]).andReturn(OCMOCK_VALUE([OCMArg isNotEqual:0]);
I want to that my stub return any Int except 0.
Thanks
It feels like you misunderstood something. A stub (by definition) has to return a predefined value. The return value is programmed behaviour of the stub. The matching occurs on the arguments to determine whether the stub matches the invocation. This is also why they are called argument constraints.
I want to use document.querySelector method to get html node. In Js I can receive null as a result. But in F# the result type is Element and it's neither nullable nor optional and it's not clear how to check it for null. How can I handle the situation when querySelector doesn't match any DOM node?
Yes, it's true the F# code assumes Element is not nullable. You can trick the compiler to think otherwise in several ways. Probably the easiest one is to box the value like this:
let el = Globals.document.querySelector(".myclass")
if (box el) = null
then doSomething()
else doSomethingElse()`
box will be ignored when generating the JS code, so there's no performance penalty in doing this.
I have some code that wants to do grab some extra return values from a function and pass them forward latter on:
local ok, ... = coroutine.resume(co)
do_stuff(ok)
return ...
However, this won't run since the ... on the variable assignment is a syntax error.
I could work around this limitation by using the old "functions arguments and variables are equivalent" trick and an immediately-invoked function
return (function(ok, ...)
do_stuff(ok)
return ...
)(coroutine.resume(co))
but I imagine doing so wouldn't be very idiomatic or efficient. Are there more reasonable ways to solve this problem of handling the remaining values returned from the resume call?
EDIT: By the way, this needs to work with nil values in the extra arguments
EDIT2: Looks like using the immediately invoked function was the best way all along.
IMHO, the best way is passing vararg as parameter to helper function as you have done in your question.
The alternative way is "pack-unpack":
-- Lua 5.2 only
local t = table.pack(coroutine.resume(co))
do_stuff(t[1])
return table.unpack(t, 2, t.n)
The idiomatic way to do this with an unknown number of return values is to wrap the function call in a table constructor:
local t = { coroutine.resume(co) }
do_stuff(table.remove(t, 1))
return unpack(t) -- table.unpack(t) in lua 5.2
While this also involves creating a temporary object, it should be a lot quicker than using a closure, and it's certainly a lot neater.
I'm new to Dart, so maybe I'm missing something here:
This works:
In my main(), I have this:
var a = _someFunction;
var b = _someFunction;
print("${a == b}"); // true. correct!
Where _someFunction is another top-level function.
This does NOT work: (at least not how I'm expecting it to)
Given this class...
class Dummy {
void start() {
var a = _onEvent;
var b = _onEvent;
print(a == b); // false. ???????
}
void _onEvent() {
}
}
Instantiating it from main() and calling its start() method results in false. Apparently a new instance of some function or closure object is created and returned whenever my code obtains a reference to _onEvent.
Is this intentional behaviour?
I would expect that obtaining multiple references to the same method of the same instance returns the same object each time. Perhaps this is intended for some reason. If so; what reason? Or is this a bug/oversight/limitation of VM perhaps?
Thanks for any insights!
Currently, the behaviour seems to be intentional, but the following defect is open since May 2012: https://code.google.com/p/dart/issues/detail?id=144
If I were to guess, I'd say that setting "var a = _onEvent;" creates a bound method, which is some sort of object that contains both the function as well as this. You are asking for bound methods to be canonicalized. However, that would require the team to create a map of them, which could lead to worries about memory leaks.
I think they made "var a = _someFunction;" work early on because they needed static functions to be constants so that they could be assigned to consts. This was so that they could write things like:
const logger = someStaticLoggingFunction;
This was in the days before statics were lazily evaluated.
In any case, I would say that comparing closures for equality is a edge case for most languages. Take all of the above with a grain of salt. It's just my best guess based on my knowledge of the system. As far as I can tell, the language spec doesn't say anything about this.
Actually, now that I've read (https://code.google.com/p/dart/issues/detail?id=144), the discussion is actually pretty good. What I wrote above roughly matches it.