4 ms·
> Haskell That can be done in Haskell with algebraic data types (with constructors without arguments), no? Languages that have enums: Java, Kotlin, Dart, Ada,
by tralarpa 4y ago
> Haskell
That can be done in Haskell with algebraic data types (with constructors without arguments), no?
Languages that have enums: Java, Kotlin, Dart, Ada, C (not in the first version), Pascal (not in the first version), Modula-2, Visual Basic,...
Pascal had subrange types since the first specification. For example "var month : 1..12;"
I don't know anything about Typescript but from what I have found it seems that Typescript's literal types are similar to Pascal's subrange types, i.e., the new type is compatible to the basic type it is derived from (unlike enums in the above languages, which are completely new types). But there is no runtime check.
- pjmlp 4y agoI am quite sure that Pascal had enumerations since the early days. Section "6.1.1 Scalar Types", 1973 edition: https://www.standardpascal.org/The_Programming_Language_Pascal_1973.pdf https://www.standardpascal.org/The_Programming_Language_Pasc...
- tralarpa 4y agoMy bad. In the newer edition they called it "enumerated types": http://pascal.hansotten.com/uploads/books/Pascal_User_Manual_and_Report_Fourth_Edition.pdf http://pascal.hansotten.com/uploads/books/Pascal_User_Manual... I didn't realize that "scalar types" in the first edition were the same thing.
- afiori 4y agoTypescripts types are more similar to OCaml's variant types[0]; sadly my knowledge of OCaml's type system is less than a tenth of what I would like so I cannot give precise parallels, but in typescript you can have type A = 0 | null | Array<string>, type B = Array<number> | 0 | Array<string> and construct the type A & B = 0 | Array<string> In general I think that it is not possible to "just" add an type intersection operator to an Hindley–Milner type system without making it either incomplete[1] or unsound[2] In typescript this is often used with records as {a:number} & {b:string} = {a:number, b:string} [0] https://v2.ocaml.org/releases/5.0/htmlman/types.html#sss:typexpr-obj https://v2.ocaml.org/releases/5.0/htmlman/types.html#sss:typ... [1][2] which typescript is EDIT: fix typo
- valenterry 4y agoIt's not quite the same. You can emulate literal string types using recursive ADTs (similar lists), storing one character at the time at the typesystem. But the ergonomics are quite different. For example, in Scala you can do this: val foo: "Must be this string" = "foo" // fails to compile In Typescript it's similar. But how would "Must be this string" look in the case of a haskell ADT?