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

Feature request: Traits #633

Open
Fluffyalien1422 opened this issue Sep 24, 2023 · 3 comments
Open

Feature request: Traits #633

Fluffyalien1422 opened this issue Sep 24, 2023 · 3 comments
Labels
enhancement New feature or request

Comments

@Fluffyalien1422
Copy link

There are already impl blocks but no traits

Traits can be syntax sugar for regular impl blocks

trait Y {
	fn say_hi() {
		println!("Hi!");
	}
}

struct X {}
impl Y for X {}

is the same as:

struct X {}

impl X {
	fn say_hi() {
		println!("Hi!");
	}
}
@Fluffyalien1422
Copy link
Author

I've started working on this here https://github.com/Fluffyalien1422/rune

@udoprog
Copy link
Collaborator

udoprog commented Sep 25, 2023

Also see #23

Some thoughts:

Since traits and their associated methods can be defined elsewhere (e.g. other crates where they trait is defined), they can't strictly be syntactic sugar for impl blocks. So how do you propose dealing with method naming conflicts and potential disambiguation?

How would method name resolution work? In Rust, the methods of a trait are only visible implicitly if the trait is in scope, but this relies on knowing the types of values which we obviously don't in Rune.

In #23 traits are solely used to define protocols. And this could be a nice area for you to explore since we currently can't implement protocols in pure Rune.

Thanks!

@udoprog udoprog added the enhancement New feature or request label Sep 25, 2023
@Fluffyalien1422
Copy link
Author

Fluffyalien1422 commented Sep 25, 2023

@udoprog

How would method name resolution work? In Rust, the methods of a trait are only visible implicitly if the trait is in scope, but
this relies on knowing the types of values which we obviously don't in Rune.

idk how the codebase works exactly yet so idk if this will actually work but here's the idea:
A trait would really only exist for compiler errors and default implementations, so it won't matter if the trait is in scope to use the methods or not because implementing a trait will be the same as just a normal impl block
So

trait X {
	fn no_default_implementation();
	fn default_implementation() {
		println!("Hello, World!");
	}
}

struct Y {};
impl X for Y {
	fn no_default_implementation() {
		println!("Bye!");
	}
}

Is the same as

struct Y {};
impl Y {
	fn no_default_implementation() {
		println!("Bye!");
	}
	fn default_implementation() {
		println!("Hello, World!");
	}
}

So it wouldn't matter if the trait is in scope because the trait only exists to give compiler errors and to fill in default implementations, it will actually just end up being exactly the same as a normal impl block

Since traits and their associated methods can be defined elsewhere (e.g. other crates where they trait is defined), they can't
strictly be syntactic sugar for impl blocks. So how do you propose dealing with method naming conflicts and potential
disambiguation?

struct X {}
impl a::b::C for X {}

a::b::C would be resolved at compile time, if a method in the trait already exists on the struct then that would be an error

@udoprog udoprog mentioned this issue Oct 1, 2023
9 tasks
@udoprog udoprog changed the title Feat: Traits Feature request: Traits Mar 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants