Skip to content

pseudo-overloading functions with enums #1838

Closed
@erickt

Description

@erickt

I've been working on a couple different serialization libraries, and I often find taking advantage of enum variants living in a different namespace than types. It's really nice to declare:

enum t {
  int(int),
  str(str),
  vec([t]),
}

fun to_str(x: t) {
   alt x {
     int(i) { int::to_str(i) }
     str(s) { s }
     vec(v) { "[" + str::concat(vec::map(v, to_str), ",") + "]" }
   }
}

But calling these functions can get a bit noisy, especially when using arrays:

to_str(vec([int(1), str("foo"), vec([int(2), str("bar")])]))

I'm not sure if we're planning on supporting overloading functions, but what if the type system could automatically box arguments if an enum name is the same as the type? As in allowing us to write:

to_str([1, "foo", [2, "bar"]])

and automatically convert it to the boxed form from above. I'm not sure if this would play well with generics or typeclasses, but it'd really clean up some code I'm writing. Is this possible, or even a good idea?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-type-systemArea: Type systemC-enhancementCategory: An issue proposing an enhancement or a PR with one.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions