-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Wouldn't it be nice if anonymous functions inside type def are considered inner constructor? #8502
Comments
That's actually a rather nice idea. |
+1, I never liked (since c++) this redundance, which is error prone (particularly at the REPL when types are renamed). I wouldn't mind a more explicit name though, like |
The real advantage is that it avoids the fact that for parametric types defining methods for the type name inside the type block means something different than it does outside. This could make it less confusing. |
Why can't MyType(x::Int) = new(MyType, x) Wouldn't that remove the need for inner constructors? I guess losing the inner / outer constructor concept for types would lead to some increased verbosity (as you would have to define all constructors) but it seems conceptually simpler and would lead to more explicit code. |
Also not a bad idea. |
One big advantage of scoping |
It will also conflict with the current approach with two default constructors (one converting) when no inner constructor is defined. |
That is an advantage (currently) but I feel that the concept of restricting extensibility is going to come up in the future for generic methods as well. This way you could unify the concepts of extensibility between type constructors and methods as the two would be exactly the same. Right now type construction is special cased, it is really the only place in Julia that is not "open by default". If I want to extend or modify how a type (with an inner constructor) is constructed I have to go in and edit the source code. @ivarne I addressed that point, although I feel the only times you should use inner constructors is when you want to opt out of the default behavior. |
I personally like that there is at least one thing in the language that |
Another reason to keep |
Going back to the issue topic, using this syntax for constructors is a nice idea, I agree. The biggest problem at this point is probably that allowing both is kind of confusing. |
We could phase out the current way and make this the new hotness. |
In favor of the current approach, I would say
|
Oh and also you can't use |
I'm not really in favor of changing things given the other points, but I think the similarity between inner and outer constructors may actually be a point of confusion for parametrized types. |
See #8135 for some thoughts on changes to inner constructors. I hope there is something we can do to improve the situation. |
Would this kind of change still easily allow the use of secondary inner constructors? (i.e. an inner constructor that makes use of the primary inner constructor, but still needs access to the type block, such that it couldn't simply be replaced with an outer constructor) i.e. can this:
be replaced to this:
To me these are very different. Not least because this makes SecondaryConstructor a closure which carries PrimaryConstructor along with it as a callable field (right?) |
What I have in mind was:
I already declare the type name above, why do I have to repeat myself inside?
The text was updated successfully, but these errors were encountered: