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
DIP 1004: Inherited Constructors #42
DIP 1004: Inherited Constructors #42
Conversation
Thanks! I will be unavailable for two weeks starting tomorrow but will check it right after. |
You only mention |
I think both could be supported. At some point there was talk about allowing Alternatively if there really is a visibility issue we could do something like: |
I think technically we have access to the |
IIRC, it was proposed that |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good overall, just need to polish the details
The process of writing forwarding constructors is tedious and may even be | ||
error-prone. It's possible to use existing language features such as | ||
template mixins to generate forwarding constructors, however this increases | ||
compilation times and also generates more object code. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
afair the main issue wasn't compilation time but the fact it creates reference loop for semantic pass (can't reliably reflect on an aggregate while trying to mix in is method)
|
||
The rationale for this current limitation: If the `Derived` object was constructible | ||
by calling only the `Base` class constructor then the `Derived` object itself | ||
could be left in an *unexpected* state. This is because the `Derived` constructors could |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd suggest also mentioning also the code example when derived class has no own constructors (thus us rationale is not applicable) but it still doesn't work. Probably separate this section in two blocks as per https://github.com/dlang/DIPs/pull/42/files#diff-3830fabd6166499fcffc5e7cb94d0c69R32
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ping?
} | ||
|
||
/// inherit base class constructors | ||
alias super.this this; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if alias super this
makes more sense - there is no way to refer to constructor method by this
name in language and at the same time semantically by forwarding constructors you are saying derived is a strict superset of base.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's sort of odd to use alias super this;
because it looks like subtyping, but on the other hand the derived object is a base class object already (implicitly converts to base).
I think I'll simply list a couple of proposed syntaxes and then let the community decide on the best choice.
|
||
## Breaking changes | ||
|
||
There are no expected breaking changes with this proposal. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not true, same as for adding any new feature there is a risk that some code that didn't compile in is(typeof())
will start doing so :) It is very unlikely to make difference for existing code bases but DIPs need to mention all possible risks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah true. :)
|
||
The feature syntax shares a similarity with object subtyping, | ||
however `alias this.super this` would not conflict with the subtyping feature | ||
since `super` is a reserved word. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you checked if existing grammar supports it already (I suppose alias this
grammar is generic but need to make sure)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm I haven't, I did assume the compiler would currently reject alias this.super this
.
Just bringing in previous discussions about this. |
You mean the disabling specific base class ctors? Yeah I must have skipped that part. I'll amend the DIP soon. |
Ping @AndrejMitrovic ;) |
edba45e
to
4ec6c61
Compare
Ok updated with the proposed changes. I think with regards to syntax proposals this can still be discussed in the community. I'm sure we can come up with a reasonable syntax most people are happy with. But the basic idea for the feature is there. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! I like the content, now it is only matter of formalizing and improving structure. I made some inline comments but checking existing state against https://github.com/dlang/DIPs/blob/master/README.md#advices-for-writing-great-dips can be a good idea too.
You may also want to re-generate table of contents now that more sections have been added.
|
||
The rationale for this current limitation: If the `Derived` object was constructible | ||
by calling only the `Base` class constructor then the `Derived` object itself | ||
could be left in an *unexpected* state. This is because the `Derived` constructors could |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ping?
new Derived(1, 2); // error: constructor is disabled | ||
} | ||
``` | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on Andrei latest comment for DIP1002, would be a good idea to add here a section showing existing constructor boilerplate in public projects and how it would be simplified.
75674cf
to
e8b7b70
Compare
This was added at the start of the examples in the previous push.
Good idea. I've added examples from an open source project, from Phobos, and I've also included the forwarding constructors mixin template as an example of what's already possible in library code (but which has its shortcomings). |
e8b7b70
to
1ce020a
Compare
Changed title in PR / code and commit message to |
Changes reading flow to first explain the problem and show existing solutions first and only to proceed with proposed changes after.
- Proposed changes are phrased as directives for compiler - Proposed changes are explained with straighforward before/after samples - @disable of constructors is split onto own section
Shows how common it is to define wrong exception class constructors with existing language semantics.
I have created a pull request against tour branch with various proposed changes : AndrejMitrovic#1 |
Proposed editorial changes
@Dicebot let me know if you need this squashed (perhaps 1 origin commit and 1 fixup commit by you), or we could just keep the history as is. |
Don't bother, it will be all squashed into one commit on merge anyway. I'll wait for Andrei to prepare his final judgement of existing DIPs in the queue and merge this one if it doesn't fail any of possibly mentioned requirements. |
Andrei's judgement of last merged DIPs: https://github.com/dlang/DIPs/blob/master/DIPs/DIP1001.md#review |
Yup, saw it. I'm not planning on throwing a tantrum if the DIP is rejected. :) |
{ | ||
// Error: class Derived cannot implicitly generate a default ctor when | ||
// base class Base is missing a default ctor | ||
auto d = new Derived(10, 10); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(10,10)
: Is there some code missing elsewhere, or should this be ()
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this is the wrong snippet and error message, fix: AndrejMitrovic#2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
DIP1004: Fix snippet for shadowing base default ctor
Ok 2 quick things. 1/ I love the general idea. |
We could certainly try. The question is if it will end up being a half-solution like the current Typedef situation. |
I would consider that having a half working solution here to be a problem with out mixin mechanism, and that we should fix that rather than baking a workaround for a specific use case in the language itself. |
My 5 cents. There are several proposed changes: This can't truly be replaced by any library solution because they all have a problem with one of stated goals - make correct approach the default by making
Those certainly can be done a library utility (I remember having issues with semantic analysis cycles in the past but that was definitely a compiler bug). One can argue though that those are not new features and already work for any other class method - omitting constructors makes an artificial exception. I am less opinionated on this part though. Anyway, before it gets to review stage, we should collect all proposed library implementations from NG and evaluate them as part of the DIP. |
@Dicebot It was never question of making 1 as a library solution, so I'm not sure where you are going with that. For 2, it is about copying a set a function from place to another. If we can't use reflection and mixin to do this, I'd argue this is the problem that need to be solved. |
For exceptions, we do now have http://dlang.org/phobos/std_exception.html#.basicExceptionCtors, which at least makes mixing in the constructors easy, though it's not automatic. You have to explicitly mix them in. But thanks to improvements in the compiler, it actually works with ddoc, which wasn't the case in the past when such solutions were brought up. |
@andrej-mitrovic-sociomantic Please email me. Thanks! |
Related issue: https://issues.dlang.org/show_bug.cgi?id=9066