4 ms·
Class hierarchies seem to me more and more like a shadow language[1]. I first realised this in Python, where: - Classes can be called like functions - Calling
by chriswarbo 11y ago
Class hierarchies seem to me more and more like a shadow language[1]. I first realised this in Python, where:
- Classes can be called like functions
- Calling a class runs a function (the constructor, called __init__)
- The constructor is implicitly passed a fresh object as an argument (usually called "self"; this is like an implicit use of "new")
- The constructor implicitly returns an initialised object (a mutated "self")
In a language with first-class functions, it's also pretty arbitrary to define "attributes" inside the constructor and "methods" outside, since methods are functions which are values (modulo the implicit binding stuff).
Hence classes can be completely replaced by their constructors; or in other words, classes are a design pattern when writing functions. This is a realisation that's become mainstream due to Javascript, and that's the first thing I thought of when I saw the Ceylon code in this article.
The details at the end, about execution order, partial constructors, inheritance, etc. all screamed "shadow language" to me: if you've already got functions(/methods), can't you just re-use them instead of defining a new, semantically distinct category?
[1] http://gbracha.blogspot.co.uk/2014/09/a-domain-of-shadows.html http://gbracha.blogspot.co.uk/2014/09/a-domain-of-shadows.ht...
- couchand 11y agoIndeed, the Ceylon constructors even read like a regular old static factory method in any other OO language, another pointer to the functional underpinnings of objects.
- gavinking 11y agoTotally this!
- gavinking 11y agoSure. 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.