Skip to content
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

virtual structs #5

Closed
wants to merge 2 commits into from

Conversation

Projects
None yet
8 participants
@nrc
Copy link
Member

nrc commented Mar 12, 2014

No description provided.

@jdm

This comment has been minimized.

Copy link

jdm commented Mar 12, 2014

Will there be some way for an override method to call the parent type's override implementation?

@jdm

This comment has been minimized.

Copy link

jdm commented Mar 12, 2014

I would also really like to see casting elaborated upon, with examples, etc.

@nikomatsakis

This comment has been minimized.

Copy link
Contributor

nikomatsakis commented Mar 13, 2014

I've been thinking more about this. I think that we ought to "close" subtyping so that you can only subtype a virtual struct within the same module (or some submodule).

My reasoning:

  1. Prevents people from abusing virtual structs to create an open-ended abstraction: traits are more suitable in such cases.
  2. Downcasting is more optimizable, becomes O(1) instead of O(n). This is a common complaint against C++ RTTI (as pointed out on the mailing list).
  3. Addresses the private fields initialization gotcha.

Basically, virtual structs become an alternative to enums. To some extent this is the expression problem -- enums are there to provide one half, virtual structs with virtual fns to provide the other half, and traits to solve extensibility.

(There is some sort of precedent, as an aside, with scala's sealed keyword)

@bill-myers

This comment has been minimized.

Copy link

bill-myers commented Mar 16, 2014

Closed subtyping seems an extremely interesting idea, because then one could see this not as competition for traits, but rather a generalization of enums.

The current "enum" syntax could be recast as syntax sugar for virtual structs.

This would give "datasort refinements" and "GADTs"/"type parametrization for variants" for free to the enum side, and would give the ability to represent the type tag in ways different than the a vtable pointer (byte-sized tag, null pointer optimization, etc.) to the virtual class side.

I still wonder whether the methods would be better put in named traits, with support for declaring a "vtable trait" on the structs/enums/enums variants.

Maybe there could be some syntax sugar there too, by having a "virtual impl" declarations that would be equivalent to declaring and implementing a trait, and declaring it as a vtable trait, just like the current "impl" behaves like syntax sugar for declaring and implementing a trait.

BTW, I'd suggest then to make the structs either abstract or final, so that it works like the current enum vs enum variant, and doesn't have the issue of having a single identifier that names both a concrete virtual struct, and the set of virtual structs inheriting it.

@nrc

This comment has been minimized.

Copy link
Member Author

nrc commented Mar 18, 2014

Incorporated comments from this issue. (Do comments on earlier commits just disappear? I guess I probably shouldn't have force pushed)

@huonw

This comment has been minimized.

Copy link
Member

huonw commented Mar 18, 2014

If you still have the commit hashes (I guess you do, via git reflog) you can view them at https://github.com/nick29581/rfcs/commit/<commithash> (at least until the commits and/or comments get GC'd by github).

@nikomatsakis

This comment has been minimized.

Copy link
Contributor

nikomatsakis commented Mar 18, 2014

On Mon, Mar 17, 2014 at 11:32:29PM -0700, Nick Cameron wrote:

Incorporated comments from this issue. (Do comments on earlier commits just disappear? I guess I probably shouldn't have force pushed)

Usually github keeps and lists commented, citing "an outdated version
of this diff". Maybe for RFCs, though, we should avoid force pushing
nonetheless, since rebasing should generally be unnecessary.

@pnkfelix

This comment has been minimized.

Copy link
Member

pnkfelix commented Mar 18, 2014

(filed as #14)

@adrientetar

This comment has been minimized.

Copy link

adrientetar commented Mar 18, 2014

@nikomatsakis The behavior you described only occurs when people comment of the diff tab and not on the commit itself.

nrc added a commit to nrc/rfcs that referenced this pull request Mar 30, 2014

Unify and nest structs and enums
Another alternative to RFC rust-lang#5 and an extension/variant of RFC rust-lang#11.

Unify enums and structs by allowing enums to have fields, and structs to have
variants. Allow nested enums/structs. Virtual dispatch of methods on struct/enum
pointers. Remove struct variants. Treat enum variants as first class. Possibly
remove nullary structs and tuple structs.

nrc added a commit to nrc/rfcs that referenced this pull request Mar 30, 2014

Unify and nest structs and enums
Another alternative to RFC rust-lang#5 and an extension/variant of RFC rust-lang#11.

Unify enums and structs by allowing enums to have fields, and structs to have
variants. Allow nested enums/structs. Virtual dispatch of methods on struct/enum
pointers. Remove struct variants. Treat enum variants as first class. Possibly
remove nullary structs and tuple structs.
@ghost

This comment has been minimized.

Copy link

ghost commented Jun 28, 2014

Please do not use the word "virtual", nor as a keyword, nor as a name for a feature. Even though i am used primarily to C++, i can not get used to this word. For me it means something that i can see, but is not actually there. Like in "virtual reality" or "virtual memory". The meaning of this word has nothing in common with what you plan to use it for. In source code i think it is the comment which is virtual - it looks to be there, but is actually not real source. Please choose some other words. Perhaps "dynamically dispatchable", ... . You know it better. Just not "virtual" please.

@sinistersnare

This comment has been minimized.

Copy link

sinistersnare commented Jun 28, 2014

@almale virtual is a well known word in the OO space, and going away from it would cause more confusion to the community,

@nrc nrc self-assigned this Sep 4, 2014

@alexcrichton alexcrichton force-pushed the rust-lang:master branch from 6357402 to e0acdf4 Sep 11, 2014

@aturon aturon force-pushed the rust-lang:master branch from 4c0bebf to b1d1bfd Sep 16, 2014

@nrc

This comment has been minimized.

Copy link
Member Author

nrc commented Sep 23, 2014

Closing in favour of other RFCs which address the same problem (see http://discuss.rust-lang.org/t/summary-of-efficient-inheritance-rfcs/494).

@nrc nrc closed this Sep 23, 2014

nikomatsakis added a commit that referenced this pull request Oct 2, 2014

Merge pull request #5 from alexcrichton/const-vs-static
Clarify taking addresses of globals in globals

@mkaufmann mkaufmann referenced this pull request Jan 14, 2015

Merged

Integer overflow #560

aturon added a commit that referenced this pull request Feb 3, 2015

Merge pull request #5 from alexcrichton/pr
Updates from implementation and feedback:

@mahkoh mahkoh referenced this pull request Feb 11, 2015

Merged

Add back `Vec::from_elem` #832

brson pushed a commit to brson/rfcs that referenced this pull request Aug 19, 2016

phaazon referenced this pull request in phaazon/rfcs Feb 10, 2017

withoutboats pushed a commit that referenced this pull request Apr 24, 2017

Merge pull request #5 from pnkfelix/mult-unbound-assoc-types-with-sam…
…e-name

Add section pointing out how associated item ambiguity is handled.

Centril added a commit that referenced this pull request Jul 2, 2018

Merge pull request #5 from Centril/patch-1/tuple-struct-self-ctor
Fix RFC 2302 to support unit structs, fn pointers, and use value namespace instead for exprs

steveklabnik added a commit that referenced this pull request Mar 30, 2019

Merge pull request #5 from Manishearth/rustdoc-roadmap
Add link to rustdoc roadmap
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.