You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It is easy to construct an instance of a single record, however with multiple records it gets very repetetive and adds no value to readability by actually decreasing it.
For readability and convenience it would be nice to have a default constructor for any record.
Current solution
For a record defined as:
record Bridge {
connectionCount : Number,
orientation : Orientation,
idx1 : Number,
idx2 : Number
}
I would like to construct a list of record instances:
I'm not against having constructors per say, I just like defining things explicitly, however I see your point about records in arrays, is there anything besides that which would be easier with constructors?
Maybe we could figure out something else which keeps the explicitness a record (naming wise) but would make it easier to construct an array of records?
Namek
changed the title
Feature Request: default constructor for record
Feature Request: default constructor for records
Feb 17, 2020
is there anything besides that which would be easier with constructors?
Notice the inputPos:
fun onPointerMove (evt : Html.Event) : Promise(Never, Void) {
try {
inputPos =
{
x = evt.offsetX,
y = evt.offsetY
}
next { }
}
}
The type of inputPos is not explicitly defined. In this case I could do Vec2(evt.offsetX, evt.offsetY). (where Vec stands for Vector)
Moreover, in future we might want to have similar types with different names treated separately, e.g.:
record Pos { x : Number, y : Number }
record Speed { x : Number, y : Number }
record Vec2 { x : Number, y : Number }
This is where the additional explicit as keyword could be useful (similar to Crystal) because if I had Speed which I would want to convert to Vec2 I would do as Vec2. Otherwise, I wouldn't want to mix those things implicitly.
And I believe we want this separation because of packages having similar structures but different names and we don't want collisions.
It is easy to construct an instance of a single record, however with multiple records it gets very repetetive and adds no value to readability by actually decreasing it.
For readability and convenience it would be nice to have a default constructor for any record.
Current solution
For a record defined as:
I would like to construct a list of record instances:
However, I have to manually write construction functions like this:
Proposal for the Compiler
I propose to simply make a function for a record. For the case above:
Of course constructors won't be used for every created Record so it would be consistent to analyze usage for dead code elemination.
The text was updated successfully, but these errors were encountered: