3 ms·
Do you know of any programming language where this type of things (having functions defined only for certain enum values, or anything equivalent) would be doabl
by bsaul 8y ago
Do you know of any programming language where this type of things (having functions defined only for certain enum values, or anything equivalent) would be doable properly, without hacking the typesystem ?
- chrismorgan 8y agoLook for languages with dependent typing. Idris and Liquid Haskell are the two that spring to my mind. Other than that, TypeScript has decent support for the concept, though with standalone functions rather than methods: you’d define your “variants” as types (or interfaces), define the “enum” as the union of those types (e.g. `Foo | Bar`), and then have your functions take either the entire “enum” type, or specific “variant” types. In place of Rust’s `match`, you will then use type guards to narrow the “enum” type down to one (or more, if you like) of the “variant” types. See https://www.typescriptlang.org/docs/handbook/advanced-types.html#type-guards-and-differentiating-types https://www.typescriptlang.org/docs/handbook/advanced-types.... for a bit of info.
- namibj 8y agoJula allows it, initially only defining the integer division operator for non-zero divisor. That default changed due to a (at that point) lack of syntactic sugar and type-inference to not make it a massive hassle. Edit: It's called a partial function, and I confused Julia with Pony. The former might be able to express it, the latter can handle it in some way, but I'm not familiar with either type system.