Skip to content

[RFC] Object method message passing syntax #1160

@mhermier

Description

@mhermier

Hi,
One of the last features that retains me from updating #1006, is that I lack a way to implement method invocation. While ideally it should look like:

some_method_mirror.call(receiver, args)

there are some technical issues that forbid to do that in a module (basically I need at minimum a primitive or language support). And that issue makes me struggling since a while.

One idea that came to my mind (after viewing a video about OOP today), would be to add language support for that feature.

I think about 2 ideas for the syntax:

var symbol="call(_,_)"

// @ is a placeholder token, but I like it or maybe `@@`
receiver@("call()")
receiver@(symbol, arg0, arg1)
// or
receiver."call()"(args0, arg1)
receiver."%(symbol)"(args0, arg1)

Implementation wise, it would more sense to have the symbol string as the last parameter. So the opcode would not need to shift all the arguments on the stack after resolving the symbol index. But these syntax feels quite natural.

With the first syntax, since the symbol is evaluated as a regular value, it must be inside the parenthesis to avoid parsing hell problems with mixed getter/setter syntax shown in the following example:

receiver@foo.getMirror().toString(arg0, arg1) // That cannot be parsed
receiver@(foo.getMirror().toString, arg0, arg1) // Parse nicely

The second syntax has the advantage of not consuming a token for that feature. But cost the allocation of a string in what I consider it the more general usage. edit Thinking at it again the syntax is viable, by optimizing the string interpolation implementation and String.+(_) (which are really under optimized right now...)

edit: It should help to solve a not solvable problem of the constructor syntax. Because of the way constructors are implemented, you can't call a constructor named differently. With super, we end up having chains of construct new which defeat the point of having named constructor. It is a secondary usage that I would not recommend, and use super where possible, but it at least gives a solution to that problem.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions