Is there a language where we can mark value as result and then continue function/method body? - return

I was reading smalltalk tutorial and this idea came to my mind.
Assume we have some language and there instead of return we're marking some value as final return value and then we continue method, once method end is reached - no matter what else was called we're returning marked value unless something else specified manually like this:
Let's say ^^ is a operator that marks value for return if no explicit return is found till end of method
m1()
{
^^1;
some_other_code;
}
m2(par)
{
^^2;
if par == 1: return 1;
some code;
}
m3(par)
{
if par == 1: return 1;
else if par == 0: ^^0;
do some stuff;
if par < 0: return -1;
}
m1() should return 1
m2(0) should return 2
m2(1) should return 1
m3(0) should return 0
m3(1) should return 1
m3(-2) should return -1
This is a little similar to ruby's tap() but not the same

Pascal did that. The return value was set by assigning to the function name. Swift does something not quite the same: You can mark code anywhere that will be executed when the function exits. Your return statement exists, but only after other code written somewhere else gets performed.

Related

Deprecated Functionality: usort(): Returning bool from comparison function is deprecated, return an integer less than, equal to, or greater than zero

Ran into a slight issue here below with some of my code.
// sorting
$sortField = $this->sortField;
$sortDir = $this->sortDir;
usort($data, function ($a, $b) use ($sortField, $sortDir) {
if ($sortDir == "asc") {
return $a[$sortField] > $b[$sortField];
} else {
return $a[$sortField] < $b[$sortField];
}
});
A bit confused here on what i need to change.
I read this in another thread.
PHP 8 introduced the Stable Sorting RFC, which (as it sounds) means that all sorting functions in PHP are now "stable".
The spaceship operator is used for comparing two expressions. It returns -1, 0 or 1 when $a is respectively less than, equal to, or greater than $b. Comparisons are performed according to PHP's usual type comparison rules.
So does this mean I need to add the spaceship operator here in the returns:
return $a[$sortField] <=> $b[$sortField];
} else {
return $a[$sortField] <=> $b[$sortField];
}
That is it?

[Clang RecursiveASTVisitor]How to make a distinction between 'If' statement and 'Else If' statement?

I'm using clang's RecursiveASTVisitor to parse some C code. I override visitStmt() and want to make a distinction between 'If' statement and 'Else If' statement.
Example:
if(a > 0){
XXX
}
else if(a == 0){
YYY
}
else{
ZZZ
}
In visitStmt(), I use:
if(isa<IfStmt>(S))
to judge whether a statement is IfStmt.
But how can I know the statement is 'Else If' rather than 'If'?
I want to insert different stubs(XXX under 'If' and YYY under 'Else If').
ps: I'm not a native English speaker. Please forgive me for my poor express.
In C there is not specific else if statement, because
if (a) {
...
}
else if (b) {
...
}
else {
...
}
is conceputally (and in terms of an AST) something like the following
if (a) {
...
}
else {
if (b) {
...
}
else {
...
}
}
So the first snippet is sort of a "hidden nesting", because the body of the first else just consists of a single if ... else statement, so you can omit the {} around it. And the last else does not belong to the first if but to the second.
So what you probably can do to determine whether you have an else if or not, is step up in the tree, and check if the parent node of the current node is an else statement and the if is the first (or only) statement in that parent-else's body.
There are other languages (for instance VB) which have a If .. ElseIf .. Else construct. But C doesn't.

Can anyone explain how we will get the output of the following code in Lua?

function ReturnTwoVal()
return "1","2"
end
function ReturnThreeVals()
return "x","y","Z"
end
TblA = {ReturnThreeVals(),ReturnTwoVal() }
print(TblA[2],TblA[1], TblA[2], TblA[3], TblA[4])
Output will be: 1 x 1 2 nil
Expressions that return multiple values are adjusted to a single value, unless they are the last expression in a function call or table constructor.
Therefore,
TblA = {ReturnThreeVals(),ReturnTwoVal() }
is equivalent to
TblA = {"x", "1","2"}

What is the difference between a return and a break in an infinite loop?

In doing the Guessing Game from the Rust Book (I won't put the link because it will probably not exist in a few weeks), there is a point where you use return to break out of the loop. I used break instead:
loop {
guess = guesser();
guess_cast = guess.trim().parse();
let guess_num = match guess_cast {
Ok(num) => num,
Err(_) => {
println!("Guess was not a number, try again");
continue;
},
};
if guess_num < answer {
println!("Too low");
} else if guess_num > answer {
println!("Too high");
} else {
println!("You guessed it!");
break;
}
}
Is there a real difference in using return over break in this case? What about other cases when you want to break out of an infinite loop?
If the loop is the final statement in the function, then there is patently no functional distinction between break; and return;. If this condition does not hold, there is evidently a difference, for return terminates execution of the function, while break only terminates execution of the loop.
In the case where they are functionally the same, which you use may vary based on context (which one feels better in this particular location), personal preference and what, if anything, you ate for breakfast.

What is the default return value of a Dart function?

The function below works even though I intentionally deleted 'return' command:
main() {
add(i) => i + 2; //I intentionally deleted 'return'
print(add(3)); //5
}
But, the function below doesn't work after I intentionally deleted the 'return' command.
main() {
makeAdder(num addBy) {
return (num i) {
addBy + i; //I intentionally deleted 'return'
};
}
var add2 = makeAdder(2);
print(add2(3) ); //expected 5, but null.
}
Edited to clarify my question.
The last sentence in the latter function above, add2(3) doesn't return a value(I expect 5) but just null returns.
My question is why 'addBy + i' of the latter function doesn't return contrary to the fact that 'add(i) => i + 2' of the first function returns 'i + 2'.
Edited again.
The answer is in the fact of '=>' being {return }, not just {}.
main() {
makeAdder(num addBy) => (num i) { return addBy + i; };
var add2 = makeAdder(2);
print(add2(3) ); // 5
}
Even the code below works as '=>' has 'return' command in it.
main() {
makeAdder(num addBy) => (num i) => addBy + i; ;
var add2 = makeAdder(2);
print(add2(3) ); //5
}
In Dart each function without an explicit return someValue; returns null;
The null object does not have a method 'call'.
makeAdder (add2) without return returns null and null(3) leads to the exception.
I would like to quote two important note here. It might help others:
Though Dart is Optionally typed ( meaning, specifying the return type of a function such as int or void is optional ), it always recommended to specify type wherever possible. In your code, as a sign of good programming practice, do mention the return type.
If your function does not return a value then specify void. If you omit the return type then it will by default return null.
All functions return a value. If no return value is specified, the statement return null; is implicitly appended to the function body.

Resources