Storing a closure within an object


#1

I’ve made a SkookumscriptBehavior which takes a closure through it’s constructor. I then bind that closure to a member of that object.

(() code) [
  SkookumScriptBehaviorComponent@!()
  //code() <-- this executes correctly

  @code: code
]

calling the closure immediately works fine… but if I bind it to a member and then try and call it from that member (@code()), I get this error:

Non-closure invoke operator 'expr()' is not fully implemented yet.

Is something like this not possible yet?


#2

What type do you specify for your @code data member?

I did also find an error in the parser that returns the incorrect type for a closure in some cases which could be related.


#3

I didn’t know what to specify the type as, so I was hoping it would be inferred.


#4

[Hey, missed your response.]

If the closure that you are storing is a method (runs and returns immediately) here is how you specify the data member class type in its !Data.sk file:

// Method closure that takes no arguments and doesn't return anything
() !@code

And here are some other types of closures:

// Method closure that takes an Integer argument and doesn't return anything
(Integer num) !@code2

// Method closure that takes an Integer argument and returns a Boolean
(Integer num) Boolean !@code3

// Coroutine closure that takes an Integer argument
_(Integer num) !@code4

// Coroutine/method closure that takes an Integer argument
+(Integer num) !@code5

#5

Ohh ok, thanks for the clarification!


#6

Is there a reason not to use the + at all times? Performance or clarity? It seems like one should always use this notation because it covers everything.


#7

It depends on the needs of the situation. There are times when you want to have a closure that returns immediately such as in an if test. There are times when you want a closure to take one or more frames before it completes such as in a tight infinite loop. And there are times when it doesn’t matter and you can use the + to indicate that either immediate or durational will be fine such as an event trigger.