Just realised that the following seems to work :

struct Cls<'a> {
    vec : &'a ~[~int]
}

impl<'a> Index<uint,&'a ~int> for Cls<'a> {
    fn index(&self,i:&uint)->&'a ~int {
        &(self.vec[*i])
    }
}

fn main(){}


On Wed, Apr 16, 2014 at 6:23 AM, Artella Coding <
[email protected]> wrote:

> Hi Eric, this is an example of code which gives the error :
>
> ************************************************************
> struct Cls {
>       vec : ~[~int]
> }
>
> /*
>     Does not compile, yielding error message :
>
>
>     "method `index` has an incompatible type for trait:
>     expected concrete lifetime, but found bound lifetime
>     parameter &"
> */
> impl<'a> Index<uint,&'a ~int> for Cls {
>       fn index(&'a self, i: &uint) -> &'a ~int {
>           let val = &'a self.vec[*i];
>           return val;
>       }
> }
>
> fn main(){}
> ************************************************************
>
> It is easy to write a function which does the exact same thing (for the
> struct above) and has a return type with a bounded lifetime, and therefore
> it does not make sense to me why operator overloading would be restricted
> in this fashion (i.e. restricted to return concrete lifetimes). Thanks.
>
>
>
> On Wed, Apr 16, 2014 at 2:48 AM, Eric Reed <[email protected]>wrote:
>
>> Could you provide a code sample that causes this error?
>>
>>
>> On Tue, Apr 15, 2014 at 6:28 AM, Artella Coding <
>> [email protected]> wrote:
>>
>>>
>>> Currently if I try to specify lifetimes in the return types of
>>> overloaded operators like Index ([]), I get an error message :
>>>
>>> "method `index` has an incompatible type for trait: expected concrete
>>> lifetime, but found bound lifetime parameter &"
>>>
>>> Why has this restriction been placed, given that I can write custom
>>> functions which can have bounded lifetimes specifications in the return
>>> type?
>>>
>>> Thanks
>>>
>>> _______________________________________________
>>> Rust-dev mailing list
>>> [email protected]
>>> https://mail.mozilla.org/listinfo/rust-dev
>>>
>>>
>>
>
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to