-
Notifications
You must be signed in to change notification settings - Fork 259
/
base.pkl
3151 lines (2634 loc) · 107 KB
/
base.pkl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//===----------------------------------------------------------------------===//
// Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//===----------------------------------------------------------------------===//
/// Fundamental properties, methods, and classes for writing Pkl programs.
///
/// Members of this module are automatically available in every Pkl module.
@ModuleInfo { minPklVersion = "0.26.0" }
module pkl.base
import "pkl:jsonnet"
import "pkl:xml"
import "pkl:protobuf"
/// The top type of the type hierarchy.
///
/// Every type is a subtype of [Any].
///
/// The following operators are supported for all values:
/// ```
/// value1 == value2 // equality
/// value1 != value2 // inequality
/// value.member // member access
/// value?.member // conditional member access; returns `value.member` if `value` is non-null and `null` otherwise
/// value ?? default // null coalescing; returns `value` if `value` is non-null and `default` otherwise
/// value is String // type test
/// value as String // type cast; throws an error unless `value is String`
/// ```
abstract external class Any {
/// Returns the class of [this].
external function getClass(): Class
/// Returns a string representation of [this].
///
/// This method is used to convert the values of string interpolation expressions to strings.
external function toString(): String
/// Returns `this |> transform` if [this] is non-null, and [null] otherwise.
///
/// This method is the complement of the `??` operator and the equivalent of an `Option` type's `map` and `flatMap` methods.
external function ifNonNull<Result>(transform: (NonNull) -> Result): Result?
}
/// The type of [null] and null values created with [Null()].
///
/// All null values are pairwise equal according to `==`.
external class Null extends Any
/// A non-null value.
typealias NonNull = Any(!(this is Null))
/// The runtime representation of a class.
external class Class<out Type> extends Any {
/// The unqualified name of this class.
external simpleName: String
}
/// The runtime representation of a type alias.
external class TypeAlias extends Any
/// Base class for modules.
abstract external class Module {
/// Returns the relative, descendent directory path between this module and [other].
///
/// Throws if no such path exists.
///
/// For example, if module `mod1` has path `/dir1/mod1.pkl`, and module `mod2` has path `/dir1/dir2/dir3/mod2.pkl`,
/// then `mod1.relativePathTo(mod2)` will return `List("dir2", "dir3")`.
///
/// A common use case is to compute the directory path between a template located at the root of a hierarchy
/// (say `rootModule.pkl`) and the currently evaluated module (accessible via the `module` keyword):
/// ```
/// import "rootModule.pkl" // self-import
/// path = rootModule.relativePathTo(module)
/// ```
external function relativePathTo(other: Module): List<String>
/// The output of this module.
///
/// Defaults to all module properties rendered as either Pcf or the format specified on the command line.
hidden output: ModuleOutput = new {
value = outer
renderer =
let (format = read?("prop:pkl.outputFormat") ?? "pcf")
if (format == "json") new JsonRenderer {}
else if (format == "jsonnet") new jsonnet.Renderer {}
else if (format == "pcf") new PcfRenderer {}
else if (format == "plist") new PListRenderer {}
else if (format == "properties") new PropertiesRenderer {}
else if (format == "textproto") new protobuf.Renderer {}
else if (format == "xml") new xml.Renderer {}
else if (format == "yaml") new YamlRenderer {}
else throw("Unknown output format: `\(format)`. Supported formats are `json`, `jsonnet`, `pcf`, `plist`, `properties`, `textproto`, `xml`, `yaml`.")
text = renderer.renderDocument(value)
}
}
/// Base class for annotation types.
abstract class Annotation
/// Indicates that the annotated member was introduced in [version].
class Since extends Annotation {
/// The version that the annotated member was introduced in.
version: String
}
/// Indicates that the annotated member is deprecated and will likely be removed in the future.
class Deprecated extends Annotation {
/// The version in which the annotated member became deprecated.
since: String?
/// A message explaining how to deal with the deprecation.
///
/// The message may contain member links, should end with a period, and should not contain line breaks.
///
/// Example: `"Use [String.codePoints] instead."`
message: String?
/// The code fragment to replace usages of the deprecated member with.
///
/// Setting this property instructs tools to automatically replace usages of the deprecated member.
/// For human instructions, use [message].
///
/// Examples:
/// ```
/// // replace usages of a deprecated class or type alias with `Inventory`
/// replaceWith = "Inventory"
/// // replace usages of a deprecated property or method with `inventory`
/// replaceWith = "inventory"
/// // replace usages of a deprecated property or method with `store.inventory`
/// // and an import of `store`, which must resolve to an import of the current module
/// replaceWith = "store.inventory"
/// // replace usages of a deprecated property or method with `order(42)`
/// replaceWith = "order(42)"
/// // replace usages of a deprecated method (that has a parameter named `amount`) with `order(amount)`
/// replaceWith = "order(amount)"
/// ```
@SourceCode { language = "PklExpr" }
replaceWith: String?
}
/// Lists alternative names that the annotated member is known under.
///
/// The alternative names will be included in Pkldoc's search index.
class AlsoKnownAs extends Annotation {
/// The alternative names for the annotated member.
names: Listing<String>
}
/// Indicates that the annotated member should not be advertised by Pkldoc and other tools.
class Unlisted extends Annotation
/// Indicates to Pkldoc that the annotated module is an example for how to use [subjects].
class DocExample extends Annotation {
/// The fully qualified names of the modules that are the subjects of the example.
subjects: Listing<String>
}
/// Indicates that the annotated property's string value contains source code written in [language].
class SourceCode extends Annotation {
/// The language that the source code is written in.
///
/// Examples:
/// - `"x = 42"` is valid source code for language `"Pkl"`.
/// - `"42"` is valid source code for language `"PklExpr"`.
/// - `"42"` is valid source code for language `"Pkl"` with [prefix] `"x = "`.
language: "Go"|"HTML"|"Java"|"JavaScript"|"Markdown"|"Pkl"|"PklExpr"|"Python"|"Ruby"|"SQL"|"Swift"|String
/// A source code prefix to help tools understand the source code.
///
/// For example, an expression may not be considered valid unless wrapped in a statement.
prefix: String?
/// A source code suffix to help tools understand the source code.
///
/// For example, an expression may not be considered valid unless wrapped in a statement.
suffix: String?
}
/// Metadata for a module.
///
/// To annotate a module, place the annotation
/// before the `module`, `amends`, or `extends` keyword, whichever comes first.
///
/// ```
/// @ModuleInfo { minPklVersion = "1.2.3"; author = "author@apple.com" }
/// module myModule
/// ```
///
/// All library modules should have a [ModuleInfo] annotation.
class ModuleInfo extends Annotation {
/// The minimum Pkl version required by this module.
///
/// The expected format is `"major.minor.patch"`.
///
/// This version constraint is enforced by the Pkl runtime.
minPklVersion: String(matches(Regex(#"\d{1,2}\.\d{1,2}\.\d{1,2}"#)))
}
/// A representation of a rendered output.
open class FileOutput {
/// The value to render.
value: Any?
/// The renderer for [value].
renderer: ValueRenderer = new PcfRenderer {}
/// The final rendered output.
text: String = renderer.renderDocument(value)
}
/// The contents and appearance of a module's output.
class ModuleOutput extends FileOutput {
/// The files to be produced by this module when rendering in multiple file output mode.
///
/// Keys determine file paths, and may include subdirectories.
/// [FileOutput.text] determines file contents.
///
/// Example:
/// ```
/// output {
/// files {
/// ["foo/bar.yml"] {
/// value = new { bar = "bar" }
/// renderer = new YamlRenderer {}
/// }
/// }
/// }
/// ```
///
/// A file can be set to the `output` of another module.
/// For example:
/// ```
/// output {
/// files {
/// ["foo.yml"] = import("foo.pkl").output
/// }
/// }
/// ```
files: Mapping<String, FileOutput>?
}
/// Base class for rendering Pkl values in some output format.
///
/// A renderer's output is guaranteed to be well-formed unless [RenderDirective] is part of the input.
abstract class ValueRenderer {
/// Value converters to apply before values are rendered.
///
/// A value converter is a function that converts a value to another value
/// immediately before the value is rendered (and after the value has been type-checked).
/// The converted value is then rendered by the renderer's hardwired rendering logic.
///
/// When a converter returns a value of type [Object], [Collection], [Map], or [Pair],
/// converters are recursively applied to the returned value's components (object properties,
/// list elements, etc.).
///
/// At most one converter is applied per value.
/// If multiple converters are applicable, a winner is chosen as follows:
/// - A converter with [String] key wins over a converter with [Class] key.
/// - Between multiple [String] key converters, the converter defined earlier
/// (according to the mapping's definition order) wins.
/// - Between multiple [Class] key converters, the most specific class (according to class
/// hierarchy) wins.
///
/// A converter's key specifies which values the converter should be applied to.
/// A converter with [Class] key is applied to values of that class, including values of its
/// subclasses.
/// A converter with [String] key is applied to values whose path matches the *path spec*
/// described by the string.
///
/// Path specs can have the following components:
/// - `^` matches the top-level value passed to `renderDocument()` or `renderValue()` (often the
/// module object)
/// - `pigeon` matches property `pigeon` at any hierarchy level
/// - `[pigeon]` matches map(ping) entry with String key `"pigeon"` at any hierarchy level
/// - `*` matches any property
/// - `[*]` matches any list(ing) element or map(ping) entry
///
/// Here are some examples of valid property paths:
/// - `"server"`
/// - `"server.timeout"`
/// - `"^server.timeout"` (matches `server.timeout`, but not `racks[12345].server.timeout`)
/// - `"racks[12345].server"`
/// - `"racks[*].server`
///
/// Paths are matched against path specs component-wise in reverse order.
/// For example, paths `server.timeout` and `racks[*].server.timeout`
/// both match path spec `server.timeout`, whereas path `server.timeout.millis` does not.
converters: Mapping<Class|String, (unknown) -> Any>
/// The file extension associated with this output format,
/// or [null] if this format does not have an extension.
extension: String?
/// Renders [value] as a complete document.
///
/// Some renderers impose restrictions on which types of values can be rendered as document.
///
/// A typical implementation of this method renders a document header/footer
/// and otherwise delegates to [renderValue()].
abstract function renderDocument(value: Any): String
/// Renders [value].
abstract function renderValue(value: Any): String
}
/// Renders values as Pcf, a static subset of Pkl.
class PcfRenderer extends ValueRenderer {
extension = "pcf"
/// The characters to use for indenting output.
/// Defaults to two spaces.
indent: String = " "
/// Whether to skip rendering properties whose value is [null].
omitNullProperties: Boolean = false
/// Whether to use custom string delimiters for string values.
///
/// If [true], custom string delimiters (such as `#"..."#`) are preferred
/// over escaping quotes and backslashes.
useCustomStringDelimiters: Boolean = false
external function renderDocument(value: Any): String
external function renderValue(value: Any): String
}
/// Directs a [ValueRenderer] to output [text] as-is in place of this directive.
///
/// [RenderDirective] is an escape hatch for exceptional cases.
/// It gives full control over how a value is rendered
/// without having to manipulate `output.text` as a whole.
/// It is the user's responsibility to ensure that the resulting output is well-formed.
///
/// Example:
/// ```
/// name = "Pigeon"
/// hobby = "singing"
///
/// output {
/// renderer = new JsonRenderer {
/// converters {
/// // render String values without quotes (not valid JSON)
/// [String] = (str) -> new RenderDirective { text = str }
/// }
/// }
/// }
/// ```
///
/// Output:
/// ```
/// {
/// "name": Pigeon
/// "hobby": singing
/// }
/// ```
class RenderDirective {
/// The text to output as-is (without escaping or quoting).
text: String
}
/// Directs [PcfRenderer] to output additional text [before] and/or [after] rendering a [value].
class PcfRenderDirective {
/// The text to output before rendering [value].
before: String?
/// The value to render.
value: Any
/// The text to output after rendering [value].
after: String?
}
/// Renders values as JSON.
class JsonRenderer extends ValueRenderer {
extension = "json"
/// The characters to use for indenting output.
indent: String = " "
/// Whether to omit JSON object fields whose value is `null`.
omitNullProperties: Boolean = true
/// Renders [value] as a JSON document.
///
/// Every JSON value is a valid JSON document.
external function renderDocument(value: Any): String
external function renderValue(value: Any): String
}
/// Renders values as YAML.
///
/// To render a YAML stream, set [isStream] to [true].
class YamlRenderer extends ValueRenderer {
extension = "yaml"
/// The YAML version used by consumers of the rendered documents.
///
/// - `"compat"` - YAML 1.1 _or_ 1.2 (default)
/// - `"1.1"` - YAML 1.1
/// - `"1.2"` - YAML 1.2 (Core schema)
///
/// At present, the mode only affects which String values are quoted in YAML.
/// For example, `x = "yes"` is rendered as `x: 'yes'` in modes `"compat"` and `"1.1"`,
/// and as `x: yes` in mode `"1.2"`.
mode: "compat"|"1.1"|"1.2" = "compat"
/// The number of spaces to use for indenting output.
indentWidth: Int(this > 1) = 2
/// Whether to skip rendering properties whose value is [null].
omitNullProperties: Boolean = true
/// If [true], [renderDocument] expects an argument of type [Listing] or [Collection]
/// and renders it as YAML stream.
isStream: Boolean = false
/// Renders [value] as a YAML document.
///
/// Every YAML value is a valid YAML document.
external function renderDocument(value: Any): String
/// Renders [value] as YAML value.
external function renderValue(value: Any): String
}
/// Renders values as [XML property lists](https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/PropertyLists/UnderstandXMLPlist/UnderstandXMLPlist.html).
///
/// XML property lists do not support [null] values.
/// This renderer handles [null] values as follows:
/// - Object and map properties whose value is [null] are skipped.
/// - [null] values occurring in a list or set result in an error.
class PListRenderer extends ValueRenderer {
extension = "plist"
/// The characters to use for indenting output.
indent: String = " "
external function renderDocument(value: Any): String
external function renderValue(value: Any): String
}
/// Renders values as [Java Properties](https://docs.oracle.com/javase/8/docs/api/java/util/Properties.html).
///
/// Pkl object properties and keys of type [Boolean], [String], [Int], and [Float]
/// are flattened into dot-separated Java property keys.
///
/// Pkl values of type [Boolean], [String], [Int], and [Float] are rendered as Java property values.
///
/// To render Pkl values of other types, convert them to one of the above types with [converters].
///
/// Example:
/// ```
/// person {
/// name = "Pigeon"
/// age = 42
/// hobbies = List("surfing", "fire making")
/// }
///
/// output {
/// renderer = new PropertiesRenderer {
/// converters {
/// // render lists as comma-separated values
/// [List] = (it) -> it.join(",")
/// }
/// }
/// }
/// ```
///
/// The above example produces the following output:
/// ```
/// person.name = Pigeon
/// person.age = 42
/// person.hobbies = surfing,fire making
/// ```
class PropertiesRenderer extends ValueRenderer {
extension = "properties"
/// Whether to skip rendering properties whose value is [null].
omitNullProperties: Boolean = true
/// Whether to render characters outside the printable US-ASCII charset range as
/// [Unicode escapes](https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.3).
restrictCharset: Boolean = false
external function renderDocument(value: Any): String
external function renderValue(value: Any): String
}
/// An external (file, HTTP, etc.) resource.
///
/// Usually, a [Resource] is obtained via a `read()` expression, such as `read("some/file")`.
class Resource {
/// The URI of this resource.
uri: Uri
/// The text content of this resource.
text: String
/// The content of this resource in Base64.
base64: String
/// The [MD5](https://en.wikipedia.org/wiki/MD5)
/// hash of this resource as hexadecimal string.
///
/// MD5 is cryptographically broken and should not be used for secure applications.
external md5: String
/// The [SHA-1](https://en.wikipedia.org/wiki/SHA-1)
/// hash of this resource as hexadecimal string.
///
/// SHA-1 is cryptographically broken and should not be used for secure applications.
external sha1: String
/// The [SHA-256](https://en.wikipedia.org/wiki/SHA-2)
/// cryptographic hash of this resource as hexadecimal string.
external sha256: String
/// The first 64 bits of the [SHA-256](https://en.wikipedia.org/wiki/SHA-2)
/// cryptographic hash of this resource.
external sha256Int: Int
}
/// Common base class of [Int] and [Float].
///
/// ## Arithmetic Operators
/// ```
/// -a // negation
/// a + b // addition
/// a - b // subtraction
/// a * b // multiplication
/// a / b // division
/// a ~/ b // integer division
/// a % b // remainder
/// a ** b // exponentiation
/// ```
///
/// ## Comparison Operators
/// ```
/// a == b // equality
/// a != b // inequality
/// a < b // less than
/// a > b // greater than
/// a <= b // less than or equal
/// a >= b // greater than or equal
/// ```
///
/// ## Number vs. Float
/// To allow transparent use of [Float] and [Int],
/// use [Number] instead of [Float] in type annotations.
abstract external class Number extends Any {
/// A [Duration] with value [this] and unit `"ns"` (nanoseconds).
abstract ns: Duration
/// A [Duration] with value [this] and unit `"us"` (microseconds).
abstract us: Duration
/// A [Duration] with value [this] and unit `"ms"` (milliseconds).
abstract ms: Duration
/// A [Duration] with value [this] and unit `"s"` (seconds).
abstract s: Duration
/// A [Duration] with value [this] and unit `"min"` (minutes).
abstract min: Duration
/// A [Duration] with value [this] and unit `"h"` (hours).
abstract h: Duration
/// A [Duration] with value [this] and unit `"d"` (days).
abstract d: Duration
/// A [DataSize] with value [this] and unit `"b"` (bytes).
abstract b: DataSize
/// A [DataSize] with value [this] and unit `"kb"` (kilobytes).
///
/// Facts:
/// ```
/// 1.kb == 1000.b
/// ```
abstract kb: DataSize
/// A [DataSize] with value [this] and unit `"mb"` (megabytes).
///
/// Facts:
/// ```
/// 1.mb == 1000.kb
/// ```
abstract mb: DataSize
/// A [DataSize] with value [this] and unit `"gb"` (gigabytes).
///
/// Facts:
/// ```
/// 1.gb == 1000.mb
/// ```
abstract gb: DataSize
/// A [DataSize] with value [this] and unit `"tb"` (terabytes).
///
/// Facts:
/// ```
/// 1.tb == 1000.gb
/// ```
abstract tb: DataSize
/// A [DataSize] with value [this] and unit `"pb"` (petabytes).
///
/// Facts:
/// ```
/// 1.pb == 1000.tb
/// ```
abstract pb: DataSize
/// A [DataSize] with value [this] and unit `"kib"` (kibibytes).
///
/// Facts:
/// ```
/// 1.kib == 1024.b
/// ```
abstract kib: DataSize
/// A [DataSize] with value [this] and unit `"mib"` (mebibytes).
///
/// Facts:
/// ```
/// 1.mib == 1024.kib
/// ```
abstract mib: DataSize
/// A [DataSize] with value [this] and unit `"gib"` (gibibytes).
///
/// Facts:
/// ```
/// 1.gib == 1024.mib
/// ```
abstract gib: DataSize
/// A [DataSize] with value [this] and unit `"tib"` (tebibytes).
///
/// Facts:
/// ```
/// 1.tib == 1024.gib
/// ```
abstract tib: DataSize
/// A [DataSize] with value [this] and unit `"pib"` (pebibytes).
///
/// Facts:
/// ```
/// 1.pib == 1024.tib
/// ```
abstract pib: DataSize
/// The sign of this number.
///
/// Returns `0` for `0`, `0.0`, `-0.0`, and [NaN],
/// `1` for positive numbers (including [Infinity]),
/// and `-1` for negative numbers (including `-`[Infinity]).
abstract sign: Number
/// The absolute value of this number.
///
/// Facts:
/// ```
/// 42.abs == 42
/// -42.abs == 42
/// 0.0.abs == 0.0
/// (-0.0).abs == 0.0
/// Infinity.abs == Infinity
/// (-Infinity).abs == Infinity
/// NaN.abs == NaN
/// ```
abstract abs: Number
/// Rounds this number to the next mathematical integer towards [Infinity].
///
/// If [this] is an [Int], returns [this].
/// If [this] is [NaN], [Infinity], -[Infinity], `0.0`, or `-0.0`, returns [this].
/// Otherwise, returns the smallest [Float] that is greater than or equal to [this]
/// and is equal to a mathematical integer.
abstract ceil: Number
/// Rounds this number to the next mathematical integer towards -[Infinity].
///
/// If [this] is an [Int], returns [this].
/// If [this] is [NaN], [Infinity], -[Infinity], `0.0`, or `-0.0`, returns [this].
/// Otherwise, returns the largest [Float] that is less than or equal to [this]
/// and is equal to a mathematical integer.
abstract floor: Number
/// Rounds this number to the nearest mathematical integer, breaking ties in favor
/// of the even integer.
///
/// If [this] is an [Int], returns [this].
/// If [this] is [NaN], [Infinity], -[Infinity], `0.0`, or `-0.0`, returns [this].
/// Otherwise, return the [Float] that is nearest to [this] and is equal to a
/// mathematical integer. If two mathematical integers are equally near, returns
/// the even integer.
abstract function round(): Number
// https://github.com/julialang/julia/issues/8750#issue-46387044
/// Rounds this number to the next mathematical integer towards zero.
///
/// If [this] is an [Int], returns [this].
/// If [this] is [NaN], [Infinity], -[Infinity], `0.0`, or `-0.0`, returns [this].
/// If [this] is less than zero, returns the smallest [Float] that is greater than or equal to [this]
/// and is equal to a mathematical integer.
/// Otherwise, returns the largest [Float] that is less than or equal to [this]
/// and is equal to a mathematical integer.
abstract function truncate(): Number
/// Converts this number to an [Int].
///
/// If [this] is an [Int], returns [this].
/// If [this] is [NaN], [Infinity], or -[Infinity], throws an error.
/// Otherwise, returns the [Int] representation for `this.truncate()`.
/// If `this.truncate()` is not representable in (that is, too large for) [Int], throws an error.
abstract function toInt(): Int
/// Converts this number to a [Float].
///
/// If [this] is a [Float], returns [this].
/// Otherwise, returns the [Float] representation for [this].
/// If [this] is not representable in [Float], returns the [Float] nearest to [this].
abstract function toFloat(): Float
/// Converts this number to its decimal string representation.
abstract function toString(): String
/// Converts this number to a decimal fixed-point representation with [fractionDigits] digits after the decimal point.
abstract function toFixed(fractionDigits: Int(this.isBetween(0, 20))): String
/// Converts this number to a duration with [this] value and the given [unit].
abstract function toDuration(unit: DurationUnit): Duration
/// Converts this number to a data size with [this] value and the given [unit].
abstract function toDataSize(unit: DataSizeUnit): DataSize
/// Tells if this number is greater than or equal to zero.
///
/// Facts:
/// ```
/// 0.isPositive
/// (-0).isPositive
/// 0.0.isPositive
/// (-0.0).isPositive
/// 3.isPositive
/// 3.14.isPositive
/// Infinity.isPositive
///
/// !(-3).isPositive
/// !(-3.14).isPositive
/// !(-Infinity).isPositive
/// !NaN.isPositive
/// ```
abstract isPositive: Boolean
/// Tells if this number is neither [NaN] nor [isInfinite].
abstract isFinite: Boolean
/// Tells if this number is [Infinity] or -[Infinity].
abstract isInfinite: Boolean
/// Tells if this number is [NaN].
///
/// Always use this method when testing for [NaN].
/// Note that `x == NaN` is *not* a correct way to test for [NaN] because `NaN != NaN` as per the IEEE spec.
abstract isNaN: Boolean
/// Tells if this number is not 0.
abstract isNonZero: Boolean
/// Tells if this number is greater than or equal to [start] and less than or equal to [inclusiveEnd].
///
/// Facts:
/// ```
/// 3.2.isBetween(2.6, 4)
/// 3.2.isBetween(2.6, 3.2)
/// 3.2.isBetween(3.2, 4)
/// !3.2.isBetween(1, 3.1)
/// ```
abstract function isBetween(start: Number, inclusiveEnd: Number): Boolean
}
/// A 64-bit signed integer.
///
/// The following operators are supported for [Number]s:
/// ```
/// -a // negation
/// a + b // addition
/// a - b // subtraction
/// a * b // multiplication
/// a / b // division
/// a ~/ b // truncating division
/// a % b // remainder
/// a ** b // exponentiation
/// ```
external class Int extends Number {
external ns: Duration
external us: Duration
external ms: Duration
external s: Duration
external min: Duration
external h: Duration
external d: Duration
external b: DataSize
external kb: DataSize
external mb: DataSize
external gb: DataSize
external tb: DataSize
external pb: DataSize
external kib: DataSize
external mib: DataSize
external gib: DataSize
external tib: DataSize
external pib: DataSize
external sign: Int
external abs: Int
external ceil: Int
external floor: Int
external function round(): Int
external function truncate(): Int
external function toInt(): Int
external function toFloat(): Float
external function toString(): String
/// Converts this number to a string representation in the given radix.
///
/// Digits above 9 are converted to lowercase letters `a` to `z`.
/// To pad the resulting string with zeros, use [String.padStart()].
///
/// Facts:
/// ```
/// (-1).toRadixString(20) == "-1"
/// 42.toRadixString(16) == "2a"
/// ```
external function toRadixString(radix: Int(this.isBetween(2, 36))): String
external function toFixed(fractionDigits: Int(this.isBetween(0, 20))): String
external function toDuration(unit: DurationUnit): Duration
external function toDataSize(unit: DataSizeUnit): DataSize
/// Shifts this integer left by [n] bits.
external function shl(n: Int): Int
/// Shifts this integer right by [n] bits, preserving the sign bit.
external function shr(n: Int): Int
/// Shifts this integer right by [n] bits, setting the sign bit to zero.
///
/// This operation is known as *unsigned right shift*.
external function ushr(n: Int): Int
/// Bitwise AND of this integer and [n].
external function and(n: Int): Int
/// Bitwise OR of this integer and [n].
external function or(n: Int): Int
/// Bitwise XOR of this integer and [n].
external function xor(n: Int): Int
/// Bitwise NOT (inverse) of this integer.
external inv: Int
external isPositive: Boolean
external isFinite: Boolean
external isInfinite: Boolean
external isNaN: Boolean
/// Tells if this integer is evenly divisible by two.
external isEven: Boolean
/// Tells if this number is not [isEven].
external isOdd: Boolean
external isNonZero: Boolean
external function isBetween(start: Number, inclusiveEnd: Number): Boolean
/// Returns the Unicode character with code point [this].
///
/// Throws if [this] is not a valid code point.
external function toChar(): Char
}
/// An [Int] value in range [math.minInt8]..[math.maxInt8].
typealias Int8 = Int(isBetween(-128, 127))
/// An [Int] value in range [math.minInt16]..[math.maxInt16].
typealias Int16 = Int(isBetween(-32768, 32767))
/// An [Int] value in range [math.minInt32]..[math.maxInt32].
typealias Int32 = Int(isBetween(-2147483648, 2147483647))
/// An [Int] value in range `0`..[math.maxUInt8].
typealias UInt8 = Int(isBetween(0, 255))
/// An [Int] value in range `0`..[math.maxUInt16].
typealias UInt16 = Int(isBetween(0, 65535))
/// An [Int] value in range `0`..[math.maxUInt32].
typealias UInt32 = Int(isBetween(0, 4294967295))
/// An [Int] value in range `0`..[math.maxUInt].
///
/// Note that [math.maxUInt] is equal to [math.maxInt],
/// not `maxInt * 2 + 1` as one might expect.
/// That is, [UInt] has half the range of [Int].
typealias UInt = Int(isPositive)
/// A value that can be compared to another value of the same type with `<`, `>`, `<=`, and `>=`.
typealias Comparable = String|Number|Duration|DataSize
/// A 64-bit floating-point number conforming to the IEEE 754 binary64 format.
///
/// The following binary operators are supported for [Number]s:
/// ```
/// -a // negative
/// a + b // addition
/// a - b // subtraction
/// a * b // multiplication
/// a / b // division
/// a ~/ b // integer division
/// a % b // remainder
/// a ** b // exponentiation
/// ```
///
/// Tip: To allow transparent use of [Float] and [Int],
/// use [Number] instead of [Float] in type annotations.
external class Float extends Number {
external ns: Duration
external us: Duration
external ms: Duration
external s: Duration
external min: Duration
external h: Duration
external d: Duration
external b: DataSize
external kb: DataSize
external mb: DataSize
external gb: DataSize
external tb: DataSize
external pb: DataSize
external kib: DataSize
external mib: DataSize
external gib: DataSize
external tib: DataSize
external pib: DataSize
external sign: Float
external abs: Float
external ceil: Float
external floor: Float
external function round(): Float
external function truncate(): Float