3 ms·
I never thought of that, and now I'm asking the same question. Perhaps there's history or mathematical explanations to this. But, yeah, "A function that can t
by Waterluvian 4y ago
I never thought of that, and now I'm asking the same question.
Perhaps there's history or mathematical explanations to this. But, yeah, "A function that can temporarily pause itself and be returned to later" is a possibly confusing overloading of the concept of a function.
- throwawaymaths 4y agoIt makes sense in a low level language like zig, because an async functions there truly is a function frame (in the sense of set up a stack and save your registers to be popped off of it at the end) but in high level languages - and even rust, if my understanding of how async works in rust - it's different and calling it a function seems incorrect.
- throwawaymaths 4y agoAnyone care to explain their downvotes?
- Waterluvian 4y agoFWIW I'm really appreciating your comments. These are all models. I really enjoy hearing how others perceive things.
- int_19h 4y agoWhy do you care what it "truly" is under the hood? The observable behavior is what matters, and in many languages, this is made explicit by some kind of "as-if rule".
- throwawaymaths 4y agoFor a low level language having a proper mental model is important. Suppose you need to inspect the generated assembly.
- jayflux 4y agoI honestly think the opposite. When I learned generators from Python the understanding of “a function that yields instead of returns” was very easy to grasp. And considering I already knew how functions worked & what their syntax was this was just an extra step (rather than starting from scratch). ECMAscript have taken a similar path to Python here. This could also be because my mental model is they are just functions that have the ability to return to the stack at a later point in time, rather than “running to completion”.
- throwawaymaths 4y agoWell maybe it's my small brain but I remember in math class it was very important that "a function has only one return value", which probably is part of why I was confused.
- WorldMaker 4y agoTechnically generators still only have one return value: an Iterable. That Iterable represents an object with a "next()" callback and you can entirely write by hand Iterables as simple dumb objects if you like. (The generator function syntax makes it a lot easier to build complex state machines, but most languages aren't going to stop you if you prefer to write your state machine by hand.)
- Dylan16807 4y agoThe mathematical definition of function also rejects all side-effects, so probably not the best thing to use around programming. Call it a routine instead?
- int_19h 4y agoWe had a descriptive yet very generic term for function-like stuff: "subprogram".
- throwawaymaths 4y agoOh man that brings memories: gosub in basic (though I think it was subroutine). Also Pascal had procedures vs functions.