4 ms·
I mean that's fine but the language decision that makes my small brain break is "why call it a function, instead of calling it a completely different name to re
by throwawaymaths 4y ago
I mean that's fine but the language decision that makes my small brain break is "why call it a function, instead of calling it a completely different name to represent the completely different things that it is"?
- Waterluvian 4y agoI 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.
- activitypea 4y agoThis was killing me when back when I was a Python dev. A "generator" is just a callable object made less reasonable.
- throwawaymaths 4y agoEven callable objects are kinda brain breaking. I used to joke that the duality of callable objects (and getter/setters, function prototypes in js) were as mysterious as the wave/particle duality in quantum mechanics.
- Izkata 4y agoI mean, if we're talking python here, functions are callable objects.
- throwawaymaths 4y agoGood point.
- eurasiantiger 4y agoFunctions are objects too.
- lmm 4y agoYep. The only way I've ever been able to understand generator/coroutine-based code is by desugaring it into iteratees into my head. (Before I learned about iteratees, I simply couldn't understand them at all).