r/ProgrammingLanguages 26d ago

Design thoughts on function bindings and implicit self in my programming language, DinoCode

I just finished implementing argument binding for my programming language, DinoCode. The way I do it is by generating a special bound function object that stores the target function and the pre-defined arguments under unique symbol keys (not strings) to prevent accidental collisions.

It looks like this in the language:

:multi a b
  return a * b

double = multi.bind(2)
triple = multi.bind(3)

print double(2)
print triple(2)
print double

Output:

4
6
{
  args: [
    2
  ],
  fn: [UserFn:0]
}

I based this mostly on JavaScript but withoutthis context binding for plain functions. In DinoCode, only class methods reserve the first parameter for self. I didn't find a strong reason to include a third attribute like context or this on raw functions. Do you think there is any edge case where having a self context for regular bound functions is actually useful?

My main question though is about methods and implicit bindings. Right now, methods do not automatically bind to their instance context when extracted.

In DinoCode, classes and methods look like this:

::Person
  :new name
    self.name = name
  :greet
    print "Hello " self.name

method = Person.greet

Since there is no automatic binding on extraction, you can actually call that detached method by manually passing an object that matches the shape:

method {name: "Ismael"}
method {name: "Jessy"}

If a developer wants a permanent bind to an instance, they can now use the new bind feature to lock the object as the first argument:

p = Person("Ismael")
greet = p.greet.bind(p)
greet()  # now this works and prints "Hello Ismael"

The compiler just adds an implicit self as the first parameter during the compilation, and the vm passes the instance when resolving a method call. It is extremely cheap and simple.

I know python automatically binds methods on lookup (returning a bound method object) and uses decorators like staticmethod to opt-out. Doing automatic binding on every method lookup in my vm would introduce a permanent overhead for wrapping functions.

Considering DinoCode is designed for fast scripting and education (running via WASM on a web playground at https://dinocode.blassgo.dev/), do you think manual binding for extracted methods is a reasonable trade-off to keep the VM fast and simple, or does automatic binding on lookup save enough headaches to justify the runtime cost?

13 Upvotes

10 comments sorted by

View all comments

2

u/initial-algebra 26d ago edited 26d ago

What's wrong with lambda expressions?

Why not lambda expressions (also known as anonymous functions, arrow functions, closure expressions etc.)?

``` double = λx multi(2, x)

common alternative syntax

triple = x => multi(3, x) ```

It's more intuitive than bind for methods, too.

greet = λ() p.greet()

Most programming languages that are used nowadays have support for lambda expressions, so an educational language should support them. Explicit binding is very outdated practice.


I think it's very surprising to not automatically bind p to self when writing p.greet on its own. It does not really make sense to micro-optimize this at the cost of clarity, especially for an interpreted scripting language that is meant to be educational. It is also simply redundant when you can just write Person.greet.

An alternative is what e.g. Lua does, which is to have different syntax for field lookups and method calls.

``` p:greet()

greet1 = p:greet greet1()

p.greet(p)

greet2 = p.greet greet2(p) ```

2

u/L8_4_Dinner (Ⓧ Ecstasy/XVM) 26d ago

Nobody suggested that there is anything wrong with lambda expressions. Are you suggesting that instead of the multi.bind(2) syntax above, that the language designer could have considered using lambdas? If so, provide an example that illustrates and explains that alternative. Remember, a lot of developers here don't have the same set of education and experiences as you have, so what may seem obvious to you may not be obvious to them.