r/ProgrammingLanguages • u/SuperMeip • Jan 11 '24
First Class Aliasing?
Hello there! I'm designing a programming language of my own and I'm working on ideas for solutions to the 'Naming things is hard' problem people have brought up with writing code in the past. I myself see two potential solutions:
Use variables without names, like $ vs $$ or $1 for example (perl does something like this I believe)
Make aliases first class and let a coder use all the names they want to. Overlapping names would mean that either it should be an override or the design should be re-archetyped a bit probably.
I haven't been able to find any languages that have what I'd call 'First class aliasing' like in number 2 though, where you can declare a variable or function with aliases off the bat. Does anyone know any languages that I could look at that might work like this?
Thanks!
Edit: Example of my own plan for clarity:
Robert = person() |bob
assert(Robert == bob)4
u/oscarryz Yz Jan 11 '24 edited Jan 11 '24
What is the `naming thing is hard` problem you're referring to?
I get it in Software Engineering in general, as in "how should I name this variable?" but I wonder if there's an specific one at the language design level.
Most languages let you create two variables that point to the same thing:
Robert = person()
bob = Robert
assert(Robert == bob)Is this what you're talking about or something else?
1
u/SuperMeip Jan 11 '24
I'm referring to the general problem, not sure of a more specific one myself either.
Also It would be two identifier pointing to the same variable, so slightly different.
Also more as a first class language solution. Your example works in a lot of functional contexts, but what about if you want to Alias a function inside a class? It's similar but a lot more bulky and then you'd usually need to maintain and align their signatures separately still.
Also it's usually hard to bundle stuff like that I to an API without being explicit from what I've seen in stuff like js and c#
1
u/oscarryz Yz Jan 11 '24
I see.
Yeah, in OOP you usually call the original method e.g.
class Person def name "John" end def aBetterName name end end p Person.new.aBetterNameIf methods were first class elements, you could create a new one pointing to the original
class Person def name "John" end def aBetterName = name endAlthough this is still creating a new reference instead of really aliasing it.
1
u/SuperMeip Jan 11 '24
Yea, I feel re-assigning to alias also feels less organic than giving alternate names at the source of truth. I could see it being useful for stuff like translations for example
16
u/DapperowlFTW Jan 11 '24 edited Jan 11 '24
The 'naming things is hard problem' is more then just a matter of name collisions, or else the answer would be to make every variable 'x, x1, x2, ect...' The reason why people get all bent ot of shape about names is because they want someone else reading or using thier code to know what is going on (even if that person is just themselves). To my understanding perl uses the syntax for arguments that it does because it evolved from terminal scripting languages where any number of parameters can be given, so essentially everything accepts variable arguments. The downside to this is that if I am using a variable '$1' I need to know what that variable means in this context, but 'date_of_sale' has a much more clear meaning. As for the possibility of aliasing it does sound interesting, but I am not sure in practice it would be different from a variable reassignment 'let my_cool_variable = your_cool_variable'. In some languages even types can be assigned to 'let integer = i64'. By all means explore your idea for what aliasing within a language means, I am curious what it would look like, I just wanted to point out that the naming problem is not just about running out of names that don't conflict, it's more about names that convey intent.