5 ms·
Sure. A class is certainly a kind of function: one which returns a closure over its own shared declarations. That's the way we conceptualize the notion of a cla
by gavinking 11y ago
Sure. A class is certainly a kind of function: one which returns a closure over its own shared declarations. That's the way we conceptualize the notion of a class in Ceylon, and it's why the syntax for a class looks so much like the syntax for a function, and why there's no essential difference between a function defined inside a class (a "method") and a function declared toplevel, or inside another function, or whatever. Ditto for a value defined inside a class (an "attribute") and a value declared toplevel or inside another function. It's the same thing, as far as Ceylon is concerned.
One of the design goals in Ceylon was to treat all these things uniformly, so that a class is, as much as possible, just like any other function.
However, there is a big and surprisingly important difference between a class and a function that returns a record: with a class you get open recursion between members of the class. You don't get that with a function that assigns members to a record type - or, at least, if you do build the machinery you need to get that, you have essentially reinvented classes with a worse syntax.
Open recursion seems like a small thing. But in fact, in practice, if you try taking it away from me, I will have to do some convoluted and nasty things to emulate it. Sure, there are plenty of simple classes where you can do without it, but there are surprisingly many classes where it's necessary, or at least very useful.
- vocal_bob 11y agoIt's not that's surprising. To support open recursion, a lot of functional languages consider differently top level functions and functions inside functions. That's why in JavaScript you have a murky global object with some weird scope rules that makes top level functions globally available.
- jules 11y agoWouldn't record literals with open recursion lead to a design with much less semantic duplication?
- gavinking 11y agoWell I guess it's not extremely clear to me how a "record literal with open recursion" isn't essentially the same thing as a "class".
- jules 11y agoA record literal is an expression that creates a value. A class would then be a function, as chriswarbo suggests.
- gavinking 11y agoI understand, but once you start taking into account other requirements, like the ability to functionally decompose construction of this record, you will wind up with something that looks painfully similar to a class in Ceylon.
- jules 11y agoCan you elaborate on that? Since in this model constructors are just functions you can of course define multiple constructors that may or may not call each other.
- gavinking 11y agoWell the issue in my mind is how to functionally decompose construction of a record. You have to come up with some way to express a "partial" record, and then "augment" it with new members. I'm trying to say this without using the word "inheritance" ;-)
- jules 11y agoYes, sure, you need a construct to extend a record. All but the simplest record systems have this though.
- gavinking 11y agoRight, so at that point, as I say, there's really not much daylight between "class" and "record". The difference being that a class protects its initialization logic (favoring modularity), whereas a record opens it up (favoring flexibility). Ultimately I see them as much more similar than they are different.
- chriswarbo 11y ago> One of the design goals in Ceylon was to treat all these things uniformly, so that a class is, as much as possible, just like any other function. That's reassuring to know, thanks :) > However, there is a big and surprisingly important difference between a class and a function that returns a record: with a class you get open recursion between members of the class. Record members are certainly bound too early to get open recursion, which is why I claimed that classes == functions rather than objects == records ;) In my example of Python, "objects" are abstract: created implicitly, passed into the constructor, then (implicitly) returned. > You don't get that with a function that assigns members to a record type - or, at least, if you do build the machinery you need to get that, you have essentially reinvented classes with a worse syntax. I think there are a few distinctions to be made here: - Records vs. objects; which I've addressed above. - Syntax vs. semantics: I'm fine with special-purpose syntax, I just don't like arbitrary semantic distinctions. It sounds like Ceylon is better than most in this regard :) - Built-in vs. library code: reinventing classes using functions is only a bad idea if they're already built into the language in some other way. If there's no built-in alternative, then doing this in a library is legitimate. Likewise, using the language's existing support for functions to build-in a class implementation is also legitimate, and it sounds like that's what Ceylon's done :)
- gavinking 11y agoYes, I think that's certainly what we've tried to achieve. It sounds like you and I have the same mental model, at least.