val scala: "Scala" = "Scala"
const typescript: "Typescript" = "Typescript"
"Scala" | "Typescript"
val scala: "Scala" = "Scala"
const typescript: "Typescript" = "Typescript"
"Scala" | "Typescript"
In the example above, the created type "Scala" is distinct from a type Scala, correct?
I did some quick poking around for Scala literal types, but I found stuff like https://github.com/jeremyrsmith/literal-types , which don't look like the example above.
i.e.
val x: "scala" = "scala" // would compile
val y: "scala" = "java" // would produce a compile error
Union types are an orthogonal concept, and work as you'd expect:
val z: String | Int = "foo"
val x: 1 = 1
val y: 2.718281828 = 2.718281828
That's kind of cute. I presume its main purpose is for overrides in order to get something similar to template specialization. C++ templates can be specialized by types and by values, but Scala method overrides are only by type, and literal types allow you to present a value as a type, so if the value can be statically proven to match, then the override is called? interface ProductOffer {
kind: 'productOffer'
eans: Ean[]
discountPercentage: number
}
interface GroupOffer {
kind: 'groupOffer'
groups: GroupId[]
discountPercentage: number
}
type Offer = ProductOffer | GroupOffer
Also check the TypeScript handbook for more information: https://www.typescriptlang.org/docs/handbook/2/narrowing.htm...Union types don't allow to model GADTs, so they are less powerful than what some other languages offer. But on the other side, they allow for much easier composition. E.g.:
type Citrus = "Orange" | "Lemon" type Prunus = "Plum" | "Apricot"
Now in typescript you can just do:
type Fruit = Citrus | Prunus
But in many other languages it's not so easy and you have to jump through hoops and e.g. either redefine the types and/or refactor the code (if you can even do that - if the code is from 3rd party libraries you are out of luck).
Just as an example; the `document.getContext` [0] api takes a string argument to select the context canvas.
Without literal types, you cannot enforce this to a set of valid values. Literal types gives you the power to tell the compiler, which values are valid.
Since there are other programming languages that support literal types, it's not just Scala sophistry. :-P
[0] https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasE...
Something that people used through ugly hacks becoming part of the language natively in a easy to use and understand (and faster to compile) way sounds very much like pragmatism to me.
Here is the source code to Witness (and other singleton stuff in shapeless) https://github.com/milessabin/shapeless/blob/main/core/src/m...
type Verb = 'GET' | 'POST' | 'PUT' | 'DELETE';
export type TerminusType = 'ICAO' | 'IATA' | 'LOCODE' | 'ADDRESS' | 'LATLNG';
It's much better than just assuming it's a string and hoping you don't make a typo.
If the value is supplied from user input then it ensures that you write checks or switch cases.
> type Verb = 'GET' | 'POST' | 'PUT' | 'DELETE';
> export type TerminusType = 'ICAO' | 'IATA' | 'LOCODE' | 'ADDRESS' | 'LATLNG';
> It's much better than just assuming it's a string and hoping you don't make a typo.
> If the value is supplied from user input then it ensures that you write checks or switch cases.
Conceptually, how is this different to an enum?
It also plays nicely with union types, so that you can very quickly define as hoc enums directly in your function declarations, which might be useful if there's only one or two usages of a particular enum, or a section of code where the allowable values very quickly changes.
You basically get a lot of expressiveness without boilerplate, which can be pretty convenient.
So to use the value you have to import the Enum and use that.
const handlers = { '/': { Verbs.GET: () => .... Verbs.POST: () => ...
Whereas:
const handlers = { '/': { GET: () => .... PAWST: //nope! must be one of GET | POST | PUT | DELETE
Basically it's a lot less work and you don't need to import the enum and maintain it.
There's a hack, it looks like:
export const VERBS = ['GET', 'POST', 'PUT', 'DELETE'] as const;
type VerbTuple = typeof VERBS;
export type Verb = VerbTuple[number];
A real enum would be better, but would violate the "typescript is just javascript" rule.Syntax is all there so maybe it'll get better in 3.1+
val result: Int | "INTERNAL_SERVER_ERROR" = 1
val m: Int = result match {
case "INTERNAL_SERVER_ERROR" => throw new Exception("Uh oh")
case t => t
}
It does work with a ClassTag and t: T, but then it thinks the match is not exhaustive. Compare that to Typescript [2].In this specific language feature TS is kinda the gold standard. For example they can automatically narrow the type based on fields existing or not and their type, which can be used to provide a pretty decent implementation of sum types [3]
[1] https://scastie.scala-lang.org/oMdkLBIiRsqUkXv6qbP61g
[2] https://www.typescriptlang.org/play?#code/MYewdgzgLgBATgUwgV...
[3] https://www.typescriptlang.org/docs/handbook/unions-and-inte...