#Are variants of enums not types themselves

1 messages · Page 1 of 1 (latest)

mystic kiln
#

This seems like this should work, but let me give an example

I'm building a little constraint solver library, and I defined multiple constraint types, Unary, Binary, and Nary for how many variables it operates on.

pub type Constraint(a) {
  Unary(fn(a) -> Bool)
  Binary(fn(a, a) -> Bool)
  Nary(fn(List(a)) -> Bool)
}

// the commented signature feels like it should be possible
// the function only ever returns a Unary constraint
// Why would you ever have to pattern match on the return since you know which variant it is?
//  fn is_even() -> Constraint(Int).Unary {
pub fn is_even() -> Constraint(Int) {
  Unary(fn(x: Int) -> Bool { x % 2 == 0 })
}

This seems like it would be an expressive construct, but I bet there are complications that make it weird or not possible that I'm not thinking of. Or maybe there is a more accurate way to express what I want

proper hare
#

Variants are not types, no. Gleam doesn’t have subtyping

mystic kiln
#

😦

deft pond
proper hare
#

its nothing to figure out its a design decision!

deft pond
fringe thistle
#

Subtyping bad

civic pine
fringe thistle
#

They dramatically increase the complexity of the type system in a way that makes the language much harder to use and understand, they hinder inference, they make error messages much worse, and they don't enable anything you can't do without them.