[rust-dev] Syntax for custom type bounds

Benjamin Striegel ben.striegel at gmail.com
Thu Jan 30 19:00:25 PST 2014


> Is using dummy struct fields for specifying custom type bounds on the
struct itself really the best we can do in a brand-new language?

I as well wonder if there's a better way to do this (but I'm note the one
to ask).

> On a slightly different note, is there a strong reason for having to name
lifetime parameters explicitly?

I believe there exist situations where it makes sense for an item to have
multiple lifetime parameters, though I can't think of one off the top of my
head.


On Thu, Jan 30, 2014 at 5:58 PM, Vadim <vadimcn at gmail.com> wrote:

> Just wondering...  Does anyone else feel that "syntax" around lifetimes
> and type bounds is becoming very unwieldy?  I am referring to this PR:
> https://github.com/mozilla/rust/pull/11768.   Is using dummy struct
> fields for specifying custom type bounds on the struct itself really the
> best we can do in a brand-new language?
>
>
> At least for the lifetimes, Rust could extend existing syntax and allow
> specifying them on by-value returns objects, so that, for example, this:
>
>     pub trait MutableVector<'a, T> {
>         fn mut_iter(self) -> MutItems<'a, T>;
>         ...
>     }
>
> could be written as:
>
>     pub trait MutableVector<'a, T> {
>         fn mut_iter(self) -> 'a MutItems<T>;
>         ...
>     }
>
> This also makes the intent much more clear.   Currently, one would have to
> dig into the definition of MutItems<'a,T> to figure out that the lifetime
> parameter 'a is used to create a dummy borrow field into the vector, so
> that the whole iterator is then treated as a mutable borrow.   This feels
> very convoluted, if you ask me.
>
>
> On a slightly different note, is there a strong reason for having to name
> lifetime parameters explicitly?   Could we simply re-use actual parameter
> names prefixed with ' as their lifetimes?   The above could then be reduced
> to this:
>
>     pub trait MutableVector<T> {
>         fn mut_iter(self) -> 'self MutItems<T>;
>         ...
>     }
>
> This used to be valid syntax, btw, though it worked because 'self lifetime
> was special, IIRC.
> Note that I am not proposing to eliminate explicitly named lifetimes
> entirely,- there are still cases when they are needed.  But as far as I've
> seen, those are few and far between.
>
> Thanks for reading,
> Vadim
>
>
> _______________________________________________
> Rust-dev mailing list
> Rust-dev at mozilla.org
> https://mail.mozilla.org/listinfo/rust-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20140130/26311af9/attachment.html>


More information about the Rust-dev mailing list