How we call a function in swift3.0 - ios8

func directionsFromCurrentLocation(to:CLLocationCoordinate2D,directionCompletionHandler:DirectionsCompletionHandler){
This code is used in apple map for finding between two destination.
and, i am using this code.
mapManager.directionsFromCurrentLocation(to: destination!) { (route, directionInformation, boundingRegion, error) -> () in

If you're bothered by the trailing closure ( the { } delimited chunk of code at the end of your function call) please read the docs here.
In short (taken from the link above):
If you need to pass a closure expression to a function as the
function’s final argument and the closure expression is long, it can
be useful to write it as a trailing closure instead. A trailing
closure is written after the function call’s parentheses, even though
it is still an argument to the function. When you use the trailing
closure syntax, you don’t write the argument label for the closure as
part of the function call.
func someFunctionThatTakesAClosure(closure: () -> Void) {
// function body goes here
}
// Here's how you call this function without using a trailing closure:
someFunctionThatTakesAClosure(closure: {
// closure's body goes here
})
// Here's how you call this function with a trailing closure instead:
someFunctionThatTakesAClosure() {
// trailing closure's body goes here
}

Related

Does payable macro change the function parameters?

in rust, nft_mint() declaration
#[payable]
pub fn nft_mint(&mut self, token_id: TokenId, receiver_id: AccountId) -> Self {
// ...
}
using the nft_mint() method in javascript
await contract.nft_mint({}, GAS_AMOUNT, ATTACHED_DEPOSIT);
I see that nft_mint in javascript has three arguments which are different from the rust code.
Is it because of that payable macro?
The contract function arguments are passed in the first argument (object) on JS side, so it should be:
await contract.nft_mint({ token_id: "my_kitten", receiver_id: "frol.near" }, GAS_AMOUNT, ATTACHED_DEPOSIT);
“payable” attribute just adds a single assert at the beginning of the function body that checks that the attached deposit is non-zero. It does not alter the function arguments.

Node Promise with Local Variable

async function A(){
const x = await B(); // reads a file or such thing
return x; // returns a promise
}
var foo;
function setFoo(){
foo = A(); // foo will be set to the promise returned by A()
// but I really want foo to be set to the data
// returned by the promise
}
function C(){
// here I need to use the value of the data returned
// by the promise. There are dozens of functions that
// will use foo. Those dozens of functions are called
// from outside of this module
}
Question - Is there some way to extract the data from the promise so I can use it in a local variable? Or do I need to use promise semantics i.e .then(function(...){...}) in every function like function C that needs to use the local variable foo? Or something else?
Don't use async in a function that does nothing other than return await. Just return the promise and you're done.
function A() {
return B(); // reads a file or such thing, returns a promise
}
Never try to set global/outer-scope variables to capture asynchronous values. Keep returning the promises, chain in worker functions with .then(). Keep returning the promise chain.
function foo(){
return A().then(C);
}
The worker functions (an event handler, really) receive the data:
function C(data) {
// here you get the data returned by B()
}
You could even wait for C() now:
foo().then(function (x) {
// `C()` ans finished and `x` will be whatever `C()` returned.
})

How do you implement specific behavior for a Mockery function

I'm using the Go Mockery package, and I want the function Next to do some actions before returning. However, when I do this:
mockIter.On("Next", mock.AnythingOfType("*types.Query")).Return(func(q *types.Query) bool {
condition := (do something that returns a boolean)
return condition
})
I get this error:
panic: interface conversion: interface {} is func(*types.Query) bool, not bool
I must be using the package wrong, but it seems like this is correct because they have this very similar example in their docs:
Mock.On("passthrough", mock.AnythingOfType("context.Context"), mock.AnythingOfType("string")).Return(func(ctx context.Context, s string) string {
return s
})
Any ideas on what I'm doing wrong?
Return function is used to return values on call of the mocked function. You can't change the signature of the mocked function.
Here you're returning func(*types.Query) bool (function that returns boolean) instead of bool, as the error message say.
You can have the function to make some computation and then return the value. You just have to add () (or whatever arguments your function accepts) after function definition to invoke it and you'll be good to go.
This works:
mockIter.On("Next", mock.AnythingOfType("*types.Query")).Return(func() bool {
condition := (do something that returns a boolean)
return condition
}())
But it seems to me that you want to do some computation based on the argument that is passed on the mocked call. I don't think mockery supports that at the moment.

How to chain completables with closures in RxSwift?

I would like to do something like
Completable.empty()
.andThen { // this does not work
// run some code
NSLog("Now pushing objects")
return Completable.empty // e.g. return api.pushObjects() -> Completable
}
.andThen(Completable.empty()) // this is working
.andThen { // this does not work
// do something else
NSLog("Now pulling objects")
return Completable.empty // e.g. return api.pullObjects() -> Completable
}
andThen (or concat) does not allow closures as argument. The compiler complains:
Trailing closure passed to parameter of type 'Completable' (aka 'PrimitiveSequence<CompletableTrait, Never>') that does not accept a closure
My question is: How to chain completables with closures?
You don't. There is no version of andThen that takes a closure. The closest you can get to it is this:
Completable.empty()
.andThen(Completable.create(subscribe: { observer in
print("Now pushing objects")
observer(.completed)
return Disposables.create()
}))

Fallback callback when calling unavailable function

Is it possible to set a fallback callback which is called when the user wants to call a function which does not exists? E.g.
my_object.ThisFunctionDoesNotExists(2, 4);
Now I want that a function is getting called where the first parameter is the name and a stack (or something like that) with the arguments passed. To clarify, the fallback callback should be a C++ function.
Assuming your question is about embedded V8 engine which is inferred from tags, you can use harmony Proxies feature:
var A = Proxy.create({
get: function (proxy, name) {
return function (param) {
console.log(name, param);
}
}
});
A.hello('world'); // hello world
Use --harmony_proxies param to enable this feature. From C++ code:
static const char v8_flags[] = "--harmony_proxies";
v8::V8::SetFlagsFromString(v8_flags, sizeof(v8_flags) - 1);
Other way:
There is a method on v8::ObjectTemplate called SetNamedPropertyHandler so you can intercept property access. For example:
void GetterCallback(v8::Local<v8::String> property,
const v8::PropertyCallbackInfo<v8::Value>& info)
{
// This will be called on property read
// You can return function here to call it
}
...
object_template->SetNamedPropertyHandler(GetterCallback);

Resources