Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags
(+1)

Is there a good way of returning multiple values from a function? For now the best I have is to pack them in a list and manually unpack to variables. Something like Lua’s unpacking would be really handy, but it conflicts with the current syntax: a, b: 1, 2 is a list of a, 1, 2 and assignment of 1 to b. In a world of pure functions it’s really hard to avoid multiple output.

(4 edits) (+2)

Not really an answer to my question, but a solution to my problem: I found a way to store mutable state. Using closures it’s possible to write “constructors” that return “objects” which have persistent mutable state, and calling “methods” of the “object” will change its state for all references to the object. Here’s an example of a stack constructor I’m using in my current project:

on new_stack do
 state: ()
 ("stack","push","peek","pop") dict
 (on _ do state end
 ,on _ x do state[count state]: x end
 ,on _ do (-1 take state)[0] end
 ,on _ do ans:-1 take state state:-1 drop state ans[0] end
 )
end

This method is used in the example module in the documentation, although outside the function so it only creates a single global mutable state.

Edit: A universal minimal mutable variable:

on new_var state do
 ("get","set") dict
 (on _ do state end
 ,on _ new do state:new end
 )
end