Uh oh!
There was an error while loading. Please reload this page.
Add syntax to partially destructure self in method signatures. - #83
Add syntax to partially destructure self in method signatures.#83mahkoh wants to merge 1 commit into
self in method signatures.#83Conversation
mahkoh
commented
May 21, 2014
I'm personally more a fan of alternative 1 but there seems to be considerable opposition to making the borrow checker work across methods. This can lead to surprising errors since it is not clear at all which fields I'm allowed to use in a method. In this example, using The following alternative is a variation of the syntax proposed here and closer to alternative 1 but removes the compiler magic. Consider the following struct and functions: structX{a:T,b:U,c:V,}fnf(x:&mutX){g(x,&mut x.a);}fng(x:&mutX,a:&mutT){/* function that doesn't use x.a */}This doesn't work for the obvious reasons. fng(x:&mutX{b,c},a:&mutT){/* function that doesn't use x.a */}This syntax would indicate that Some examples: fnf(x:&mutX){g(x,&mut x.a);}fng(x:&mutX{b,c},a:&mutT){h(x, a,&mut x.b);}fnh(x:&mutX{c}, a:&mut T, b:&mutU){/* ... */}// impl Xfnf(&mutself){self.g(&mutself.a);}fng(&mutself{b,c},a:&mutA){/* ... */}As you can see, fnf(x:&mutX){g(x,&mut x.a);}fng(x:&X{b,mut c},a:&mutA){/* b is immutable */}I think this approach contains all the good parts of the current proposal while also extending it to arbitrary objects (not The only downside is that the syntax can get quite messy. However, consider that without this syntax you have to manually destructure the objects and pass the arguments individually which makes the function signatures and calls even longer. Edit: One could go so far as to add a new keyword to cover the most common cases: fng(x:&mutX{not a}, a:&mutT){/* function that doesn't use x.a */} |
lilyball
commented
May 21, 2014
I feel like this proposal will conflict quite severely with UFCS. Which is to say, |
thestinger
commented
May 21, 2014
Up to this point, patterns have been an internal implementation detail rather than part of the external API. Using this to perform partial borrows creates a backwards compatibility hazard and takes a step away from treating methods as normal functions. |
mahkoh
commented
May 21, 2014
@kballard: @thestinger mentioned on IRC that you were trying to move away from treating methods as a special case but I hadn't heard of UFCS before. In this case you can forget about the original RFC. This leaves us with the "alternative" I proposed in the comment above which works for all function calls and not just methods. @thestinger: Are you against these partial borrows in general or just against the destructuring version I proposed in the original RFC? |
lilyball
commented
May 21, 2014
I think that if you want to propose something like this for arbitrary methods, what you really want to do is come up with some definition for a "struct slice". By that I mean some pseudo-type that means "fields x, y, z from struct Foo". Then you could use this to declare an argument as being of the type of that struct slice. Given that, whenever a struct is partially-moved or partially-borrowed, the compiler could use that to define the maximal valid struct slice (or maximally-borrowable struct slice, for taking Basically, I'm suggesting a way to make the type system aware of what you're trying to do, instead of having it be crazy pattern shenanigans. My proposed syntax for a "struct slice" would be That said, I feel like this is a post-1.0 sort of thing. |
mahkoh
commented
May 21, 2014
The |
lilyball
commented
May 21, 2014
Well, what you're proposing seems to be just special destructuring stuff that the compiler uses to inform borrowck about whether it's safe to pass the value to the function. What I'm suggesting is that it be formalized into the type system properly. Although thinking again on my "struct slice" idea, it doesn't handle the case where a field of a struct is itself only partially-moved. Fixing that issue will probably make this a lot more complicated. |
huonw
commented
May 23, 2014
"struct slicing" seems similar to "datasort refinements": http://smallcultfollowing.com/babysteps/blog/2012/08/24/datasort-refinements/ and rust-lang/rust#1679 |
arcto
commented
May 28, 2014
Would a struct slice be a "derived type" in that its definition is based on another type, limiting access to its members but remembering the size of the original struct? Another view on the semantics of slicing structs could be in terms of set theory. Each struct slice is a subset of accessible members. In some ways this would be similar to traits. |
nikomatsakis
commented
Jun 5, 2014
I think that this more-or-less falls out of the current plans for method dispatch and UFCS. Basically the |
No description provided.