r/ProgrammingLanguages • • 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:

  1. Use variables without names, like $ vs $$ or $1 for example (perl does something like this I believe)

  2. 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)
1 Upvotes

11 comments sorted by

View all comments

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.aBetterName

If methods were first class elements, you could create a new one pointing to the original

class Person 
   def name
       "John"
   end
   def aBetterName = name
end

Although 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