On Wed, May 1, 2013 at 4:50 PM, Niko Matsakis <[email protected]> wrote:
> On Wed, May 01, 2013 at 02:39:12PM +0200, Gábor Lehel wrote:
>
> > So *if* the plan ends up working out, then taking the second part of my
> > earlier proposal:
> >
> > > You might also have a rule whereby dereferencing a variable when the
> > > result would be a dynamically-sized type is allowed *if* the result is
> > > immediately borrowed. Then instead of `impl Eq for {@str, ~str, &str}`,
> > > you would have just `impl Eq for str`, and if you want to compare an
> > > ~str you dereference it.
> >
> > Would that work? Would it be a good solution?
>
> I believe that would be the plan, yes. Most of these
> "apply-to-almost-everything"
> traits, like `Eq` or `Ord`, could be implemented in a similar fashion.
>
> I'm not sure what problem you are proposing this as a solution *for*,
> though. Do you mean the problem of comparing strings using `==`?
>
Yeah. Which was the original topic of this thread... :)
>
> I suppose it is true that under this proposal you could write
>
> let str1: ~str = ~"hi";
> let str2: &str = "hi";
> *str1 == *str2
>
> and it would work fine. That is a nice side-effect I hadn't
> considered.
>
Right. That was the intent. And similarly(?) for comparing against string
literals. (Though I'm not completely clear about when auto-borrowing does
or doesn't happen, and how that would interact with this.)
>
> *An aside:* Note that dereferencing a pointer to an unsized object is
> only allowed in an "lvalue" context (that is, when we are taking its
> address):
>
> let str1: ~str = ~"hi";
> let foo = *str1; // ERROR
> let bar = &*str1; // OK
>
> Here, `foo` is an error because `*str1` is being evaluated in an
> rvalue context, but `bar` is fine, because `*str1` is being evaluted
> in an lvalue context. In the case of `==`, this works out because the
> arguments to overloaded operators are always passed by reference.
>
Yep, that's what I was figuring. I'm not sure what "lvalue context" means
precisely -- it's not actually on the left hand side of anything here, and
the other example below where it *is* on the LHS is illegal -- but the
shape of things matches what I was expecting.
>
> *A further aside:* don't be misled by my use of the term
> "lvalue context" into thinking that a program like this would
> be legal:
>
> let mut str1: ~str = ~"Hello";
> *str1 = "World"; // ERROR
>
> This is illegal because assigning to an lvalue of unsized type is
> illegal, even though `*str1` appears in an lvalue context.
>
Hmm. If I'm thinking right this is because the size of the string is stored
in the pointer, which, if the string gets changed behind its back, would
become invalid?
>
>
>
> regards,
> Niko
>
--
Your ship was destroyed in a monadic eruption.
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev