In SML, the following is possible for modelling lazy programming,
// Have a datatype to wrap a computation
datatype 'a susp = Susp of (unit -> 'a)
// A function to hold the computation
fun delay(f ) = Susp(f)
I know that closures can be written using Blocks,
int multiplier = 7;
int (^myBlock)(int) = ^(int num) {
return num * multiplier;
};
So I think I can use it as a function argument. The next step would be how to use functions with no real arguments ( unit value e.g. in SML fn () =>) and creating lazy datatypes as the one above.
Is this possible or should I be pursuing a different more obvious way ?
The end goal would be to emulate the suspended computation behaviour from SML,
let val x = Susp(fn () => horribleComp(345))
in
force(x) + force(x)
end
where force(x) is
fun force (Susp(f)) = f ()
Cool question!
You could implement a lazy container in Objective-C as follows (but you probably shouldn’t, see below):
That’s a lot of boilerplate, but whatever. Then, you could use it like this:
But that’s all rather silly, since for what you’re doing, you really don’t need another data type. Just use blocks as your datatype:
That’s a much more compact, idiomatic way of going about it, and it’s what I suggest. Unless you need to add further semantics to your lazy objects that go beyond the basic utilities of blocks, you shouldn’t wrap them needlessly.
Cheers!