Show Menu
Cheatography

Pkl Cheat Sheet Cheat Sheet (DRAFT) by

Pkl is a new configuration management language developed by Apple, designed to offer a simpler, more intuitive, and efficient way of creating and managing configuration files through its concise key-value structure.

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Comments

Line comment
// Single­-line comment
Block comment
/* First line
Second line */
Doc comment
/// A bird superstar.
/// Unfort­una­tely, extinct.
dodo: Bird

Integers

decimal
123
123
hexade­cimal
0x012AFF
76543
binary
0b00010111
23
octal
0o755
493
decimal with underscore
1_000_000
1000000
hexade­cimal with underscore
0x0134­_64DE
0x013464DE
binary with underscore
0b0001­_0111
0b00010111
octal with underscore
0o0134­_6475
0o01346475
To restrict an integer’s range
client­Port: UInt16
To restrict an integer’s range
server­Port: Int(is­Bet­ween(0, 1023))

Comparison operators for integers

5 == 2
false
5 < 2
false
5 > 2
true
5 <= 2
false
5 >= 2
true

Arithmetic operators for integers

5 + 2
7
5 - 2
3
5 * 2
10
5 / 2
2.5
always Float
5 ~/ 2
2
always Int
5 % 2
1
always Int
5 ** 2
25

Floats

.23
0.23
1.23
1.23
1.23e2
123
1.23e-2
0.0123
NaN
not a number
Infinity
positive infinity
-Infinity
negative infinity
To Restrict a float to a finite value
x: Float(­isF­inite)
To restrict a float’s range
y: Float(­isB­etw­een(0, 10e6))

Booleans

Boolean
true or false

Logical operators for Booleans

logical conjun­ction
true && false
false
logical disjun­ction
true || false
true
logical negation
!false
true
exclusive disjun­ction
true.x­or(­false)
true
logical implic­ation
true.i­mpl­ies­(false)
false

Strings

String
"­Hello, World!­"
Multiline string
"­"­" Although the Dodo is extinct,
the species will be rememb­ered. "­"­"
Unicode
"­\u{26} \u{E9} \u{1F6­00}­"
To concat­enate strings
"­abc­" + "­def­" + "­ghi­"
String Interp­olation
name = "Dodo"
greeting = "Hi, \(name­)!"
Custom String Delimiters
#" \\\\\ "­"­"­"­" "#
Inside a string literal, the following character escape sequences have special meaning:

\t - tab
\n - line feed
\r - carriage return
\" - verbatim quote
\\ - verbatim backslash

String API

"­dod­o".l­ength
4
"­dod­o".r­eve­rse()
"­odo­d"
"­dod­o".c­ont­ain­s("a­liv­e")
false
" dodo ".trim()
"­dod­o"

Dynamic objects vs.Typed Objects

Dynamic object
Schema­-less data models that are not validated
Typed object
Schema­-backed data models that are validated

Objects

Object with properties
dodo {
name = "Dodo"
extinct = true
}
Nested object
dodo {
name = "Dodo"
taxonomy {
`class` = "Aves"
}
}
An object is an ordered collection of values indexed by name.
An object’s key–value pairs are called its proper­ties.
Property values are lazily evaluated on the first read.

Typed Objects

class Bird {
  name: String
  lifespan: Int
  migratory: Boolean
}

pigeon = new Bird {
  name = "Pigeon"
  lifespan = 8
  migratory = false
}

Amending Objects

pigeon {
  name = "Turtle dove"
  extinct = false
}

parrot = (pigeon) {
  name = "Parrot"
}

Transf­orming Objects

dodo {
  name = "Dodo"
  extinct = true
}

dodo
  .toMap()
  .remove("name")
  .toDynamic()

Converting untyped objects to typed objects

class Bird {
  name: String
  lifespan: Int
}

pigeon = new Dynamic {
  name = "Pigeon"
  lifespan = 8
}.toTyped(Bird)

Late Binding

penguin {
  eggIncubation = 40.d
  adultWeightInGrams = eggIncubation.value * 100
}
adultWeightInGrams = penguin.adultWeightInGrams

madeUpBird = (penguin) {
  eggIncubation = 11.d
}
adultWeightInGrams = madeUpBird.adultWeightInGrams

Hidden Properties

class Bird {
  name: String
  lifespan: Int
  hidden nameAndLifespanInIndex = "\(name), \(lifespan)"
  nameSignWidth: UInt = nameAndLifespanInIndex.length
}

/*
pigeon {
  name = "Pigeon"
  lifespan = 8
  nameSignWidth = 9
}
*/

pigeon = new Bird {
  name = "Pigeon"
  lifespan = 8
}

pigeonInIndex = pigeon.nameAndLifespanInIndex
// pigeonInIndex = "Pigeon, 8"

pigeonDynamic = pigeon.toDynamic()
/*
pigeonDynamic {
  name = "Pigeon"
  lifespan = 8
  nameSignWidth = 9
}
*/

Local properties

class Bird {
  name: String
  lifespan: Int
  local separator = ","
  hidden nameAndLifespanInIndex = "\(name)\(separator) \(lifespan)"
}

pigeon = new Bird {
  name = "Pigeon"
  lifespan = 8
}

pigeonInIndex = pigeon.nameAndLifespanInIndex
pigeonSeparator = pigeon.separator // Error

Fixed properties

class Bird {
  wingspan: Int
  weight: Int
  fixed wingspanWeightRatio: Float = wingspan / weight
}

bird = (Bird) {
  wingspan = 10
  weight = 8
}
A property with the fixed modifier cannot be assigned to or amended when defining an object of its class.

Extending a class with fixed properties

abstract class Bird {
  fixed canFly: Boolean
  name: String
}

class Penguin extends Bird {
  canFly = false // Error: missing modifier fixed.
  fixed name = "Penguin" // Error: modifier fixed cannot be applied to property name.
}

Const properties

const laysEggs: Boolean = true

const examples: Listing<String> = new {
  "Pigeon"
  "Hawk"
  "Penguin"
}

pigeonName: String = "Pigeon"

const function birdLifespan(i: Int): Int = (i / 4).toInt()

class Bird {
  name: String
  lifespan: Int
}

const bird: Bird = new {
  name = pigeonName // Error: cannot reference non-const property pigeonName from a const property.
  lifespan = birdLifespan(24) // Allowed: birdLifespan is const.
}
Because const members can only reference its own values, or other const members, they are not late bound.
 

Durations

nanose­conds (smallest unit)
5.ns
micros­econds
5.us
millis­econds
5.ms
seconds
5.s
minutes
5.min
hours
5.h
days (largest unit)
3.d
negative duration
-5.min
duration with floating point value
5.13.min

Comparison operators for durations

5.min == 3.s
false
5.min < 3.s
false
5.min > 3.s
true
5.min <= 3.s
false
5.min >= 3.s
true

Arithmetic operators for durations

5.min + 3.s
5.05.min
5.min - 3.s
4.95.min
5.min * 3
15.min
5.min / 3
1.6666­666­666­666­667.min
5.min / 3.min
1.6666­666­666­666667
5.min ~/ 3
1.min
5.min ~/ 3.min
1
5.min % 3
2.min
5.min ** 3
125.min
The value component can be an expres­sion:
x = 5
xMinutes = x.min
y = 3
xySeconds = (x + y).s

Data sizes with decimal unit (factor 1000)

bytes (smallest unit)
5.b
kilobytes
5.kb
megabytes
5.mb
gigabytes
5.gb
terabytes
5.tb
petabytes (largest unit)
5.pb

Data sizes with binary unit (factor 1024)

bytes (smallest unit)
5.b
kilobytes
5.kb
megabytes
5.mb
gigabytes
5.gb
terabytes
5.tb
petabytes (largest unit)
5.pb

Arithmetic operators for data sizes

5.mb + 3.kib
5.0030­72.mb
5.mb - 3.kib
4.9969­28.mb
5.mb * 3
15.mb
5.mb / 3
1.6666­666­666­666­667.mb
5.mb / 3.mb
1.6666­666­666­666667
5.mb ~/ 3
1.mb
5.mb ~/ 3.mb
1
5.mb % 3
2.mb
5.mb ** 3
125.mb
The value component can be an expres­sion:
x = 5
xMegabytes = x.mb
y = 3
xyKibi­bytes = (x + y).kib

Comparison operators for data sizes

5.mb == 3.kib
false
5.mb < 3.kib
false
5.mb > 3.kib
true
5.mb <= 3.kib
false
5.mb >= 3.kib
true