In my last post, I talked about open aliases and how they can be used to achieve polymorphism. Since then, I’ve been a bit stuck on the exact syntax for them. I don’t know if that’s silly or useful; syntax seems like such a superficial concern, but then again, it makes a difference if a language looks nice.
Here’s the syntax I used in that last post:
open type Boolean: def negate -> Boolean type True: def negate -> False: return False Boolean |= True
This has some nice elements, but it also has some negatives.
open typeis pretty explicit
- Con: Requires adding
openas a keyword, but it’s a natural function name for I/O (like opening a stream)
Boolean |=mirrors the familiar
|=operator (from other languages we know and love), so that we naturally read it as “
Booleanis whatever it previously was, or
|=doesn’t lend itself to being put in the
typedefinition, as opposed to top-level as above. It would have to look something like this:
type True: |= Boolean def negate -> False: return False
… but that’s not good because it reads as
True |= Boolean, which is the flip of what we really want to say. If we want to say that
Trueis an alternative for
True’s definition, we really need the open type to be on the right of the statement.
I tried various other alternatives. For instance, I thought about using ellipses to mark open types (
type Boolean = ...), but ellipses are commonly used in code fragments to say “some code goes here,” and I didn’t want to introduce that ambiguity. For adding to an open type, I even went as far as considering
True (- Boolean, where
(- was supposed to look like ∈. Nice try, but nope.
Here’s the syntax I settled on in the end:
type Boolean = ? def negate -> Boolean type True: def negate -> Boolean is Boolean ...
(Note that in this latest snippet,
... is back to its usual, informal definition of “some code goes here.”) This does require adding
is as a keyword, but I’m not too worried about that. My bigger concern with
is is that it evokes the “is a” concept from OO, but I think I’m just going to have to bite the bullet on that; everything else I can think of is worse.