asoffer / Icarus

An experimental general-purpose programming language
Apache License 2.0
9 stars 2 forks source link

Enums and Flags should be printable via io.Print() #86

Open perimosocordiae opened 2 years ago

perimosocordiae commented 2 years ago

I'm not sure if this needs to be built-in somehow, or if this is implementable in the io.ic module, but we need support for it.

asoffer commented 2 years ago

I think we'd need a builtin function accepting any enum type and returning a []char.

Flags are harder and I'm not sure what we want to do here.

I'm hesitant about this from a Hyrum's law perspective. It's definitely useful. Perhaps the right solution is the we seem to be defaulting to in these cases: Make it available by default within the same module defining the type, and allow the builtin to be explicitly exported.

asoffer commented 2 years ago

One thing we could provide is a building-block for this: A scope that iterates through all defined values in an enum/flags definition (in an unspecified order).

This has well-defined semantics without making any significant decisions on behalf of the user. In particular, one of the difficulties for printing flags is how to print it when the flag has multiple components set. Do we separate with commas, or pipes? Are there spaces between each? Can this function allocate?

Here's an example of what could be done with this proposal:

Color ::= flags { RED \\ GREEN \\ BLUE }

MyPrint ::= (c: Color) -> () {
  separator := ""
  component_string (c) do [component: []char] {
    io.Print(core.exchange(separator, " | "), component)
  }
}