Using only closures over functions in Swift - swift

After reading that functions are just a special case of closures, being a named constant closure, is there any reason to use a function over a closure? I ran into a problem recently where I needed to pass my functions into a method for sequential animation and ended up needing to transform my functions into closures. So with that disadvantage and loss of flexibility, why would I ever want to use functions over closures?

I advise you to use closures whenever you need to use this code just once and only in that exactly point where you implement it in your whole code.
If you need to reuse the logic of a block of code, the best way is to encapsulate your code inside a function and use it as a callback.
I can´t imagine a situation when you would need to convert all your functions into closures. All you need to do is create a function that fits with your closure signature.

Related

Function which one must not shadow in Matlab

I am writing some helper functions for unit testing in Matlab. For that I need to overload Matlab functions, built-ins etc. In the documentation is written, that one shall not overload the function builtin. I also found out that one must not overload the lt operator, since it seems to get called by the UI all the time.
Is there a list of functions which one shall not overload, or do you know of any particular problems from overloading some specific functions?
Further Information:
My use case: I want to do mutation-testing and faul-injection.
in ther words, sometimes the called functions shall return wrong results or throw an error. If I overload the Matlab functions, I do not have to change the source code to do that.

Where to define typecast to struct in MATLAB OOP?

In the MATLAB OOP framework, it can be useful to cast an object to a struct, i.e., define a function that takes an object and returns a struct with equivalent fields.
What is the appropriate place to do this? I can think of several options:
Build a separate converter object that takes care of conversions between various classes
Add a function struct to the class that does the conversion to struct, and make the constructor accept structs
Neither option seems to be very elegant: the first means that logic about the class itself is moved to another class. On the other hand, in the second case, it provokes users to use the struct function for any object, which will in general give a warning (structOnObject).
Are there altenatives?
Personally I'd go with the second option, and not worry about provoking users to call struct on other classes; you can only worry about your own code, not that of a third-party, even if the third party is MathWorks. In any case, if they do start to call struct on an arbitrary class, it's only a warning; nothing actually dangerous is likely to happen, it's just not a good practice.
But if you're concerned about that, you can always call your converter method toStruct rather than struct. Or perhaps the best (although slightly more complex) way might be to overload cast for your class, accepting and handling the option 'struct', and passing any other option through to builtin('cast',....
PS The title of your question refers to typecasting, but what your after here is casting. In MATLAB, typecasting is a different operation, involving taking the exact bits of one type and reinterpreting them as bits of another type (possibly an array of the output type). See doc cast and doc typecast for more information on the distinction.
The second option sounds much better to me.
A quick and dirty way to get rid of the warning would be disabling it by calling
warning('off', 'MATLAB:structOnObject')
at the start of your program.
The solutions provided in Sam Roberts' answer are however much cleaner. I personally would go for the toStruct() method.

self. in trailing swift closures, meaning and purpose?

whenever I use a trailing closure on an action ... example:
run(SKAction.wait(forDuration: 10)){timeRemains = false}
I’m seeing this:
Reference to property (anything) in closure requires explicitly ‘self’
to make capture semantics explicit.
What does this mean? And what is it on about? I'm curious because I'm only ever doing this in the context/scope of the property or function I want to call in the trailing closure, so don't know why I need `self and fascinated by the use of the word
"semantics"
here. Does it have some profound meaning, and will I magically understand closures if I understand this?
Does it have some profound meaning, and will I magically understand closures if I understand this?
No and no. Or perhaps, maybe and maybe.
The reason for this syntactical demand is that this might really be a closure, that is, it might capture and preserve its referenced environment (because the anonymous function you are passing might be stored somewhere for a while). That means that if you refer here to some property of self, such as myProperty, you are in fact capturing a reference to self. Swift demands that you acknowledge this fact explicitly (by saying self.myProperty, not merely myProperty) so that you understand that this is what's happening.
Why do you need to understand it? Because under some circumstances you can end up with a retain cycle, or in some other way preserving the life of self in ways that you didn't expect. This is a way of getting you to think about that fact.
(If it is known that this particular function will not act as a closure, i.e. that it will be executed immediately, then there is no such danger and Swift will not demand that you say self explicitly.)

What is a callback in programming?

I have heard this term tossed around, can anybody explain what it means and what problem it solves. Also where does this originate from.
Simplified it's when you pass a function to another function, and that other function call you back using the function you passed.
It's useful for asynchronous programming, when events can happen at any time. Or for generic handling of certain functionality to make some algorithms more generic (for example in C++ you can pass a comparison function to the std::sort function, and your comparison function will be used to compare two items when sorting).

Function Pointer

How is that function pointer better than if-else or switch case? Is it because function pointer helps callback functions and thus promotes asynchronous implementation?
It's better if the functions are not known beforehand. How would you design the C standard library qsort() without using a function pointer?
It's not "better", it's different. Yes, the main purpose of function pointer is to provide callback functionality. No, it is not directly related to asynchronism.
Check this article for more information on function pointers.
According to Nigel Jones, it improves code readability, among others
http://embeddedgurus.com/stack-overflow/2010/04/efficient-c-tip-12-be-wary-of-switch-statements/
He even provides some examples:
http://www.rmbconsulting.us/Publications/jump-Tables.pdf
Function pointers result in a better design of your code. Consider when you have an option of calling one function out of fifty functions. How massive would the switch case be? However, you could easily map all the function pointers as per their id, and call the appropriate function from the map using the "id". The code would be easily maintained and look neat.
There are other benefits and powers of using function pointer.
For a good explanation, look at the tutorial here