I somehow found the call() method is on every function.
Using this method I could change my if (callback != null) callback() to callback?.call().
So I tried to find the implementation and document of call(), but I couldn't. Is it just built-in method? What will be the implementation of this method? Function.apply() will be called inside it?
All Dart functions (objects which has a function type rather than an class/interface type) have a call method.
The call method has the same function type as the function itself, and it behaves exactly the same when you call it. You could even say that calling a function is implicitly calling its call method.
And, not by coincidence, the specification actually does say that: If you write the function invocation e1(e2, e3), then the compiler checks if e1 has call method, and if so converts it to the method invocation e1.call(e2, e3).
Other Dart objects may have a call method too. It's just a normal method for interface types, but if class C has a call method like int call(int x) => ..., and c has type C, then c(e2, e3) is also converted to c.call(e2, e3). It has to be a call method, not just a call getter returning a function.
Related
I have a third party API that has an event listener adding function which takes as parameter a callback function to be triggered when the event occurs. I would like to pass argument to that callback function. I'm looking for Lua's equivalent of JavaScript's bind.
The Lua code:
EventListenerAddingFunction(myCallbackFunction); // I want to add a param to the callback here
How I would do it in JS:
EventListenerAddingFunction(myCallbackFunction.bind({}, myParameter));
Can this be done in Lua?
No Lua doesn't have this feature, so closest I can think of would be making a closure-wrapper:
EventListenerAddingFunction(function(...) myCallbackFunction({}, myParameter, ...) end)
This passes your parameter everytime the callback is called, all other callback parameters will be passed next. If you don't know your parameters use ... (I don't know them so I used varargs), it's better if you pass exact amount of parameters.
I'm trying to decorate multiple functions with a function decorator, and I want to get the parameters of a function that I'm gonna decorate (in this case called fun in the parameters) and I want to pass as an argument to the returned function (in this case called func) the arguments of the gotten function from the parameters (which is called fun)
So it may look like this:
local function decorator(fun)
local function func(fun.args)
-- Write here custom behaviour to add to the function 'fun'
fun(fun.args)
end
return func
end
However, obviously there is no such thing as fun.args that was just a way of explaining with more exactitude to you what I want. Take in mind this, I DON'T know the function I want to decorate, and the functions I want to decorate may be different to each other, so this would be a way of ADDING a custom behaviour to a function (as you can see in the code example above)
So, is there a way to do what I'm needing?
Lua supports varargs via .... In your case, you'd use it like this:
local function decorator(fun)
local function func(...)
-- Write here custom behaviour to add to the function 'fun'
fun(...)
end
return func
end
And if you want to use the arguments in the "custom behaviour" section, then you can do local args = {...}, and then access them numerically (e.g., args[1] would contain the first argument).
I've an entity and I want to call each element in the entity with a function and pass an argument with it.
I know I can call the function by using entity.each(&:calculate) but I want to pass argument to the function.
def calculate(x)
puts "#{x}"
end
I'm trying with entity.each(&:calculate, x)
Here & converts the method to a proc. As far as I'm aware allowing passing arguments is an open issue: https://bugs.ruby-lang.org/issues/12115
I would simply call the block explicitly
entity.each{ |el| el.calculate(x) }
I'm trying to design an interface that abstracts long-running operation that should not be used from UI directly. To abstract it, I've created an abstract class with the only method to perform such an operation:
abstract class MakeSomething {
Result make(Param param);
}
However I can't mark it as async (tried to place in before the signature, before the return type and before the semicolon). Is it possible, and, if yes - how?
async functions almost always must return a Future. (An uncommon exception is async functions may have a void return type to be "fire-and-forget"; in such cases, there is no automatic way for the caller to be notified when the function completes.)
If you want your make function to be asynchronous and to provide a Result to the caller, it must return Future<Result>.
Note that async is not part of the function's type signature; async is a contextual keyword that enables the use of await inside the body of the function. That means that async is not very useful when declaring an abstract interface. The important part is that the function returns a Future, and derived classes can choose whether to implement that function using async/await.
Why "this" behaves differently when used inside a class function/method as compared to when it is used inside an anonymous function.
For example
public MyClass
{
function myfun()
{
output(this) // << will show the instance of this class but not myfun() function
abc = function ()
{
output ( this ) // << will show abc function
}
abc()
}
}
So, why "this" outputs the instance of MyClass but doesnot output myfun() . What makes it different from anonymous functions ?
While the specific will vary between languages, the general idea behind an anonymous inline method such as that is that the compiler will create a new class (possibly given some random name, just for it's own use), inside that class will be a method (again, probably given some auto-generated name for it's own use) and that method will do the work of the anonymous method. Then in the original call site of the anonymous method it creates an instance of this compiler generated class and calls the appropriate method within that class.
As such, the definition of that anonymous method is going to actually be called from within another type, not from within the type that declared the anonymous method.