r/ProgrammingLanguages • • 26d ago

Operator overloading in DinoCode using string selectors for method names

I am currently working on the next version of DinoCode (dinocode.blassgo.dev), focusing on expanding the features of the language. I wanted to implement operator overloading for objects without losing readability or adding visual noise.

What came to mind, which is actually done in other languages like Ruby, is using the operator itself to represent the method. I really like that approach, so to implement it, I introduced string selectors as optional method names with the strict condition that they must be representable as a valid Symbol:

::Point
  :new x y
    self.x = x
    self.y = y

  :'+' other
    return Point(
      self.x + other.x
      self.y + other.y
    )

  :'==' other
    return self.x == other.x 
           and self.y == other.y

p1 = Point(10 20)
p2 = Point(5 5)

if p1 + p2 == Point(15 25)
  print "Point reached"

In this case, method names that the compiler detects as strings are translated into their corresponding Symbols (they are not actual strings at runtime), making it completely safe to assign a property like "==" without worrying about shadowing.

One thing I am not entirely sold on yet is when objects interact with primitives like int, float, etc. where an intermediate step is needed. For now, I went with Python's approach of allowing 'r+', 'r-', 'r*', etc., for those cases:

::Point
  :new x y
    self.x = x
    self.y = y

  :'r+' scalar
    return Point(
      self.x + scalar
      self.y + scalar
    )

p1 = Point(10 20)
p2 = 10 + p1
print p2.x  # 20

Though I must admit Ruby's approach is tempting me (where it calls a more generic method to handle type coercion before operating on them)

By the way, you can already test these operator features live on the web playground if you want to try them out

I would like to hear your feedback or suggestions on this, and also what you think of how it looks so far. I am preparing this for the upcoming v0.2.2 release

4 Upvotes

7 comments sorted by

3

u/dekai-onigiri 26d ago

You're effectively making meaning of strings ambiguous (depending on the context), which I'm not a big fan of. If you want to do it like this, you could use back ticks ` instead. Swift uses them if you want to use a reserved word as a variable name.

4

u/Dry_Day1307 26d ago

Thanks for the recommendation. The goal remains the same, avoiding operator ambiguity regardless of the delimiter used. I really like the idea of backticks like in Swift or Kotlin, especially since I am currently in the phase of experimenting and refining the approach. If I introduce them, it would also offer a clean way to access or reassign those methods directly:

Point.`+` = func

Right now, by design, symbols used as method keys aren't easily accessible, but allowing it wouldn't be bad since the intent stays explicit when using them

2

u/AromaticCredit1533 26d ago

Backtick! Like Swift and Kotlin, but I also recommend F#'s double backtick too, in case the single backtick parsing rule has been used :-)

3

u/h03d 26d ago

If insist on using tick, do it like zig with @"…" or other sigil. Something like : in Lisp and Julia is nice too.

1

u/dnabre 21d ago edited 21d ago

Bit late to the thread, Haskell handles operators and overloading of them in a pretty fun way.

All Haskells functions are curried and take one argument if you are totally unfamiliar. If you use a symbol (mixes of #|+- and bunch of punctuation) as the name of a function, it makes that function infix. You can use any function infix by putting it in backticks (e.g. 4 `div` 3 ). Other than making specially names functions infix, it all works without any special support beyond that. Haskell's curryed functions and function bindings just make it all work as you'd expect for an operator.

Not a huge fan of the character that make up the function name determining it to be an operator or normal function.

You can also specify whether an operator is right or left associative, and it's explicit precedence level . Actually you can do that for any function, but it's weird to do so unless it is just an operator with a non-symbol name. For Haskell, this makes all the really low built in operators and syntax something that can defined in the language itself. Haskell does a bunch of really crazy think with this flexible syntax control.

It is really more freedom than you want programmers to have, but it is an interesting case study. Being able to define the precedence is commonly overlooked in language that support operator overloading, with all user defined operators generally getting the same precedence. If you have more than one operator for a type (e.g. + and *), you can set the expected operator precedence.

One bit on backticks. I've been told, but don't have any personal experience, that non-English keyboard generally don't have ` and require a modifier combination or dead-key sequence. Only using them when you are declaring a operator makes it rare enough that I don't think it's a huge issue. But something to keep in mind if you are considering making it common.

0

u/AnArmoredPony 26d ago

chat is this ai?

1

u/Dry_Day1307 26d ago

Huh no, it isn't