Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to persist a Swift closure?

I need to persist a closure but it doesn't seem possible with any persistence methods available in Swift and I can't seem to find any information.

like image 632
diegomontoyas Avatar asked Oct 31 '22 10:10

diegomontoyas


1 Answers

Since the "Code" part of the closure is fixed (compiled) there is no need to persist it, you only need to keep a reference to it with a keyword or some other identifier. This can be achieved using a dictionary with references to functions for example (i.e. a factory of functions).

What actually needs persistence are the variables that are captured by the closure. This is equivalent to parameters of a function.

So basically, you should probably approach your problem differently and define a set of function keywords and parameter lists. You can easily persist those and later "execute" the data by passing the parameters to the factory of functions associated with the keywords.

like image 137
Alain T. Avatar answered Nov 09 '22 11:11

Alain T.