Here you own the trait "Double". Doesn't work if you were trying to
implement a trait you hadn't just defined. The specific examples you
mentioned were Clone and Drop, so that wouldn't work.
On Tuesday, November 5, 2013, Ziad Hatahet wrote:
> The following seems to work:
>
> trait Double {
> fn double(&self) -> Self;
> }
>
> impl Double for int {
> fn double(&self) -> int {
> *self * 2
> }
> }
>
> fn main() {
> let x = 2;
> println!("{}", x.double()); // prints "4"
> }
>
>
>
> --
> Ziad
>
>
> On Tue, Nov 5, 2013 at 1:29 PM, Steven Blenkinsop
> <[email protected]<javascript:_e({}, 'cvml', '[email protected]');>
> > wrote:
>
>> As long as "you" are the person who owns the type, yeah, but I suspect
>> that's not what you mean. Coherence requires that you only implement traits
>> for types if you own either the trait or the type (or both). You can't
>> implement a 3rd party trait for a 3rd party type, since then there could be
>> multiple such implementations for a given (trait, type) pair, and coherence
>> would be broken.
>>
>>
>> On Tue, Nov 5, 2013 at 2:28 PM, Ziad Hatahet
>> <[email protected]<javascript:_e({}, 'cvml', '[email protected]');>
>> > wrote:
>>
>>> On Tue, Nov 5, 2013 at 9:17 AM, Patrick Walton
>>> <[email protected]<javascript:_e({}, 'cvml', '[email protected]');>
>>> > wrote:
>>>
>>>> On 11/5/13 2:44 AM, spir wrote:
>>>>
>>>>> Why not just add a declaration of the trait at the top of the struct
>>>>> type def?
>>>>>
>>>>> struct PairList<Val> : Iterable {
>>>>>
>>>>
>>>> You can implement traits on types that aren't structs.
>>>
>>>
>>>
>>> Isn't another effect of this is the ability to "monkey-patch" structs to
>>> implement extra methods or traits? E.g. you can later in implement a
>>> to_str() method for a type, or implement certain traits, like Clone or Drop.
>>>
>>>
>>> --
>>> Ziad
>>>
>>>
>>> _______________________________________________
>>> Rust-dev mailing list
>>> [email protected] <javascript:_e({}, 'cvml',
>>> '[email protected]');>
>>> https://mail.mozilla.org/listinfo/rust-dev
>>>
>>>
>>
>
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev