DEV Community

loading...

Can function overloading and no function declaration syntax be reconciled?

yujiri8 profile image Ryan Westlund ・1 min read

I've thought a lot about how I imagine my 'ideal' language for general purposes. I even started to write a compiler for it, but stopped because I ran into several uncertainties about how it should work. I thought one of them would make a good question to ask here.

One thing I wanted to avoid is special syntax for function declarations; function are just values that you assign to a variable like any other. Basically like Javascript arrow functions, but there's only them, no function keyword.

func = (arg1, arg2) =>
   statement1
   statement2
Enter fullscreen mode Exit fullscreen mode

But another part of the plan was to implement polymorphism by allowing multiple functions of the same name as long as they have different type signatures. Structs don't have methods; you just declare a function that take a function take them as its first argument. In paricular, object.method(arg) is actually sugar for method(object, arg). (That interpretation of the dot syntax is one idea I've come up with that's not borrowed from another language I know)

But can these be reconciled? How do you declare an implementation of the function for a different type if that would require assigning to the variable, which would overwrite its previous value (I wouldn't entertain the thought of having the = mean something different for function values on the right)?

I would declare it's impossible, but I also thought default arguments couldn't be reconciled with automatic currying and OCaml proved me wrong.

Discussion (0)

pic
Editor guide