arua-meta icon indicating copy to clipboard operation
arua-meta copied to clipboard

RFC: Method Forwarding

Open Qix- opened this issue 9 years ago • 6 comments

Method forwarding in Arua is the idea that, since it's planned that there is no such thing as inheritance and only composition, we make method forwarding very easy.

struct Eye
    # ...

on Eye
    fn look(...)
        # look at something

struct Nose
    # ...

on Nose
    fn sniff(...)
        # sniff something

struct Face
    leftEye Eye
    rightEye Eye
    nose Nose

on Face
    fn look(...)
        this.leftEye.look(...)
        this.rightEye.look(...)

    use this.nose.sniff as sniff # (Face()).sniff() now calls (Face()).nose.sniff()

Qix- avatar May 31 '16 10:05 Qix-

// @Polygn

Qix- avatar Jun 04 '16 09:06 Qix-

So from what I'm gathering from this, you have in essence just built a quicker way of adding data into a structure without actually calling the structure. This is an idea I quite like, seeing as how I've never seen something like this... I like the idea that you can just at will pack a new member into the structure without modifying the structure directly, you just write a call to it and Arua will automatically pack it in. My only concern with this would be that you might possibly try to call the structure that is a private scope and try to write data to it from something in the global scope. Example:

# Struct in private scope
fn fishoven(...)
    struct Foo
        bar u32
        stuff i16

# Can't modify structure due to the structure being inside the private scope of fishoven().
on Foo
    fn modify(...)
        # Do stuff

You'd of course have to have some guard against this, which I would assume would be handled.

corbin-r avatar Jun 04 '16 15:06 corbin-r

Not exactly - I should have explained a bit more.

So for instance in Java, you have Base and Child extends Base. All of Base's protected/public methods are callable on Child objects as well.

However in Arua since there is no such thing as inheritance, only composition, re-defining a bunch of methods to call members might be tedious. However, this is a feature because, unlike C++, you're more explicitly defining the interface for your struct that would otherwise be multiple inheritance in C++.

So for in the OP's example, Head is composed of a Nose and two Eye objects. We can forward calls to Head.sniff() to the Head.nose object (so Head.sniff() forwards to Head.node.sniff()), and then explicitly define Head.look() - which calls Eye.look() for both Eyes in the head.

By using the use keyword we can "fake" inheritance by simply forwarding a few or all method calls to one of the struct's members.

struct Foo
    bar Bar

on Foo
    use bar

The above pulls all of Bar's methods and exposes them in Foo, but calling them on a Foo struct will forward those calls to the Foo.bar object.

For Bar being

struct Bar
    qux i32

on Bar
    fn baz()
        this.qux += 5

    fn bez()
        this.qux -= 5

the example above is equivalent to

struct Foo
    bar Bar

on Foo
    fn baz()
        this.bar.baz()

    fn bez()
        this.bar.bez()

But since Bar could have hundreds of methods this might not be ideal, so we can use bar to forward those calls to Foo.bar. Alternatively, if Bar has a hundred methods and we only want to forward a handful we can use on bar use baz.

The use of use X and on X use Y within struct behaviors is called Method Forwarding, hence this RFC.

Qix- avatar Jun 04 '16 19:06 Qix-

Ohhhhh I see now. Well shoot. That is quite a handy little feature there. I quite like it haha. I can see how this would reduce overhead again because now instead of the compiler linking say hundreds of methods.. It merely forwards them all to a single location Foo.bar. I really like this...

corbin-r avatar Jun 04 '16 19:06 corbin-r

@Polygn it's still generating methods for them, but the syntax is much cleaner. :) See #23.

Qix- avatar Jun 05 '16 05:06 Qix-

@Qix- exactly. This is very clean. A lot easier than the way C++ does it, for sure! And I'll read up on the ABI you've written up!

corbin-r avatar Jun 05 '16 05:06 corbin-r