Enums and scalars#
Meta: split this 50/50 — both feel obvious on the page but trip people up in real code (enum literal vs. string, scalar coercion timing).
The examples on this page are live: they share one Dang environment, so later snippets use earlier definitions. Each result is computed and baked in by the docs build — edit a snippet and hit Run ▶ to replay the page in your browser. Blocks that show an error are supposed to fail: the build verifies the failure the same way it verifies the results.
Enums#
Declaration#
An enum lists its members as bare CAPS names:
enum Color { RED GREEN BLUE }
Members are separated by spaces or newlines, not commas:
enum Size { SMALL, LARGE }
Values#
A value is written with its enum's name, as Color.RED. An enum value is a field on the enum (see Modules and imports), not a top-level name, so a bare RED isn't in scope, even inside case. (Enums imported from a schema are the exception, below.)
RED
Color.values lists every member, as a real [Color!]!:
Color.values
Two values of the same enum are equal when they're the same member. Values of different enums can't be compared, even when the member names match:
enum Signal { RED AMBER GREEN }
Color.RED == Color.RED
Color.RED == Signal.RED
A string literal compared with an enum value converts to it, as it does in an argument, so it must name a member. Color.RED is still the clearer spelling:
let shade = Color.BLUE
[shade == "BLUE", shade != "RED"]
A String held in a variable doesn't convert; cast it with :: (below):
let picked = "BLUE"
shade == picked
As function arguments#
An enum is an ordinary parameter type. A string literal naming a member works too, and is checked before the script runs:
paint(c: Color!): String! { `painted ${c}` }
[paint(Color.GREEN), paint("BLUE")]
paint("PURPLE")
Enums from a schema work the same way, including inside input objects. With the bundled Demo schema (see GraphQL interop):
import Demo
usersByStatus(status: Status.ACTIVE).{{ name }}.map { _.name }
An imported enum's values are also in scope bare, as long as no other import uses the same name (see Modules and imports):
usersByStatus(status: PENDING).{{ name }}.map { _.name }
let byNameDesc = UserSort(field: UserSortField.NAME, direction: SortDirection.DESC)
sortedUsers(sort: byNameDesc).{{ name }}.map { _.name }
In case#
Value patterns are the qualified members (or string literals). A case that names every member is exhaustive: it needs no else, and its result stays non-null:
action(c: Color!): String! {
case (c) {
Color.RED => "stop"
Color.GREEN => "go"
Color.BLUE => "wait"
}
}
[action(Color.RED), action(Color.BLUE)]
Leave a member out and the case can fall through to null, so it no longer fits a String!. Add the member, or an else:
caution(c: Color!): String! {
case (c) {
Color.RED => "stop"
Color.GREEN => "go"
}
}
A nullable operand also needs a null => arm (see Control flow).
From strings (::)#
A string that isn't a literal becomes an enum value with a :: cast. The cast checks the string when it runs:
let favorite = "GREEN"
favorite :: Color!
let wanted = "PURPLE"
wanted :: Color!
Custom scalars#
Declaration#
scalar Email
scalar Semver
A custom scalar is a string with its own type. An Email! and a String! are different types.
Coercion#
A literal converts to a compatible scalar wherever a value is handed over: call arguments, typed slots, list and map literals, and return values. That covers string literals, list and map literals, and templates, even ones with ${...} inside:
sendTo(to: Email!): String! { `mail for ${to}` }
let name = "grace"
[sendTo("ada@example.com"), sendTo(`${name}@example.com`)]
A string that isn't a literal, such as a variable or a + b, doesn't convert:
let address = "ada@example.com"
sendTo(address)
It needs an explicit :: cast:
sendTo(address :: Email!)
List literals don't convert their elements to match each other, so a plain string doesn't join a list of Email!s:
["ada@example.com" :: Email!, "grace@example.com"]
Only a String, or a value of another custom scalar (see below), converts to a scalar. 42 :: Email! is a type error:
42 :: Email!
Scalars from GraphQL responses need no cast. Demo's homepage is a URL!, and fetchURL takes one:
fetchURL(url: homepage)
Some APIs define custom scalars (like a JSON scalar) whose values are numbers, booleans, or even structured JSON. Dang keeps those values as they are: passed back as an argument, one is sent unchanged, and where a String is expected (degrading, interpolation, toString) it reads as its JSON text. Demo's Opaque scalar is one of these:
[describeOpaque(value: opaqueNumber), `object: ${opaqueObject}`]
A schema's scalar JSON is a schema scalar like the ones above, its values as the server sends them. Unqualified, JSON is Dang's own scalar of JSON text (see JSON, YAML, and TOML), a separate type, and decoded data of any shape is Decoded (see Keeping data undecoded). Distinct scalars never mix on their own, but a cast converts one to another, validating at the target, so j :: Dagger.JSON! and d :: JSON! both work:
let pair = ("[1, 2]" :: JSON!)
let pairEmail = (pair :: Email!)
[(pairEmail :: JSON!) == pair, (pairEmail :: String!) == "[1, 2]"]
Text the target rejects fails the cast:
("ada@example.com" :: Email!) :: JSON!
Treating scalars as opaque#
Scalars support equality and list membership, and can go in records and lists. They are Ordered (see Type parameters, Ordered and Numeric): < and .sort compare them by their string form, numerically for a scalar whose value is a number. They have no string methods:
let ada: Email! = "ada@example.com"
let bob: Email! = "bob@example.com"
[ada == ada, [ada].contains(ada), ada < bob]
ada.toUpper
Meta: the :: rule is the durable contract — literals are a convenience, non-literal values always go through an explicit cast.
Degrading to String#
A scalar value turns back into a String! wherever a value is handed over: typed slots, call arguments, return values, reassignment, ::, and case-clause values. It's a string underneath:
let plain: String! = ada
shout(msg: String!): String! { msg.toUpper }
[plain, shout(ada), `to: ${ada}`]
A string literal compared with a scalar converts to it, like a literal argument. A String held in a variable doesn't; cast it first:
[ada == "ada@example.com", ada == (plain :: Email!)]
ada == plain
A String method needs a String receiver, so degrade first:
(ada :: String!).toUpper
Scalar bodies#
A scalar can have a body. Its new() hook takes the raw string and returns the canonical one. It runs every time a value is made: a string literal in a Slug! slot, a :: Slug! cast, a codec decode, and the Slug(...) constructor that declaring new() adds:
scalar Slug {
new(raw: String!) {
raw.trimSpace.toLower.replace(" ", "-")
}
words: [String!]! { (self :: String!).split("-") }
first: String! { words[0] ?? "" }
}
Slug(" Hello World ")
Since every value goes through new(), equality compares the normalized strings:
Slug("A b") == Slug("a-b")
Members must be methods: a scalar holds nothing but its string. Inside a method, self is the scalar value, and sibling methods can be called bare:
Slug("one two three").first
scalar Version { major: Int! = 1 }
A raise in the hook rejects a bad value, and rescue can recover from it at the spot where the value was made:
scalar Upper {
new(raw: String!) {
if (raw != raw.toUpper) { raise `not upper: ${raw}` }
raw
}
self {
of(s: String!): Upper! { Upper(s.toUpper) }
}
}
Upper("nope") rescue { e: Error! => e.message }
A scalar's self { } block declares statics exactly like a type's (see Static members):
Upper.of("mixed Case")
Built-in scalars#
Int!,Float!,String!,Boolean!,ID!(see Types and nullability)Path!andRegexp!are scalars carrying their own methods and a construction hook —Pathnormalizes on construction,Regexpcompiles its pattern.Pathis declared in the prelude with a scalar body (see Scalar bodies); full method signatures for both are in Standard library reference
ID! is its own scalar, not a String alias. String literals still convert into ID! slots, but a String! value doesn't:
let key: ID! = "abc"
let text = "abc"
let other: ID! = text
Path normalizes as it's made:
"src/../docs/./index.md" :: Path!
Meta: scalar fields are invariant across interface implementation — id: String! does not satisfy id: ID!. Cross-ref the variance rules in Interfaces and unions.
See also: enum values and custom scalars declared in an imported schema are reachable through the module name (Demo.Status.ACTIVE), unqualified (Status.ACTIVE), or, for enum values, bare when no other import uses the name (see Modules and imports); GraphQL representations are covered in GraphQL interop.