Hacker News new | ask | show | jobs
by LoganDark 826 days ago
> Its not really an “OOP concept”.

It sort of is, because what you're doing is encapsulating the concept of "a value that may arrive in the future" with an object. Both Promises (in JS) and Futures (in Kotlin) work this way.

What I'm saying is that you don't necessarily need to encapsulate that. Rust doesn't: Futures may be called "futures", but they're actually more like "suspendable computations" (not to be confused with generators, which have their own proposal, or coroutines, which also have their own proposal). You can await one as part of another suspendable computation, but you're not holding an object that represents a concept (as in a Promise), you're holding the computation itself. By awaiting it, it becomes a part of you.

The only place a Promise/Future is necessary is when you're not using the computation directly: when you chuck a function off to a task scheduler in order for it to be executed for you. Then, in order to get anything back, you need a handle to that eventual return value. There's your Promise/Future. But if you simply don't chuck it off in the first place, and integrate it into your own suspendable computation instead, you don't need any sort of Promise/Future.

I should note that having taken a look at this library myself, it seems like both paradigms should be possible to implement on top.

1 comments

I don't see how the abstraction of a future or promise is any more inherently object oriented than than the abstraction of a first class function. They each involve a handle for a computation with particular features, and they each are unnecessary when directly embedding the computation rather than passing it around, but a first class value representing a computation is not a fundamentally object-oriented concept.