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

Feature Request: default constructor for records #191

Closed
Namek opened this issue Feb 16, 2020 · 3 comments
Closed

Feature Request: default constructor for records #191

Namek opened this issue Feb 16, 2020 · 3 comments
Labels
Milestone

Comments

@Namek
Copy link
Contributor

Namek commented Feb 16, 2020

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:

bridges =
  [
    brg(2, Orientation::Horizontal, 0, 3),
    brg(1, Orientation::Vertical, 0, 16),
    brg(1, Orientation::Vertical, 3, 11),
    brg(1, Orientation::Horizontal, 9, 11),
    brg(1, Orientation::Horizontal, 11, 19),
    brg(1, Orientation::Horizontal, 16, 19)
  ]

However, I have to manually write construction functions like this:

  fun brg (
    connectionCount : Number,
    orientation : Orientation,
    idx1 : Number,
    idx2 : Number
  ) : Bridge {
    {
      connectionCount = connectionCount,
      orientation = orientation,
      idx1 = idx1,
      idx2 = idx2
    }
  }

Proposal for the Compiler

I propose to simply make a function for a record. For the case above:

Bridge(1, Orientation::Horizontal, 16, 19)

Of course constructors won't be used for every created Record so it would be consistent to analyze usage for dead code elemination.

@gdotdesign
Copy link
Member

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 Namek changed the title Feature Request: default constructor for record Feature Request: default constructor for records Feb 17, 2020
@Namek
Copy link
Contributor Author

Namek commented 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.

@gdotdesign gdotdesign added this to the 0.9.0 milestone Mar 27, 2020
@Namek
Copy link
Contributor Author

Namek commented Apr 3, 2020

I've seen the constructors are already implemented, I'm glad it works.

Thank you.

@Namek Namek closed this as completed Apr 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants