Interface implementation
Spydr06 opened this issue · 3 comments
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?
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.
I might have to think abt it a bit more, but that looks great as a start!
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)
}