-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Trait bounds #1634
Comments
Technically, you can sort of do this with |
Traits are not lesser than types, they just aren't types (trait objects create a syntactic confusion here). Does anyone know of any Haskell extensions or anything where types are allowed to be parameterized by type classes? This seems like it would have weird implications. My impression of your problem is that you have an // A source
trait Source {
...
}
// An Event from a source
trait Event {
type Source: Source;
...
}
// An EventBus accepts events from only one source.
impl EventBus<S: Source> {
// Register an Event which is from this source.
fn register<T>(...) where T: Event<Source=S>;
...
} An If you want to define game events as having methods which aren't implemented for irc events, you could do something like this: trait GameEvent: Event<Source=Game> {
...
}
trait IrcEvent: Event<Source=Irc> {
...
} |
@withoutboats And how do you handle IrcEvent, IrcClientEvent and IrcServerEvent, and limiting a bus to IrcClientEvent (which's This would probably be most useful if you have an internal bus and an external bus, the internal bus for only your own events (for preprocessing and parsing and stuff) and the external bus shared with everything else. Instead of duplicating all your events you'd limit the internal bus to your events and then re-post on the external bus. |
@Stebalien That appears to be unstable. rust-lang/rust#27732 |
So you want to be able to create a bus which handles all That sounds to me like an trait Event<S: Source> {
...
}
impl EventBus<S: Source> {
fn register<T>(...) where T: Event<S>;
} Then I would implement This is more flexible than a kind of type hierarchy - events can be associated with arbitrary sets of sources, rather than only "a source and all of its super-types". |
@withoutboats @Stebalien 's hack is (almost, see below) exactly what I'm looking for, if it were stable... (almost: the hack doesn't let you limit the bus to just a single event type) And I'm not sure what the implications of your method would be... |
That hack also only works as long as the trait is object safe and isn't actually parameterization by traits. |
|
@withoutboats Your method would let events have different cancellability based on the source, which would be Very Bad™. I think I could split events into 2 traits: a base trait that sets cancellability and a filter trait which sets source, but that makes implementation a pain... |
It seems like this requirement is met entirely by specialisation? Can we focus this issue on anything that is misssing once specialisation stabilises? |
Closing in favor of #2190. |
I was trying to modify my
eventbus
crate to be more strict about types. That is, so you could haveEventBus<IrcEvent>
and not be able to sendGameEvents
through it, unless theGameEvent
is also anIrcEvent
. So I tried this:And as it turns out I cannot currently use
T: E
, pass a trait to<E>
, or anything like that, as Rust doesn't really like traits and traits are often considered lesser than types.Note that being able to specify inheritance bounds like that would also benefit the
typemap
andanymap
crates. Both have a shitty workaround for it that doesn't let you control the types you can use with them (and controlling those types would in turn benefit me on theeirc
crate).The text was updated successfully, but these errors were encountered: