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

Named Record Initialisers #704

Closed
DavePearce opened this Issue Jan 19, 2017 · 0 comments

Comments

Projects
None yet
1 participant
@DavePearce
Member

DavePearce commented Jan 19, 2017

To help determine the intended representation for a structural value, it is useful to provide the ability for programmers to give name their record initialisers. Consider this example:

type Point is {int x, int y}

    ...
    Point p = { x:1, y:2}

When compiling this code down for a given platform, the challenge is to determine an appropriate type for the anonymous record value (i.e. { x:1, y:2}). This allows a value of the given type to be constructed immediately, rather than using a generic structural form (which is likely to be less efficient).

In the above example, the issue can be resolved using a back propagation algorithm which takes the type being assigned and propagates it back up the expression tree. Whilst this solution should resolve many cases, it is not a general solution. Consider why:

type Location is {i32 x, i32 y}

    ...
    Point|Location p = { x:1, y:2}

Here, it's not clear which representation to use. The programmer can resolve this by rewriting the above as follows:

type Location is {i32 x, i32 y}

    ...
    Location tmp = { x:1, y:2}
    Point|Location p = tmp

At this point, it is immediately clear. To avoid this, a neat solution is to permit named record initialisers, as follows:

type Location is {i32 x, i32 y}

    ...
    Point|Location p = Location{ x:1, y:2}

Here, the type name comes before the curly braces to signal the intended return type. This should not be an effective record type.

@DavePearce DavePearce added this to the ChangeList Post v0.4.0 milestone Jan 19, 2017

DavePearce added a commit that referenced this issue Feb 23, 2017

Updated front-end for Named Records #704
This updates the front-end so that it now passes named records, and
stores the name internally within the AST.  In addition, I've added a
few test cases to go with it.

At this stage, the naming information does not make it's way to WyIL.
It remains a little unclear how this is going to work.

@DavePearce DavePearce closed this Feb 23, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment