-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathspec.html
12387 lines (11814 loc) · 498 KB
/
spec.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Ballerina Language Specification</title>
<meta charset="utf-8" />
<style type="text/css">
body { font-family: sans-serif; line-height: 1.38; }
pre.grammar code, code.grammar { background-color: #D0D0D0; }
pre, code { font-family: Consolas, monospace; }
table { border: solid thin; border-collapse: collapse; }
td, th { border: solid thin; padding: 0.5em; }
p.status { font-size: large; font-weight: bold; }
</style>
</head>
<body>
<h1>Ballerina Language Specification, 2024R1</h1>
<p>
Primary contributors:
</p>
<ul>
<li>James Clark, <a href="mailto:[email protected]">[email protected]</a></li>
<li>Sanjiva Weerawarana, <a href="mailto:[email protected]">[email protected]</a></li>
<li>Sameera Jayasoma, <a href="mailto:[email protected]">[email protected]</a></li>
<li>Hasitha Aravinda, <a href="mailto:[email protected]">[email protected]</a></li>
</ul>
<p>
(Other contributors are listed in <a href="#contributors">Appendix D</a>.)
</p>
<p>
Copyright © 2018-2024 <a href="https://wso2.com/">WSO2</a>
</p>
<p>
Licensed under the <a
href="https://creativecommons.org/licenses/by-nd/4.0/">Creative Commons
Attribution-NoDerivatives 4.0 International</a> license
</p>
<p class="status">
Language and document status
</p>
<p>
The language described in this specification is now stable. We do not plan to
make changes that introduce significant incompatibilities.
</p>
<p>
This release contains a preview of the regular expression feature. Minor
changes may be made before it becomes stable.
</p>
<p>
Comments on this document are welcome and should be made by creating an issue in
<code><a href="https://github.com/ballerina-platform/ballerina-spec"
>https://github.com/ballerina-platform/ballerina-spec</a></code>, which is the
GitHub repository where this specification is maintained.
</p>
<section class="toc">
<h2>Table of contents</h2>
<p><a href="#introduction">1. Introduction</a></p>
<p><a href="#notation">2. Notation</a></p>
<p><a href="#program_structure">3. Program structure</a></p>
<p><a href="#lexical_structure">4. Lexical structure</a></p>
<p><a href="#values_types">5. Values, types and variables</a></p>
<p><a href="#expressions">6. Expressions</a></p>
<p><a href="#actions_statements">7. Actions and statements</a></p>
<p><a href="#module_level">8. Module-level declarations</a></p>
<p><a href="#metadata">9. Metadata</a></p>
<p><a href="#data_tags">10. Data tags</a></p>
<p><a href="#lang_library">11. Lang library</a></p>
<p><a href="#references">A. References</a></p>
<p><a href="#changes">B. Changes since previous versions</a></p>
<p><a href="#planned_future_functionality">C. Planned future functionality</a></p>
<p><a href="#contributors">D. Other contributors</a></p>
</section>
<section>
<h2 id="introduction">1. Introduction</h2>
<p>
Ballerina is a statically typed, concurrent programming language, focusing on
network interaction and structured data. It is intended to be the core of a
language-centric middleware platform. It has all the general-purpose
functionality expected of a modern programming language, but it also has several
unusual aspects that make it particularly suitable for its intended purpose.
</p>
<p>
First, it provides language constructs specifically for consuming and providing
network services. Future versions of Ballerina will add language constructs for
other middleware functionality such as event stream processing and reliable
messaging; this is described in more detail in <a
href="#planned_future_functionality">Appendix C</a>.
</p>
<p>
Second, its abstractions and syntax for concurrency and network interaction have
been designed so that there is a close correspondence with sequence diagrams.
This enables a bidirectional mapping for any Ballerina function between its
textual representation in the syntax described in this specification and its
graphical representation as a sequence diagram, such that the sequence diagram
fully shows the aspects of the behavior of that function that relate to
concurrency and network interaction.
</p>
<p>
Third, it has a type system that is more flexible and allows for looser coupling
than traditional statically typed languages. The type system is structural:
instead of requiring the program to explicitly say which types are compatible
with each other, compatibility of types and values is determined automatically
based on their structure; this is particularly useful when combining data from
multiple, independently-designed systems. In addition, the type system provides
union types and open records. This flexibility allows the type system to be used
as a schema for the data that is exchanged in distributed applications.
Ballerina's data types are designed to work particularly well with JSON; any
JSON value has a direct, natural representation as a Ballerina value. Ballerina
also provides support for XML and tabular data.
</p>
<p>
Ballerina is not a research language. It is intended to be a pragmatic language
suitable for mass-market commercial adoption. It tries to feel familiar to
programmers who are used to popular, modern C-family languages, notably Java, C#
and JavaScript. It also gets ideas and inspiration from many other existing
programming languages including TypeScript, Go, Rust, D, Kotlin, Swift, Python
and Perl.
</p>
<p>
The Ballerina language has been designed in conjunction with the Ballerina
platform, which provides comprehensive support for a module-based software
development model, including versioning, dependency management, testing,
documentation, building and sharing. Modules are organized into repositories;
there is a globally-shared, central repository, but repositories can also be
local.
</p>
<p>
The Ballerina language includes a small library, the lang library, which
provides fundamental operations on the data types defined by the language; the
lang library is defined by this specification. The Ballerina platform includes
an extensive standard library, which includes not only the usual low-level,
general-purpose functionality, but also support for a wide variety of network
protocols, interface standards, data formats and authentication/authorization
standards, which make writing secure, resilient distributed applications
significantly easier than with other languages. The standard library is not
specified in this document.
</p>
</section>
<section>
<h2 id="notation">2. Notation</h2>
<p>
Productions are written in the form:
</p>
<pre>symbol := rhs
</pre>
<p>
where symbol is the name of a nonterminal, and <code>rhs</code> is as follows:
</p>
<ul>
<li><code>0xX</code> means the single character whose Unicode code point is
denoted by the hexadecimal numeral X</li>
<li><code>^x</code> means any single Unicode code point that does not match x
and is not a disallowed character;</li>
<li><code>x..y</code> means any single Unicode character whose code point is
greater than or equal to that of x and less than or equal to that of y</li>
<li><code class="grammar">xyz</code> means the characters <code>xyz</code> literally</li>
<li><code class="grammar">xyz</code><sub>NR</sub> means the characters <code>xyz</code> literally,
and <code>xyz</code> is not a reserved keyword</li>
<li><code>symbol </code>means a reference to production for the nonterminal
<code>symbol</code></li>
<li><code>x|y</code> means x or y</li>
<li><code>x&y</code> means x and y, interleaved in any order</li>
<li><code>[x]</code> means zero or one times</li>
<li><code>x?</code> means x zero or one times</li>
<li><code>x*</code> means x zero or more times</li>
<li><code>x+</code> means x one or more times</li>
<li><code>(x)</code> means x (grouping)</li>
</ul>
<p>
The <code>rhs</code> of a symbol that starts with a lower-case letter implicitly allows white
space and comments, as defined by the production <code>TokenWhiteSpace</code>,
between the terminals and nonterminals that it references.
</p>
</section>
<section>
<h2 id="program_structure">3. Program structure</h2>
<p>
A Ballerina program is divided into modules. A module has a source form and a
binary form. The source form of a module consists of an ordered collection of
one or more source parts; each source part is a sequence of bytes that is the
UTF-8 encoding of part of the source code for the module. The format of a source
part is defined by this specification. The format of a binary module is
specified by the Ballerina platform.
</p>
<p>
A source module can reference other modules. Each source module can be
separately compiled into a binary module: compilation of a source module needs
access only to the binary form of other modules referenced from the source
module. A source module identifies each module that it references using an
organization name and a module name, which is divided into one or more parts.
Both the organization name and each part of the module name are Unicode strings.
Any organization name starting with the string <code>ballerina</code> is
reserved for use by the Ballerina platform.
</p>
<p>
The Ballerina platform defines a packaging system for Ballerina modules, which
allows one or more modules to be combined into a package. The packaging system
treats the first part of each module's name as being a package name. All the
modules combined into a package share the same package name. Packages have both
a source and a binary format. The source format stores the source form of a
package's modules in a hierarchical filesystem. The binary format stores the
binary form of a package's module as a sequence of bytes.
</p>
<p>
Binary packages can be stored in a package repository. Packages are versioned;
versions are semantic, as described in the SemVer specification. A package
repository can store multiple versions of the same package. Thus, within a
repository, binary packages are organized into a three-level hierarchy:
</p>
<ol>
<li>organization;</li>
<li>package name;</li>
<li>version.</li>
</ol>
<p>
The source format of a package includes a <code>Ballerina.toml</code> file that
allows control over the package versions used for referenced modules.
</p>
<p>
The packaging system also allows control over which modules are exported from a
package; modules that are not exported from a package are visible only to
modules within the package.
</p>
</section>
<section>
<h2 id="lexical_structure">4. Lexical structure</h2>
<p>
The grammar in this document specifies how a sequence of Unicode code points is
interpreted as part of the source of a Ballerina module. A Ballerina module part
is a sequence of octets (8-bit bytes); this sequence of octets is interpreted as
the UTF-8 encoding of a sequence of code points and must comply with the
requirements of RFC 3629.
</p>
<p>
After the sequence of octets is decoded from UTF-8, the following two
transformations must be performed before it is parsed using the grammar in this
document:
</p>
<ul>
<li>if the sequence starts with a byte order mark (code point 0xFEFF), it must
be removed</li>
<li>newlines are normalized as follows:
<ul>
<li>the two character sequence 0xD 0xA is replaced by 0xA</li>
<li>a single 0xD character that is not followed by 0xD is replaced by 0xA</li>
</ul>
</li>
</ul>
<p>
The sequence of code points must not contain any of the following disallowed
code points:
</p>
<ul>
<li>surrogates (0xD800 to 0xDFFF)</li>
<li>non-characters (the 66 code points that Unicode designates as
non-characters)</li>
<li>C0 control characters (0x0 to 0x1F and 0x1F) other than white space (0x9,
0xA, 0xC, 0xD)</li>
<li>C1 control characters (0x80 to 0x9F)</li>
</ul>
<p>
Note that the grammar notation ^X does not allow the above disallowed code
points.
</p>
<pre
class="grammar">identifier := UnquotedIdentifier | QuotedIdentifier
UnquotedIdentifier := (IdentifierInitialChar | IdentifierEscape) (IdentifierFollowingChar | IdentifierEscape)*
QuotedIdentifier := <code>'</code> (IdentifierFollowingChar | IdentifierEscape)+
IdentifierInitialChar := AsciiLetter | <code>_</code> | UnicodeIdentifierChar
IdentifierFollowingChar := IdentifierInitialChar | Digit
IdentifierEscape := IdentifierSingleEscape | NumericEscape
IdentifierSingleEscape := <code>\</code> ^ ( AsciiLetter | 0x9 | 0xA | 0xD | UnicodePatternWhiteSpaceChar )
NumericEscape := <code>\u{</code> CodePoint <code>}</code>
CodePoint := HexDigit+
AsciiLetter := <code>A</code> .. <code>Z</code> | <code>a</code> .. <code>z</code>
UnicodeIdentifierChar := ^ ( AsciiChar | UnicodeNonIdentifierChar )
AsciiChar := 0x0 .. 0x7F
UnicodeNonIdentifierChar :=
UnicodePrivateUseChar
| UnicodePatternWhiteSpaceChar
| UnicodePatternSyntaxChar
UnicodePrivateUseChar :=
0xE000 .. 0xF8FF
| 0xF0000 .. 0xFFFFD
| 0x100000 .. 0x10FFFD
UnicodePatternWhiteSpaceChar := 0x200E | 0x200F | 0x2028 | 0x2029
UnicodePatternSyntaxChar := <em>character with Unicode property Pattern_Syntax=True</em>
Digit := <code>0</code> .. <code>9</code>
</pre>
<p>
Note that the set of characters allowed in identifiers follows the requirements
of Unicode TR31 for immutable identifiers; the set of characters is immutable in
the sense that it does not change between Unicode versions.
</p>
<p>
The <code>QuotedIdentifier</code> syntax allows a reserved keyword
<code><var>K</var></code> can be used as an identifier by preceding it with a
single quote i.e. <code>'<var>K</var></code>. The <code>IdentifierEscape</code>
syntax allows an arbitrary non-empty string to be treated as an identifier. In a
<code>NumericEscape</code>, <code>CodePoint</code> must valid Unicode code
point; more precisely, it must be a hexadecimal numeral denoting an integer
<em>n</em> where 0 ≤ <em>n</em> < 0xD800 or 0xDFFF < n ≤
0x10FFFF.
</p>
<pre
class="grammar">RestrictedIdentifier := AsciiLetter RestrictedFollowingChar* RestrictedIdentifierWord*
RestrictedIdentifierWord := <code>_</code> RestrictedFollowingChar+
RestrictedFollowingChar := AsciiLetter | Digit
</pre>
<p>
Identifiers used for the names of organizations and modules are restricted to
<code>RestrictedIdentifier</code>.
</p>
<pre
class="grammar">TokenWhiteSpace := (Comment | WhiteSpaceChar)*
Comment := <code>//</code> AnyCharButNewline*
AnyCharButNewline := ^ 0xA
WhiteSpaceChar := 0x9 | 0xA | 0xD | 0x20
</pre>
<p>
<code>TokenWhiteSpace</code> is implicitly allowed on the right hand side of
productions for non-terminals whose names start with a lower-case letter.
</p>
<pre
class="grammar">NoSpaceColon := <code>:</code>
</pre>
<p>
When <code>NoSpaceColon</code> is used in a production,
<code>TokenWhiteSpace</code> is not allowed immediately before or after the
colon. When a literal <code>:</code> is used in a production, white space is
handled in the same was as for any other character.
</p>
</section>
<section>
<h2 id="values_types">5. Values, types and variables</h2>
<section>
<h3>Overview</h3>
<section>
<h4>Type system fundamentals</h4>
<p>
Ballerina programs operate on a rich universe of values. This universe of values
is partitioned into a number of <em>basic types</em>; every value belongs to
exactly one basic type.
</p>
<p>
Values are of four kinds, each corresponding to a kind of basic type:
</p>
<ul>
<li>simple values, like booleans and floating point numbers, which are not
composed from other values;</li>
<li>structured values, like mappings and lists, which contain values of arbitrary basic types;</li>
<li>sequence values, which consists of sequences of values of the same basic type;</li>
<li>behavioral values, like functions and objects, which are not just data.</li>
</ul>
<p>
There is a fundamental distinction between values that have a <em>storage
identity</em> and values that do not. A value that has storage identity has an
identity that comes from the location where the value is stored. All structural
and behavioural values have a storage identity, whereas all simple values
do not. Storage identity for sequence values is more complicated and will be
explained in the section on sequence values.
</p>
<p>
Values can be stored in variables or as members of structures or in constituents
of sequences. When a value has no storage identity, it can be stored directly in
the variable, structure or sequence. However, when a value has storage identity,
what is stored in the variable, structure or sequence is a reference to the
location where the value is stored rather than the value itself. Storage
identity allows values in Ballerina to represent not just trees but graphs.
</p>
<p>
Ballerina provides the ability to test whether two values have the same storage
identity, but does not expose the specific storage location of a value. For
values with storage identity, there is the concept of creating a <em>new</em>
value: this means creating a value that has a storage identity that is different
from any existing value. For values with storage identity, there is also the
concept of <em>copying</em>: it means to create a value that is the same, except
for having a new storage identity. The concept of having storage identity is
similar to the concept of a reference type in some other programming languages,
but also accomodates the concept of a sequence value.
</p>
<p>
Ballerina programs use types to categorize values both at compile-time and
runtime. Types deal with an abstraction of values that does not consider storage
identity. This abstraction is called a <em>shape</em>. A type denotes a set of
shapes. Subtyping in Ballerina is <em>semantic</em>: a type S is a subtype of
type T if the set of shapes denoted by S is a subset of the set of shapes
denoted by T. Every value has a corresponding shape. A shape is specific to a
basic type: if two values have different basic types, then they have different
shapes. The shape of the values contained in a structured value are part of the
shape of the structured value. Since shapes do not deal with storage identity,
they represent trees rather graphs. For simple values, there is no difference
between a shape and a value, with the exception of floating point values where
the shape does not consider representation details that do not affect the
mathematical value being represented.
</p>
<p>
A value is <em>plain data</em> if it is a simple value, a sequence value, or a
structured value that does not contain a behavioral value at any depth.
More precisely, a value is defined to be <em>plain data</em> if it is
</p>
<ul>
<li>a simple value,</li>
<li>a sequence value,</li>
<li>a structured value, all of whose members are also plain data.</li>
</ul>
<p>
Plain data values can in general contain cycles of references, but in some
contexts are restricted to be acyclic. Plain data values, including values with
cycles, can be compared for equality.
</p>
<p>
The two most important kinds of behavioural values are functions and objects. A
function value can be executed by calling it; when a function is called, it is
passed values and arguments and returns a value. An object encapsulates data
with functions that operate on the data: an object's members are divided into
fields, which hold the data, and methods, which are the functions that operate
on the data.
</p>
</section>
<section>
<h4>Mutation</h4>
<p>
There are two kinds of things that can be mutated in Ballerina: variables and
values. Mutation of values is tied to storage identity: mutation is only
possible for values with storage identity. When a value stored in some storage
location is mutated, the change will be visible through all variables referring
to the value in that location. But not all values with storage identity can be
mutated: a value may not support mutation even though it has a storage identity.
</p>
<p>
The possibility of mutation gives rise to two relations between a value and a
type:
</p>
<ul>
<li>a value <em>looks like</em> a type at a particular point in the execution of
a program if its shape at that point is a member of the type;</li>
<li>a value <em>belongs to</em> a type if it looks like the type, and it will
necessarily continue to look like the type no matter how the value is mutated.</li>
</ul>
<p>
If a value cannot be mutated, looking like a type and belonging to a type are
the same thing.
</p>
<p>
When a Ballerina program declares a variable to have a compile-time type, this
means that the Ballerina compiler together with the runtime system will ensure
that the variable will only ever hold a value that belongs to the type.
Ballerina also provides mechanisms that take a value that looks like a type and
use it to create a value that belongs to a type.
</p>
<p>
Every value has a read-only bit. If the read-only bit is on, it means that the
value is immutable. A value's read-only bit is fixed when the value is
constructed, and cannot be changed thereafter. Ballerina maintains the invariant
that immutability is deep: any value reachable from a value with its read-only
bit set is guaranteed to have its read-only bit set. Here <em>reachable</em>
means reachable through read operations: every member of a structure value is
reachable from the structure value; every constituent of a sequence value is
reachable from the sequence value; every member of an object value is reachable
from the object value. Reachability is transitive: if <var>t</var> is reachable
from <var>s</var>, and <var>s</var> is reachable from <var>r</var>, then
<var>t</var> is reachable from <var>r</var>. Reachability is also considered
reflexive: a value is reachable from itself.
</p>
<p>
Some basic types are inherently immutable: the read-only bit is always on for a
value that belongs to an inherently immutable basic type. All simple types are
inherently immutable as are functions. Some basic types are selectively
immutable: a type is selectively immutable if it is possible to construct both
values of the type that have the read-only bit on and values that do not have
the read-only bit on. All structured types are selectively immutable as are
objects. Finally, some basic types are inherently mutable: the read-only bit is
never on for a value belonging to an inherently mutable basic type.
</p>
<p>
Each selectively immutable basic type can be partitioned into two
<em>uniform</em> types, one containing values with the read-only bit on, and one
containing values where the read-only bit is off. For every other basic type,
there is a single uniform type. Every uniform type is thus either completely
readonly or completely mutable; every value thus belongs to exactly one uniform
type, and mutation cannot change the uniform type to which a value belongs.
</p>
<p>
It is also possible to limit the mutability of variables, by making them final.
This means that the value that a variable holds cannot be changed after the
variable has been initialized. Unlike immutability of variables, this is not
deep. A final variable can hold a mutable value.
</p>
</section>
<section>
<h4>Isolation</h4>
<p>
There are three possible operations on storage: read, write and execute. The
concept of immutability relates to reading and writing storage, and provides
limited information about execution: execution cannot lead to mutation of an
immutable value. For functions and objects, it is useful to have more
information about how execution may lead to mutation. Ballerina has a concept of
<em>isolation</em> that provides this.
</p>
<p>
In addition to defining when a value is reachable from a value, we can define
when a value is reachable from a variable: a value is reachable from a variable
if the value is reachable from the value that the variable holds. We
can also define when mutable state is reachable from a value or variable: the
mutable state of a value v is reachable from a value or variable, if v is
reachable from the value or variable; the mutable state of a variable v is
reachable only from the variable v.
</p>
<p>
Objects have an isolated bit in addition to a read-only bit; an object value is
isolated if its isolated bit is set. Mutable state is defined to be <em>freely
reachable</em> from a value or variable if it is reachable without following a
reference to an isolated object. A variable or value is an <em>isolated
root</em> if its mutable state is isolated from the rest of the program's
mutable state: any mutable state that is freely reachable from the isolated root
is reachable from outside only through the isolated root. More precisely, if
some mutable state <var>s</var> is freely reachable from an isolated root value
<var>r</var>, then <var>s</var> is not freely reachable from a variable or value
that is not reachable from <var>r</var> except by following a reference through
<var>r</var>; similarly, if some mutable state <var>s</var> is freely reachable
from an isolated root variable <var>r</var>, then <var>s</var> is not freely
reachable from a value that is not reachable from <var>r</var> and is not freely
reachable from any variable other than <var>r</var>. A variable can also be
declared to be isolated. Ballerina maintains the invariant that isolated objects
and isolated variables are isolated roots. Ballerina also guarantees that any
mutable state freely reachable from an isolated object or isolated variable is
accessed only within the scope of a <a href="#lock_statement">lock
statement</a>, which ensures that there is no data race in accessing that
mutable state. This implies that there will be no data race accessing any
mutable state that is reachable (not just freely reachable) from an isolated
object or isolated variable.
</p>
<p>
Functions and methods have an isolated bit in addition to a read-only bit; a
function or method is isolated if its isolated bit is set. Ballerina guarantees
that a call an isolated method or function will only result in access to mutable
state if at least one of the following conditions applies:
</p>
<ul>
<li>the mutable state is freely reachable from an argument passed to the
function or method and the access happens on the strand on which the function is
called; the object on which a method is invoked is considered as an argument for
this purpose;</li>
<li>the mutable state is freely reachable from an isolated variable or an
isolated object;</li>
<li>the mutable state is part of a new value created by the call.</li>
</ul>
<p>
The caller of a function can thus ensure that a function call will not lead to a
data race by ensuring that no data race is possible for the mutable state freely
reachable from the arguments that it passes to the function, for example by
passing only immutable values or isolated objects.
</p>
</section>
<section>
<h4>Type descriptors</h4>
<p>
Ballerina provides a rich variety of type descriptors, which programs use to
describe types. For example, there is a type descriptor for each simple basic
type; there is a type descriptor that describes a type as a union of two types;
there is a type descriptor that uses a single value to describe a type that
contains a single shape. This means that values can look like and belong to
arbitrarily many types, even though they look like or belong to exactly one
<em>basic</em> type.
</p>
<p>
The following table summarizes the type descriptors provided by Ballerina.
</p>
<table>
<tr>
<td><strong>Kind</strong></td>
<td><strong>Name</strong></td>
<td><strong>Set of values denoted by type descriptor</strong></td>
</tr>
<tr>
<td rowspan="5">basic, simple</td>
<td>nil</td>
<td>()</td>
</tr>
<tr>
<td>boolean</td>
<td>true, false</td>
</tr>
<tr>
<td>int</td>
<td>64-bit signed integers</td>
</tr>
<tr>
<td>float</td>
<td>64-bit IEEE 754-2008 binary floating point numbers</td>
</tr>
<tr>
<td>decimal</td>
<td>decimal floating point numbers</td>
</tr>
<tr>
<td rowspan="2">basic, sequence</td>
<td>string</td>
<td>a sequence of Unicode scalar values</td>
</tr>
<tr>
<td>XML</td>
<td>a sequence of zero or more elements, processing instructions, comments or
text items</td>
</tr>
<tr>
<td rowspan="5">basic, structured</td>
<td>array</td>
<td>an ordered list of values, optionally with a specific length, where a
single type is specified for all members of the list</td>
</tr>
<tr>
<td>tuple</td>
<td>an ordered list of values, where a type is specified separately for each
member of the list</td>
</tr>
<tr>
<td>map</td>
<td>a mapping from keys, which are strings, to values; specifies mappings in
terms of a single type to which all keys are mapped</td>
</tr>
<tr>
<td>record</td>
<td>a mapping from keys, which are strings, to values; specifies maps in
terms of names of fields (required keys) and value for each field</td>
</tr>
<tr>
<td>table</td>
<td>a ordered collection of mappings, where a mapping is uniquely identified
within the table by a key derived from the mapping</td>
</tr>
<tr>
<td rowspan="7">basic, behavioral</td>
<td>error</td>
<td>an indication that there has been an error, with a string identifying the
reason for the error, and a mapping giving additional details about the error</td>
</tr>
<tr>
<td>function</td>
<td>a function with 0 or more specified parameter types and a single return
type</td>
</tr>
<tr>
<td>future</td>
<td>a value to be returned by a function execution</td>
</tr>
<tr>
<td>object</td>
<td>a combination of named fields and named methods</td>
</tr>
<tr>
<td>typedesc</td>
<td>a type descriptor</td>
</tr>
<tr>
<td>handle</td>
<td>reference to externally managed storage</td>
</tr>
<tr>
<td>stream</td>
<td>a sequence of values that can be generated lazily</td>
</tr>
<tr>
<td rowspan="11">other</td>
<td>singleton</td>
<td>a single value described by a literal</td>
</tr>
<tr>
<td>readonly</td>
<td>any value whose read-only bit is on</td>
</tr>
<tr>
<td>any</td>
<td>any value other than an error</td>
</tr>
<tr>
<td>never</td>
<td>no value</td>
</tr>
<tr>
<td>optional</td>
<td>a value that is either () or belongs to a type</td>
</tr>
<tr>
<td>union</td>
<td>a value that belongs to at least one of a number of types</td>
</tr>
<tr>
<td>intersection</td>
<td>a value that belongs to all of a number of types</td>
</tr>
<tr>
<td>distinct</td>
<td></td>
</tr>
<tr>
<td>anydata</td>
<td>plain data (a simple value, sequence value or structured value that
does not contain behavioral members at any depth)</td>
</tr>
<tr>
<td>json</td>
<td>the union of (), int, float, decimal, string, and maps and arrays whose
values are, recursively, json</td>
</tr>
<tr>
<td>byte</td>
<td>int in the range 0 to 255 inclusive</td>
</tr>
</table>
<p>
A shape is divided into two <em>aspects</em>: the <em>primary aspect</em> and
the <em>read-only aspect</em>. A value's read-only bit is a part of the
<em>read-only aspect</em> of the value's shape. The read-only bit of values
contained in a structured value is part of the read-only aspect of those values
and of the read-only aspect of the structured value. Everything about a shape
except the read-only bits constitutes the primary aspect of the shape. If two
plain data values compare equal, then the primary aspect of their shapes is the
same, but there may be differences in the read-only aspect.
</p>
<p>
Type descriptors other than <code>readonly</code> describes types using only the
primary aspect of shape: whether a value belongs to the type is not affected by
the read-only aspect of the value's shape. The <code>readonly</code> type uses
only the read-only aspect: whether a value belongs to the <code>readonly</code>
type depends only on the read-only aspect of the value's shape.
</p>
<p>
In addition to describing a type, a type descriptor may also include information
used to construct a value of the type, as well as metadata. Whereas the type
described by a type descriptor is known at compile time, this additional
information may need to be resolved at runtime. The typedesc basic type
represents a type descriptor that has been resolved.
</p>
<pre
class="grammar">type-descriptor :=
simple-type-descriptor
| sequence-type-descriptor
| structured-type-descriptor
| behavioral-type-descriptor
| other-type-descriptor
</pre>
<p>
For simplicity, the type-descriptor grammar is ambiguous. The following table
shows the various types of type descriptor in decreasing order of precedence,
together with associativity.
</p>
<table>
<tr>
<td><strong>Operator</strong></td>
<td><strong>Associativity</strong></td>
</tr>
<tr>
<td><code>distinct T</code></td>
<td></td>
</tr>
<tr>
<td><code>T[]</code><br /><code>T?</code></td>
<td></td>
</tr>
<tr>
<td><code>T1 & T2</code></td>
<td>left</td>
</tr>
<tr>
<td><code>T1 | T2</code></td>
<td>left</td>
</tr>
<tr>
<td><code>function(args) returns T</code></td>
<td>right</td>
</tr>
</table>
</section>
<section>
<h4>Type-ids</h4>
<p>
Ballerina has a feature, called <em>distinct types</em>, which provides
functionality similar to that provided by nominal types, but which works within
Ballerina's structural type system. Distinct types are similar to the branded
types found in some other structurally typed languages, such as Modula-3.
</p>
<p>
The semantics of distinct types are based on type-ids. These are similar to the
brands used by branded types. A distinct type is created by the use of the
<code>distinct</code> keyword in either a <a
href="#distinct_types">distinct-type-descriptor</a> or the
<code>class-type-quals</code> of a <code>module-class-defn</code>. Each such
occurrence of the <code>distinct</code> keyword has a distinct type-id, which
uniquely identifies it within a Ballerina program. A type-id has three parts:
</p>
<ol>
<li>a module id, which identifies the module within which the
distinct-type-descriptor occurs; this consists of an organization, a module
name, and an array of platform-specified strings, which could include
information about, for example, the repository or version of the package the
module comes from;</li>
<li>a local id, which identifies the occurrence of the
<code>distinct</code> within the module; this takes one of two forms:
<ul>
<li>named -
<ul>
<li>if the <code>distinct</code> keyword is part of a distinct-type-descriptor
that is the only distinct-type-descriptor occurring within a module-type-defn,
then the local id is the name of the type defined by the module-type-defn;</li>
<li>if the <code>distinct</code> keyword is part of the class-type-quals
of a module-class-defn, then the local id is the name of the class defined
by the module-class-defn;</li>
</ul>
</li>
<li>anonymous - otherwise, the local id is a compiler-generated integer;</li>
</ul>
</li>
<li>a boolean flag saying whether the type-id is public; this flag is on if and
only if the local id part is named and is the name of a module-type-defn that is
public.</li>
</ol>
<p>
Distinct types can be used with only the object or error basic types. An object
value or error value has a set of type-ids. These type-ids are fixed at the
time of construction and are immutable thereafter. A value's set of type-ids may
be empty. The type-ids of a value are part of the value's shape and so can
affect when an object belongs to a type. The set of type-ids of an object or
error value are divided into primary type-ids and secondary type-ids: the
secondary type-ids could be inferred from the primary type-ids using the
program's source.
</p>
<p>
An object or error value is always constructed using a specific type descriptor.
A type descriptor for objects and errors thus performs a dual role: it denotes a
type and it defines a mechanism to construct a value of the type. A type
descriptor is <em>definite</em> if it induces a specific set of type-ids. The
set of type-ids of an object or error value are those induced by the
type-descriptor used to construct it; such a type descriptor must therefore be
definite. A type descriptor that denotes a type that does not allow object or
error values induces an empty set of type-ids and so is vacuously definite. For
other type descriptors, the section that specifies that type descriptor will say
when it is definite, the set of type-ids that it induces when it is, and which
of those are primary.
</p>
</section>
<section>
<h4>Iterability</h4>
<p>
Values of some basic types are <em>iterable</em>. An iterable value supports an
iteration operation, which treats the iterable value as consisting of a sequence
of zero or more simpler values, which are in some sense a part of the iterable
value; the iteration operation provides the values in the sequence, one after
another. The sequence of values that an iteration operation on a value provides
is the <em>iteration sequence</em> of the value. Each iterable basic type
defines the iteration sequence for a value of that basic type. There is also a
value associated with the completion of the iteration operation, which is nil if
the iteration completed successfully and an error otherwise. The iteration
operation thus determines two associated types for an iterable type: the value
type, which is the type of the values in the iteration sequence, and the
completion type, which is the type of the iteration completion value.
</p>
<p>
The following tables summarizes the iterable basic types.
</p>
<table>
<tr>
<th>Basic type</th>
<th>Iteration sequence</th>
<th>Type descriptor</th>
<th>Value type</th>
<th>Completion type</th>
</tr>
<tr>
<td>string</td>
<td>length 1 substrings</td>
<td><code>string</code></td>
<td><code>string:Char</code></td>
<td><code>()</code></td>
</tr>
<tr>
<td>xml</td>
<td>singleton xml values</td>
<td><code>xml<T></code></td>
<td><code>T</code></td>
<td><code>()</code></td>
</tr>
<tr>
<td>list</td>
<td>members in order</td>
<td><code>T[]</code></td>
<td><code>T</code></td>
<td><code>()</code></td>
</tr>
<tr>
<td>mapping</td>
<td>members</td>
<td><code>map<T></code></td>
<td><code>T</code></td>
<td><code>()</code></td>
</tr>
<tr>
<td>table</td>
<td>members in order</td>
<td><code>table<T></code></td>
<td><code>T</code></td>
<td><code>()</code></td>
</tr>
<tr>
<td>stream</td>
<td>items</td>
<td><code>stream<T,C></code></td>
<td><code>T</code></td>
<td><code>C</code></td>
</tr>
</table>
</section>
</section>
<section>
<h3>Simple values</h3>
<p>
The basic type of a simple value is either one of the following
</p>
<ul>
<li>nil</li>
<li>boolean</li>
<li>int</li>
<li>float</li>
<li>decimal</li>
</ul>
<p>
or is a tagged data type.
</p>
<p>
All simple basic types are inherently immutable.
</p>
<pre
class="grammar">simple-type-descriptor :=
nil-type-descriptor
| boolean-type-descriptor
| int-type-descriptor
| floating-point-type-descriptor
| tagged-data-type-descriptor
</pre>
<p>
The type descriptor for each simple basic type contains all the values of the
basic type.
</p>
<section>
<h4>Nil</h4>
<pre
class="grammar">nil-type-descriptor := nil-literal
nil-literal := <code>(</code> <code>)</code> | <code>null</code>
</pre>
<p>
The nil type contains a single value, called nil, which is used to represent the
absence of any other value. The nil value is written <code>()</code>. The nil
value can also be written <code>null</code>, for compatibility with JSON; the
use of null should be restricted to JSON-related contexts.
</p>
<p>
The nil type is special, in that it is the only basic type that consists of a
single value.
</p>
</section>
<section>
<h4>Boolean</h4>
<pre
class="grammar">boolean-type-descriptor := <code>boolean</code>
boolean-literal := <code>true</code> | <code>false</code>
</pre>
<p>
The boolean type consists of the values true and false.
</p>
</section>
<section>
<h4>Int</h4>
<pre
class="grammar">int-type-descriptor := <code>int</code>
int-literal := DecimalNumber | HexIntLiteral
DecimalNumber := <code>0</code> | NonZeroDigit Digit*
HexIntLiteral := HexIndicator HexNumber
HexNumber := HexDigit+
HexIndicator := <code>0x</code> | <code>0X</code>