-
-
Notifications
You must be signed in to change notification settings - Fork 658
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
Property specifier which allows writing only from constructor #3442
Comments
I don't think that |
That doesn't make any sense. It would introduce e.g. |
ah, you mean like |
I think he meant something like |
Yeah I misunderstood the meaning of |
I don't understand the example : if we have a call to leave, we cannot inline the constructor unless leave() is inlined as well, since we need to pass it a MyVector object. The thing we might want instead is this:
In a struct classes, we enforce that member variables can only be written in constructor, and we always substitute the value by the list of member vars, which means that Another thing:
Turns into:
The only place when MyPoint is used as an object is when unified with a type parameter (Array of MyPoint for instance), of with a structure such as |
PS : this is what C++ does when you don't use pointers |
PPS : struct classes cannot be extended (and cannot extend a non-struct class ?) |
I don't want to inline the constructor, I want to use the field values for constant propagation. This is quite easy to implement for immutable fields because we do not have to worry about objects leaving context, or their aliases leaving context. |
Here's a small demo commit: Simn@e539939 This is all that's needed to allow this optimization: https://gist.github.com/Simn/484866c918d6abcd5f9e Obviously this optimization it not safe for arrays and structures because the fields could be written to, it's just to get the idea across and because structure and array is easier to implement than constructor (which has to look at the field code). So what I really want here is two-fold:
|
I think we could simply allow write access from a constructor to a |
We already allowed it for structure types.
|
I like |
@Atry abstracts allow much more, such as auto cast and operator overloading. You could still have abstracts using struct class implementation for multi-field support |
The |
@Simn +1 |
Cross-post from the google group: Regarding struct classes, I would love the following features:
|
Hi Dan |
I think implementing an explicit type
Will compile to:
|
Note:
Becomes
When invoking a |
We have |
This was brought up and rejected before, but this time I have a better understanding of the benefits and would like to present it again. The idea is to have a property specifier such as
new
which allows writing a field from a class constructor but nowhere else.What's nice about this is that we can infer field immutability from it, which allows us to e.g. propagate constants even if an object leaves context. Consider this piece of code:
The call to
leave(vec)
cancels constructor inlining. Now if bothx
andy
were defined to be writable-only-from-constructor, we could still use their values and optimize theuse(vec.x, vec.y)
call to plain25
.The text was updated successfully, but these errors were encountered: