Directives#

Meta: short page. The interesting bit is that Dang directives are real declarations, type-checked, not magic comments — that's the differentiator from most languages with comment-pragmas.

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.

What a directive is#

directive @deprecated(reason: String = "No longer supported") on FIELD_DEFINITION | OBJECT
directive @experimental on FIELD_DEFINITION | ARGUMENT_DEFINITION | SCALAR
directive @auth(role: String!) on FIELD_DEFINITION
directive @cache(ttl: Int! = 300, key: String) on FIELD_DEFINITION
directive @check on FIELD_DEFINITION

Locations (on ...)#

Applying#

type Person @deprecated(reason: "use NewPerson") {
  name: String! @deprecated
  email: String! @cache(ttl: 60)
  greet(who: String! @experimental): String! { `hi, ${who}` }
}

Person("Ada", "ada@example.com").greet("Grace")
=> "hi, Grace"

Multiple prefix directives go on separate lines, and prefix and suffix directives on the same declaration are all collected. A directive has no effect on what the field does:

@check
@experimental
mixedField: String! @cache(ttl: 120) { "mixed" }

mixedField
=> "mixed"

Using a directive that isn't declared is an error:

type Account { id: String! @audited }
Type error: FieldDecl.Infer: directive validation: DirectiveApplication.Infer: directive @audited not declared

Arguments#

Arguments are named, or positional in declaration order, and the declaration's defaults fill in what's left out:

type Session {
  token: String! @cache(ttl: 60, key: "user")
  user: String! @cache(60, key: "user")
  role: String! @cache
}

Arguments are checked like a function call's: required ones must be given, types must match, and positional arguments come before named ones:

type Admin { panel: String! @auth }
Type error: FieldDecl.Infer: directive validation: DirectiveApplication.Infer: required argument "role" not provided
type Admin { panel: String! @auth(role: 3) }
Type error: FieldDecl.Infer: directive validation: DirectiveApplication.Infer: argument "role" type mismatch: expected String!, got Int!
type Admin { panel: String! @cache(key: "x", 60) }
Type error: FieldDecl.Infer: directive validation: DirectiveApplication.Infer: positional arguments must come before named arguments

Qualified access#

Common built-ins#

"""
doubles a number
"""
@example(```dang
double(21)
```)
double(n: Int!): Int! { n * 2 }

double(21)
=> 42

Structural, not semantic#

Meta: this framing is the differentiator — call it out.