New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Mutability polymorphism" #414

Open
glaebhoerl opened this Issue Oct 25, 2014 · 7 comments

Comments

Projects
None yet
8 participants
@glaebhoerl
Contributor

glaebhoerl commented Oct 25, 2014

It would be desirable to, at some point, grow the ability to abstract over the "mutability" of reference types. Currently, it is frequently the case that one must write separate "getter" methods for shared & and unique &mut references, even though the bodies of these methods are exactly the same. This is especially bothersome when those method bodies are nontrivial.

See an earlier discussion on reddit here.

Ideas for accomplishing this could potentially be borrowed from the research mentioned in this presentation, as well as from the Disciple language (a language with region types, effect types (e.g. mutation involving a region), and polymorphism over both).

This could also potentially allow the Fn/FnMut/FnOnce, Deref/DerefMut, Index/IndexMut, and so forth hierarchies to be collapsed into single traits parameterized over capabilities / effects / constraints (whatever we choose to call them).

@blaenk

This comment has been minimized.

Contributor

blaenk commented Oct 26, 2014

Yeah, this would be very useful I think.

@thehydroimpulse

This comment has been minimized.

thehydroimpulse commented Oct 26, 2014

/cc me

@reem

This comment has been minimized.

reem commented Oct 26, 2014

It would be even better if we could abstract over ownership status, i.e. we could collapse Trait[Mut|Move]? into just Trait.

@brendanzab

This comment has been minimized.

Member

brendanzab commented Oct 26, 2014

This would be great. Do you have any thoughts on how this could be added in a backwards compatible way in the future?

@reem

This comment has been minimized.

reem commented Oct 26, 2014

Not really. I think this deserves consideration pre-1.0 because it would collapse and change traits which are lang items, such as the Deref, Fn and Index families. Code that implemented those traits would be broken.

Parametrizing over mutability is much easier than parametrizing over ownership, since you can just say "if you have &m? T you can only treat it like &T", but the story with owned? T becomes trickier.*

* note all syntax is super hypothetical

@glaebhoerl

This comment has been minimized.

Contributor

glaebhoerl commented Oct 26, 2014

Yeah, figuring out how to abstract over out and move is much less obvious than mut and shared. Possibly we would have to take a capability-based perspective on it (see that presentation, particularly the part about strong updates), i.e. currently Rust's references are pointers bundled together with the capabilities to access them; maybe taking them separately and, possibly, having traits on capabilities or something might lead us in the right direction... but this is extremely hazy and hand-wavy for now.

@Gankro

This comment has been minimized.

Contributor

Gankro commented Oct 27, 2014

@reem I don't think it's as easy as that. &T is often less restrictive, in that much more general code structures are valid with & and than &mut. You can make multiple &'s, and &mut's often necessitate temporary wrangling to appease borrowck (although this may simply be a SESE artifact).

You would have to assume that your ptrs are &mut's in some regards, and &'s in others. Of course, in a hypothetical polymorphic mutability world many of the restrictions that & provides would be swept away by virtue of presumably all getters being polymorphic and "just working".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment