On Fri, Nov 15, 2013 at 05:05:28PM +0100, Gábor Lehel wrote:
> I have some ideas about typey things and I'm going to write them down. It
> will be long.
I haven't read this all yet, I just want to respond to the first few
paragraphs. :)
> It would be nice if `Trait1 + Trait2` were itself a trait, legal in the
> same positions as any trait. This is already partly true: in trait bounds
> on type parameters and super-traits of traits. Where it's not true is trait
> objects, e.g. `~(ToStr + Send)`. Having this could remove the need for the
> current `~Trait:OtherTraits` special syntax.
It's plausible to imagine, though I wouldn't phrase it that "Trait1+Trait2"
is a trait, but rather a form of generalized object types. Today object
types have the form `~Trait` and these generalized object types would
have the form `~(Trait1+...+TraitN)`. Probably their size would be
N+1 words to account for the extra vtables, which would make it trival
to support the (natual) subtyping relation of the form
~(Trait1+Trait2) <: ~Trait1
~(Trait1+Trait2) <: ~Trait2
You can sort of do something similar today with
trait Trait3 : Trait1 + Trait 2 { }
impl<T:Trait1+Trait2> Trait3 for T { }
Note that the current syntax `~Trait:Bounds` is intentionally limited
in that the traits that can appear in `Bounds` do not offer methods
but rather give information about the kind of data that the object
receiver consists of.
> I wonder whether lifetimes could also be interpreted as traits, with the
> meaning: "[object of type implementing lifetime-trait] does not outlive
> [the given lifetime]". This is an honest wondering: I'm not sure if it
> makes sense. If it does make sense, it would fit in perfectly with the fact
> that 'static is already a trait. Together with the above, it might also
> allow a solution for capturing borrowed data in a trait object: you could
> write `~(Trait + 'a)`.
I don't think of `'static` as a trait, though it is currently
implemented that way for historical reasons that will hopefully soon
be eased. Rather, type parameters can have a *lifetime bound*, which
indicates the minimum lifetime that they can exist for. `'static` is
just a special case of this. This might be (for all intents and
purposes) what you meant. In particular, one should be able to do
`~Trait:'a`, which is interpreted as:
a pointer to some type T which implements the trait `Trait`
and which contains data valid for at least the lifetime 'a
Niko
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev