Skip to content
This repository has been archived by the owner on Sep 7, 2023. It is now read-only.

Add support for enumerated values #3

Closed
jeromesimeon opened this issue Feb 7, 2018 · 4 comments
Closed

Add support for enumerated values #3

jeromesimeon opened this issue Feb 7, 2018 · 4 comments
Labels

Comments

@jeromesimeon
Copy link
Member

jeromesimeon commented Feb 7, 2018

Ergo should allow for creations of enum values. This relies on the ability to read/import CTO files.

@jeromesimeon jeromesimeon added the Type: Feature Request 🛍️ New feature or request label Mar 9, 2018
@jeromesimeon
Copy link
Member Author

jeromesimeon commented Mar 13, 2018

This requires #9 #10

@jeromesimeon jeromesimeon added this to the Type checking support milestone May 6, 2018
@jeromesimeon
Copy link
Member Author

Some notes about how enum types and values can be encoded into Ergo's internal notion of types and values.

Enum types are encoded as branded sum types:

enum Gender {
  o MALE
  o FEMALE
  o OTHER
  o UNKNOWN
}
==
Brand Gender {
  Either String
    (Either String
      (Either String
        String))

This encoding should provide for accurate type checking. There will be challenges in providing good error reporting for those. Most likely the compilation context should maintain a table of declared enum types.

Enum values are encoded as branded sum values:

MALE    == dbrand Gender (dleft "MALE")
FEMALE  == dbrand Gender (dright (dleft "FEMALE"))
OTHER   == dbrand Gender (dright (dright (dleft "OTHER")))
UNKNOWN == dbrand Gender (dright (dright (dright "UNKNOWN")))

From value to JSON:

MALE
==
{ "$class": "Gender",
  "$kind": "enum",
  { "left": "MALE" } }
FEMALE
==
{ "$class": "Gender",
  "$kind": "enum",
  { "right": { "left" : "FEMALE" } } }
OTHER
==
{ "$class": "Gender",
  "$kind": "enum",
  { "right": { "right" : { "left" : "OTHER" } } } }
UNKOWN
==
{ "$class": "Gender",
  "$kind": "enum",
  { "right": { "right" : { "right" : "UNKNOWN" } } } }

toString(data) in REPL

dbrand Gender (dright (dleft "FEMALE"))
-->
FEMALE

toString(data) in JavaScript runtime

{ "$class": "Gender",
  "$kind": "enum",
  { "right": { "left" : "FEMALE" } } }
-->
"FEMALE"

@jeromesimeon
Copy link
Member Author

Some notes about internal enum encoding, for historical preservation.

Some notes about how enum types and values can be encoded into Ergo's internal notion of types and values.

Enum types are encoded as branded sum types:

enum Gender {
  o MALE
  o FEMALE
  o OTHER
  o UNKNOWN
}
==
Brand Gender {
  Either String
    (Either String
      (Either String
        String))

This encoding should provide for accurate type checking. There will be challenges in providing good error reporting for those. Most likely the compilation context should maintain a table of declared enum types.

Enum values are encoded as branded sum values:

MALE    == dbrand Gender (dleft "MALE")
FEMALE  == dbrand Gender (dright (dleft "FEMALE"))
OTHER   == dbrand Gender (dright (dright (dleft "OTHER")))
UNKNOWN == dbrand Gender (dright (dright (dright "UNKNOWN")))

From value to JSON:

MALE
==
{ "$class": "Gender",
  "$kind": "enum",
  { "left": "MALE" } }
FEMALE
==
{ "$class": "Gender",
  "$kind": "enum",
  { "right": { "left" : "FEMALE" } } }
OTHER
==
{ "$class": "Gender",
  "$kind": "enum",
  { "right": { "right" : { "left" : "OTHER" } } } }
UNKOWN
==
{ "$class": "Gender",
  "$kind": "enum",
  { "right": { "right" : { "right" : "UNKNOWN" } } } }

toString(data) in REPL

dbrand Gender (dright (dleft "FEMALE"))
-->
FEMALE

toString(data) in JavaScript runtime

{ "$class": "Gender",
  "$kind": "enum",
  { "right": { "left" : "FEMALE" } } }
-->
"FEMALE"

@jeromesimeon
Copy link
Member Author

Based on that encoding, with a lot of additional work (notably, enum values are translated as constants), there is a full implementation in https://github.com/accordproject/ergo/tree/js-feature-dynamic-templates

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

1 participant