Orthogonal-Classes
Orthogonal-Classes copied to clipboard
Expose statics on instances
I'm curious why this.#helper() would be disallowed? This seems to be a very very common language pattern, but I've never understood a good reason for it.
I understand that it's not the instance per-se that has the field, but it's class does, and the # implicitly says to me- look at this instance's class for this field.
There are a number of very straightforward and useful places where having static methods virtualized seems like it'd be potentially very handy. A really simple example of what this could enable:
class Human{
static move(){ console.log("walk walk walk")
constructor(){}
loop(){ this.#move() }
}
class Driver extends Human{
static move(){ console.log("drive drive drive") }
}
class Racer extends Driver{
static move(){ console.log("race race race") }
}
It seems intuitively obvious and simple to a laymen like me that I'd want to be able to ask for whatever the current class's static is. Time and time again I run into languages where this is impossible or requires extensive and painful reflection steps to accomplish. I can understand the conceptual argumentation that this doesn't have a static field, but especially if we have a symbol designating that the field is static- # it seems like there's no chance for ambiguity and we can should and ought allow this straightforward ask- give me the static field for whatever my class is.
In EcmaScript, not having this would imply having to write userland code to walk the prototype chain, looking at constructors. This seems suboptimal and painful from a usability and performance standpoint both.
I'd like to see statics exposed on instances, especially given that there is a # indicator in front of the field indicating very clearly the user's intent to look at statics. Please allow this.#helper().
The # prefix doesn't mean "static". If is something completely different, a non-property "field" that can only be referenced from within the body of the class that defines it.
What you are asking for seems to be what some language call "protected" fields (or properties).
I must have been conflating different topics.
I'm not very interested in private fields.
My real interest is in getting a this.myStatic, in having static imply singleton accessible both from on the constructor and from the instance. It feels like more of a stretch ask, but semantically I've always found it be-deviling that no language makes it easy to ask for a static by name for the current instance.
Do you mean you want a syntactic way to do this.constructor.foo, like static.foo?
or something like what is shown in https://github.com/erights/Orthogonal-Classes/issues/16#issuecomment-287908053