Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interface implementation #18

Open
Spydr06 opened this issue May 6, 2023 · 3 comments
Open

Interface implementation #18

Spydr06 opened this issue May 6, 2023 · 3 comments

Comments

@Spydr06
Copy link
Owner

Spydr06 commented May 6, 2023

Draft on how interfaces will be implemented:

import "std.csp";

type Foo: interface {
    fn foo(self: &Foo): i32
};

type Bar: struct {
    x: i32
};

# how to implement Foo for Bar?

fn main(): i32 {
    let bar = Bar::{0};
    (&bar: &Foo).foo();

    <- 0;
}

The main question is: how should the syntax for implementing Foo for Bar look like?

@Its-Kenta
Copy link
Contributor

Its-Kenta commented May 6, 2023

Personally not a fan of this syntax. It feels slightly confusing.

Thought of this way:


type Foo: interface {
    fn foo(self: &Foo): i32
};

type Bar: struct {
    x: i32
} impl Foo {
    fn foo(self: &Foo): i32 {
        # implementation here
        …
    };
…
# Call with bar.foo()

This would also allow calling multiple interfaces and their implementations followed by a comma.

I think this way it clearly shows that struct Bar is implementing the Foo interface. The shown example on your end is slightly confusing on what it’s trying to achieve imo.

@Spydr06
Copy link
Owner Author

Spydr06 commented May 7, 2023

I might have to think abt it a bit more, but that looks great as a start!

@Spydr06
Copy link
Owner Author

Spydr06 commented May 7, 2023

When implementing this, I'll improve the with statement too.
currently, this uses a clunky [drop] compiler directive, which is far from perfect.

In the future I want there to be a pre-defined interface, that allows the with statement to call the "exit"/"drop"/"free" function (however you call them)

This would be defined by the compiler internally and could be invoked by any type:

type With: interface {
    fn drop(self: &With)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants