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: support derive for traits #342

Closed
gmlewis opened this issue Dec 6, 2024 · 6 comments · Fixed by #345
Closed

Feature request: support derive for traits #342

gmlewis opened this issue Dec 6, 2024 · 6 comments · Fixed by #345

Comments

@gmlewis
Copy link
Contributor

gmlewis commented Dec 6, 2024

Context: https://github.com/gmlewis/moonbit-ray-tracer

In this repo, I was forced to write:

pub(open) trait Object {
  ...
  output(Self, Logger) -> Unit
  to_string(Self) -> String
}

instead of (preferred):

pub(open) trait Object {
  ...
} derive(Show)

in order to support inspect! in black-box unit tests, but the compiler doesn't allow it.
(Also, I wanted to add op_equal to support assert_eq! but couldn't get it to work with trait objects... but that's probably a different issue that is unrelated to this one.)

It might be nice to be able to derive any methods for traits, like Show, Eq, FromJson, ToJson, etc.

@gmlewis
Copy link
Contributor Author

gmlewis commented Dec 6, 2024

As an aside... now I'm remembering that I got a weird error message about Self showing up twice when I tried to add op_equal(Self, Self) -> Bool to the trait... very strange.

@peter-jerry-ye
Copy link
Collaborator

pub(open) trait Object {
...
} derive(Show)

Does

pub(open) trait Object : Show {
 // everything other than to_string / output
}

Work for you?

As an aside... now I'm remembering that I got a weird error message about Self showing up twice when I tried to add op_equal(Self, Self) -> Bool to the trait... very strange.

Maybe you are using it as trait object?

@gmlewis
Copy link
Contributor Author

gmlewis commented Dec 9, 2024

Sure, sounds good. So multiple derives would be trait Object : Show, Eq, ToJson, FromJson {...} for example ?

Maybe you are using it as trait object?

I didn't think so, but will have to try again to test it out.

@gmlewis
Copy link
Contributor Author

gmlewis commented Dec 9, 2024

Again, feel free to close if this is already the case.

@peter-jerry-ye
Copy link
Collaborator

Sure, sounds good. So multiple derives would be trait Object : Show, Eq, ToJson, FromJson {...} for example ?

It's trait Object : Show + Eq + ToJson + FromJson.

As an aside... now I'm remembering that I got a weird error message about Self showing up twice when I tried to add op_equal(Self, Self) -> Bool to the trait... very strange.

This could be a bug if you are using a typealias with a trait. Should be fixed soon.

@peter-jerry-ye peter-jerry-ye transferred this issue from moonbitlang/core Dec 9, 2024
@peter-jerry-ye
Copy link
Collaborator

Transferring to moonbit-docs since it's a missing topic in document.

@peter-jerry-ye peter-jerry-ye linked a pull request Dec 9, 2024 that will close this issue
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

Successfully merging a pull request may close this issue.

2 participants