What's the difference between returning void and {.noreturn.}? - pragma

In Nim, the noReturn pragma marks a proc that never returns.
How is that different than a function that returns void?

Returning void means the function returns nothing:
proc saySomething(): void =
echo "something"
The empty brackets as well as the : void are optional:
proc saySomething =
echo "something"
Annotating a function with noReturn means the function will not return at all:
proc killTheProgram {.noReturn.} =
quit(0)
proc raiseSomething {.noReturn.} =
raise newException(ValueError, "Something")

Related

How to have function as field value in a lua table, when the function is declared at a later point?

I have the following scenario in which the position of code shall not change. How to modify this code to fix the error without moving the function and table variable. I am a Lua newbie, just 4 days
function a()
print("function a");
end
ftable = {
["a"] = a,
["b"] = b
};
function b()
print("function b");
end
ftable["a"](); -- prints 'function a'
ftable["b"](); -- attempt to call field 'b' (a nil value)
Update : Using the following mechanism its possible to do this.
function a()
print("function a");
end
ftable = {
["a"] = a,
["b"] = "b"
};
function b()
print("function b");
end
ftable["a"](); -- prints 'function a'
_G[ftable["b"]]();
Lua's table declaration and function definition (and function calling syntax) is very flexible. You can use the field key as an identifier instead of the index syntax, provided that the key is a string and is also a valid identifier ({ a = a } and ftable.a()). This seems to be the case with your a and b keys. You can also make the field assignment with the function definition statement (function ftable.b…).
function a()
print("function a")
end
ftable = {
a = a
}
function ftable.b()
print("function b")
end
ftable.a() -- prints 'function a'
ftable.b() -- prints 'function b'
Of course, you could also move "function a" down and code it like "function b". that would leave ftable = { } at the top.
One difference from your code. The global variable b is no longer set to the function. In most cases, this would be considered an advantage.
I would also like to re-iterate what #lhf said,
There are no declarations in Lua, only definitions.
A function definition (in the general case) is an expression. When evaluated, it produces a function value. Various types of statements can assign the value to a variable or field, as does function ftable.b()….
You can't do that. There are no declarations in Lua, only definitions. You need to define variables before using them.
What you can do is to register the names of the functions in ftable and then fix the values before using them:
ftable = {
["a"] = true,
["b"] = true,
}
...
for k,v in pairs(ftable) do ftable[k]=_G[k] end
This code assumes that your functions are defined globally.

Lua / Initialize table with inline function code but assign value not function

I have this Lua code to initialize a table:
table = {
a = 1;
b = myfunc();
c = function () <some code> end;
}
After this table.c has type function and I have to use table.c()
in a print statement with the .. operator to get the result. But I would like to just use table.c instead.
Is there a way that I can get the return value of the function assigned to table.c so the type is not function without having to define a function outside of the table?
If you wanted table.c to contain the return value of the function, then you should have assigned it the return value of the function. You instead assigned it the function itself.
To get the return value of a function, you must call that function. It's really no different from b. myfunc is a function; myfunc() is calling that function and storing its return value.
But, due to Lua's grammar, calling a function that you are defining requires that you need to wrap the function constructing expression in (), then call it:
c = (function () <some code> end)();
This of course will only contain the value of that function at the time that the table is constructed.

can pcall return value of called function instead of boolean result true/false?

Can pcall return value of called function instead of boolean result true/false?
for example
function f()
return 'some text'
end
print(tostring(pcall(f)))
print will show only true or false instead of value returned by f
tostring selects only first parameter.
a,b = pcall(f)
print(b) --> 'some text'
function f()
return 'some text'
end
local status, res = pcall(f)
if pcall() succeeded, status is true and res is the return value of f().
if pcall() failed, status is false and res is the error message.

Dart lambda/shortland function confusion

I'm still pretty new to Dart and the syntax of => (fat arrow) still confuses me (I come from C# background).
So in C# fat arrow ( => ) says: goes to so for example:
Action<string> action1 = (str) => { System.Diagnostic.Debug.WriteLine("Parameter received: " + str.ToString()); }
action1("Some parameter");
means: whatever send as parameter to action1 (if it could be casted to string) goes to inner scope (in our case it just printed in Debug.WriteLine()
but in Dart it's something different.... (?)
for example in Future.then
ClassWithFutures myClass = new ClassWithFutures();
myClass.loadedFuture.then(
(str) => { print("Class was loaded with info: $str"),
onError: (exp) => { print("Error occurred in class loading. Error is: $exp"); }
);
Dart editor warn me that the first and second print is: Expected string literal for map entry key. I think in C# way that str it just name for parameter that will be filled by internal callback that Future.then uses to call onValue or onError
What I'm doing wrong ?
You need to choose either block syntax or single expression syntax, but not both.
You can't combine => with {}.
Your two options are as follows using your example:
ClassWithFutures myClass = new ClassWithFutures();
myClass.loadedFuture.then(
(str) => print("Class was loaded with info: $str"),
onErrro: (exp) => print("Error occurred in class loading. Error is: $exp")
);
or
ClassWithFutures myClass = new ClassWithFutures();
myClass.loadedFuture.then(
(str) { print("Class was loaded with info: $str"); },
onErrro: (exp) { print("Error occurred in class loading. Error is: $exp"); }
);
In both cases, it is just a way to express an anonymous function.
Normally if you want to just run a single expression, you use the => syntax for cleaner and more to the point code. Example:
someFunction.then( (String str) => print(str) );
or you can use a block syntax with curly braces to do more work, or a single expression.
someFunction.then( (String str) {
str = str + "Hello World";
print(str);
});
but you can't combine them since then you are making 2 function creation syntaxes and it breaks.
In Dart => xxx is just a syntaxic sugar to avoid { return xxx; }. Thus the two following functions are equivalent :
var a = (String s) => s;
var b = (String s) { return s; } ;
You can also use => on method definitions :
String myFunc(String s) => s;
String myFunc(String s) {
return s;
}
That syntax works well in a language like javascript and also c# where it supports (param1, param2, …, paramN) => { statements } with statement being separated with semi colon. In dart, the fat arrow only supports expression which is shorthand for { return expr; }.
That explains your Error. Your code with the curly brace (exp) => { print("Error occurred in class loading. Error is: $exp"); } means you are returning a map, so it expects to see something like (param) => {"key": "value"} where key is a string literal.

How to specify a default value of a function parameter?

This function should transform each element of the list with the given function transform:
void _doSomething(List<Something> numbers, [transform(Something element)]) {...}
As I don't want to skip this method when the transform should not do anything, I wanted to give a default value to the transform method like this:
void _doSomething(List<Something> numbers,
[transform(Something element) = (v) => v]) {...}
Unfortunately, the editor tells me
Expected constant expected
Is there some workaround or simply not possible (or shouldn't be done like this at all)?
if you want to initialize a Function parameter that is also a field of your class I suggest:
class MyClass{
Function myFunc;
MyClass({this.myFunc = _myDefaultFunc}){...}
static _myDefaultFunc(){...}
}
Or more suitable:
typedef SpecialFunction = ReturnType Function(
FirstParameterType firstParameter,
SecondParameterType secondParameter);
class MyClass{
SpecialFunction myFunc;
MyClass({this.myFunc = _myDefaultFunc}){...}
static ReturnType _myDefaultFunc(FirstParameterType firstParameter,
SecondParameterType secondParameter){...}
}
You can define the default function as private method :
_defaultTransform(Something v) => v;
void _doSomething(List<Something> numbers,
[transform(Something element) = _defaultTransform]) {...}
Or check argument like this :
void _doSomething(List<Something> numbers, [transform(Something element)]) {
if (!?transform) transform = (v) => v;
...
}
Or like Ladicek suggests :
void _doSomething(List<Something> numbers, [transform(Something element)]) {
transform ??= (v) => v;
...
}
Write your default parameters inside a square bracket []
DummyFunctin(String var1, int Var2,[ String var3 = "hello", double var4 = 3.0, List<int> var5 = [2,4,5,6],] ){
// some calculation
// return something
}

Resources