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
Closed

virtual structs #5

wants to merge 2 commits into from

Conversation

nrc
Copy link
Member

@nrc nrc commented Mar 12, 2014

No description provided.

@jdm
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
Copy link

jdm commented Mar 12, 2014

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

@nikomatsakis
Copy link
Contributor

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
Copy link

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
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
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
Copy link
Contributor

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
Copy link
Member

(filed as #14)

@adrientetar
Copy link

@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
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
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
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
Copy link

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

@nrc
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
Clarify taking addresses of globals in globals
@mkaufmann mkaufmann mentioned this pull request Jan 14, 2015
aturon added a commit that referenced this pull request Feb 3, 2015
Updates from implementation and feedback:
brson pushed a commit to brson/rfcs that referenced this pull request Aug 19, 2016
hadronized referenced this pull request in hadronized/rfcs Feb 10, 2017
withoutboats pushed a commit that referenced this pull request Apr 24, 2017
…e-name

Add section pointing out how associated item ambiguity is handled.
Centril added a commit that referenced this pull request Jul 2, 2018
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
epage pushed a commit to epage/rfcs that referenced this pull request Mar 28, 2022
Document feature flags drawbacks
Mark-Simulacrum pushed a commit that referenced this pull request Jun 15, 2023
Fixed broken markdown in Russian translation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants