Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Next version! #732

Merged
merged 224 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
224 commits
Select commit Hold shift + click to select a range
58701af
make type system more nominal, except for unions
hishamhm Oct 21, 2023
79c9b09
don't infer table literal as the first table type of a union
hishamhm Sep 15, 2023
c46dee5
macroexp: initial commit
hishamhm Aug 18, 2023
d36733c
macroexp: type check expression body
hishamhm Nov 18, 2023
800cc79
macroexp: check that arguments are used only once
hishamhm Sep 16, 2023
8d11028
__is: mark as a valid metamethod
hishamhm Aug 22, 2023
11acc34
__is: accept unions over multiple tables that declare `__is` metamethod
hishamhm Aug 22, 2023
7decf46
__is: add macroexp tests for is operator
hishamhm Oct 21, 2023
e3bf9fd
accept indexing a union when index is the same is all entries
hishamhm Aug 22, 2023
bf791d1
refactor: use __is and macroexps in Fact
hishamhm Aug 27, 2023
a40d062
refactor: use Where union, rename error functions
hishamhm Sep 2, 2023
621a3f0
add magic type @self
hishamhm Nov 19, 2023
db02a91
interfaces: add "interface" syntax
hishamhm Sep 24, 2023
d34d144
interfaces: add "is <interface_list>" to record syntax
hishamhm Sep 24, 2023
0b494bf
interfaces: is_record_type
hishamhm Sep 24, 2023
8f20b6e
interfaces: implement show_type
hishamhm Sep 24, 2023
559d6c2
interfaces: add "where <expression>" syntax
hishamhm Sep 24, 2023
9726088
interfaces/records: move arrayrecord to `is {...}`
hishamhm Sep 24, 2023
27124cf
refactor: use `where` clause in fact interfaces
hishamhm Oct 1, 2023
bdc937f
remove arrayrecord: now defined as array interface in records
hishamhm Nov 20, 2023
bf75403
interfaces: prevent assignment to interfaces and types
hishamhm Nov 20, 2023
9a5aeb4
optional arity: add syntax for optional arity in function arguments
hishamhm Jan 26, 2021
f034cf1
optional arity: calculate minimum arity for a function
hishamhm Jan 26, 2021
d62c0e1
optional arity: prepare error messages for optional arity
hishamhm Jan 26, 2021
997d289
optional arity: adapt the compiler to specify optional arities
hishamhm Jan 29, 2021
51a0be2
optional arity: fix arguments in some tests
hishamhm Jan 27, 2021
44bdf19
optional arity: add some optionals
hishamhm Jan 29, 2021
7f3fd80
optional arity: enable optional arity checks!
hishamhm Jan 27, 2021
3583627
refactor: move method downgrade to type-checking time
hishamhm Nov 23, 2023
3b25cdb
refactor: assert_is_a uses Where, not Node
hishamhm Nov 27, 2023
3a81bd0
remove "tl build"
hishamhm Nov 30, 2023
4896aee
remove node.type
hishamhm Nov 28, 2023
5658b9a
add node.debug_type for debugging only
hishamhm Dec 6, 2023
c54e4f5
refactor type relation functions is_a and same_type
hishamhm Dec 8, 2023
8a49055
remove "nestedtype"
hishamhm Dec 9, 2023
988e45c
no need to clear tk when uniting flattening constants
hishamhm Dec 10, 2023
fe33c75
avoid setting (or updating!) typename directly
hishamhm Dec 11, 2023
be6a19e
interfaces: fix error location
hishamhm Dec 11, 2023
8aaf3de
macroexp: require `return` in statement
hishamhm Dec 11, 2023
94a8368
fix: define typeargs in function definition for recursion
hishamhm Jan 4, 2024
5aebc64
local macroexp: new statement
hishamhm Dec 11, 2023
b55294f
macroexp: adjust yend
hishamhm Dec 11, 2023
180394b
remove node.rtype
hishamhm Dec 13, 2023
80e41bd
improve ipairs error message
hishamhm Dec 14, 2023
b428197
be more careful with typeid when shallow-copying types
hishamhm Dec 13, 2023
660f741
refactor: flatten tuple
hishamhm Dec 14, 2023
f651782
abstract checks for macroexp and interfaces
hishamhm Dec 11, 2023
e3a22d8
fixup use macroexp
hishamhm Dec 14, 2023
2780503
refactor: reduce confusion in Type instances
hishamhm Dec 14, 2023
305ef38
tl types: produce types for for-in variables
hishamhm Dec 14, 2023
0f8311c
visit_type: minor refactor
hishamhm Dec 14, 2023
d9117f0
interfaces: expand field list
hishamhm Dec 28, 2023
da234d5
interfaces: subtype checking
hishamhm Dec 28, 2023
f7ac7de
interface constraints in type arguments
hishamhm Dec 28, 2023
f5ea5e5
match record keys in constrained type variables
hishamhm Dec 28, 2023
179e669
arg_check: explicit variance rules
hishamhm Dec 28, 2023
9e99670
wip subtype interface rework
hishamhm Dec 28, 2023
a357282
refactor: display_typevar
hishamhm Dec 29, 2023
2dbdefa
resolve_interface_type
hishamhm Dec 29, 2023
5c0f475
EnumType: begin use interface subtyping!
hishamhm Dec 29, 2023
7d9434a
narrow interfaces on assignment
hishamhm Dec 29, 2023
69a5393
refactor: simplify interface_type to constraint
hishamhm Dec 29, 2023
f0ad383
do not resolve constraint too early
hishamhm Dec 29, 2023
45d20e9
array: rename constant types to consttypes
hishamhm Dec 29, 2023
0fbb2d7
minor tweaks
hishamhm Dec 29, 2023
0428560
FunctionType, PolyType, UnionType, TupleTableType
hishamhm Dec 29, 2023
318c7b7
EmptyTableType, UnresolvedEmptyTableValueType
hishamhm Dec 29, 2023
30650c3
MapType (includes fix in expand_type)
hishamhm Dec 29, 2023
b6534e5
TypeVarType, TypeArgType, UnresolvedTypeArgType, UnresolvableTypeArgType
hishamhm Dec 29, 2023
36e8ea3
subtype-match a typearg using its constraint
hishamhm Dec 30, 2023
c54a112
more efficient nominal check
hishamhm Dec 30, 2023
0954829
TupleType, InvalidType
hishamhm Dec 31, 2023
af519c6
simplify is_total check a bit
hishamhm Jan 1, 2024
5768abf
remove bad_nominal type, just use invalid
hishamhm Jan 1, 2024
fd02195
refactor: move show_fields out
hishamhm Jan 1, 2024
cc9c029
parse_newtype: preparatory refactor
hishamhm Jan 3, 2024
f556501
refactor: declname and is_alias_node
hishamhm Jan 3, 2024
9845b79
add typealias type, split from typetype
hishamhm Jan 3, 2024
19a3a04
remove Node.is_alias_node
hishamhm Jan 4, 2024
30739c3
NominalType, InterfaceType, RecordType, ArrayType
hishamhm Jan 6, 2024
94059b9
UnresolvedType
hishamhm Jan 6, 2024
476573b
LiteralTableItemType
hishamhm Jan 6, 2024
25e667e
simplify function argument traversal
hishamhm Jan 7, 2024
0938c21
TypeAliasType
hishamhm Jan 7, 2024
4293ee1
TypeDeclType
hishamhm Jan 7, 2024
7031282
StringType
hishamhm Jan 7, 2024
69f7a4b
use interface subtyping for Fact
hishamhm Jan 7, 2024
55be243
remove is_self from Type
hishamhm Jan 7, 2024
b2fe2c9
fix: may add meta_fields when expanding interface
hishamhm Jan 7, 2024
fd7538f
compute min_arity during parsing, not type checking
hishamhm Jan 7, 2024
20fa3d8
tl.new_env: simpler, more extensible API
hishamhm Jan 8, 2024
59e5fe1
tl: use new_env
hishamhm Jan 8, 2024
94cf23c
language flag: feat-arity=on/off
hishamhm Jan 8, 2024
a5e1415
standard library definition written in Teal
hishamhm Jan 7, 2024
8f8408e
fix select signature
fperrad Jan 8, 2024
93126f4
Makefile: allow testing with different interpreter values
hishamhm Jan 9, 2024
aed4035
typecodes: drop mask entries
hishamhm Jan 9, 2024
c17be97
TypeReporter and TypeCollector objects
hishamhm Aug 21, 2024
e7a1966
Where: make it an interface
hishamhm Jan 10, 2024
ea7d841
Type: remove xend and yend
hishamhm Jan 10, 2024
cbbb8f1
fix: resolves 'or' to the larger type
hishamhm Jan 14, 2024
d79e47f
refactor: use to_structural to resolve nominals
hishamhm Jan 15, 2024
4487f80
this block is no longer needed
hishamhm Jan 15, 2024
84c5b8d
show typeargs when displaying record names
hishamhm Jan 15, 2024
867a659
more integer in stdlib signatures
fperrad Jan 8, 2024
123635b
standard library: add missing entries
hishamhm Jan 8, 2024
25f64b5
_G: reserve a typeid
hishamhm Jan 15, 2024
7ecb0bc
fix typealias forwarding logic
hishamhm Jan 15, 2024
946f2b7
union_type: nominals should be already resolved here
hishamhm Jan 11, 2024
775cc2a
drop validate_union, just use is_valid_union
hishamhm Jan 11, 2024
bba12c4
unite: removing duplicates via t.found should be enough
hishamhm Jan 11, 2024
c2dc259
refactor: simplify signature of type_check_function_call a bit
hishamhm Jan 12, 2024
5a6fb5f
move module_name logic out of type_check
hishamhm Jan 15, 2024
52aa5f6
all prefixed errors have known locations
hishamhm Jan 16, 2024
8793fe5
attribute <total> now can only apply to literal tables
hishamhm Jan 16, 2024
c6b0302
interfaces: actually check interfaces!
hishamhm Jan 18, 2024
0d3c682
big code reorganization: TypeChecker record
hishamhm Jan 24, 2024
4ead134
fix: check if for iterator does not return values
hishamhm Jan 26, 2024
30515ef
fix: lax arity of returns in function literals
hishamhm Jan 26, 2024
9701800
fix: let lax mode perform emptytable key-value inference as normal
hishamhm Jan 26, 2024
705b61e
fix 'where'/macroexp behavior with type arguments
hishamhm May 13, 2024
3fb26dc
interfaces: fix collect_interfaces
hishamhm Jun 1, 2024
5bb3716
total: do not consider a record function to be a missing field
hishamhm Jun 1, 2024
823a893
total: ignore metamethods when checking for record totality
hishamhm Jun 12, 2024
da75aa2
Accept __lt and __le for > and >= operators.
hishamhm Jun 14, 2024
73f104f
fix: can use nested records in global 'table' record
hishamhm Jul 19, 2024
90f2a1e
fix: do not crash when comparing type defined with 'function'
hishamhm Jul 19, 2024
8d6b589
accept 'or' as a boolean in if/while/repeat expressions
hishamhm Jul 19, 2024
e85ac75
we can now reexport nested types
hishamhm Jul 23, 2024
15c1bff
add the code output to the io.open function (#769)
V1K1NGbg Jul 26, 2024
9326efd
fix regression in bivariant arg check
hishamhm Jul 23, 2024
d121f2e
can now apply '#' on enum values
hishamhm Jul 23, 2024
222b8c1
API: tl.pretty_print_ast third argument is optional
hishamhm Jul 23, 2024
a1dcf79
tests: add regression test to function call check crash
hishamhm Jul 26, 2024
3995c24
standard library: special-case tuple support for table.unpack
hishamhm Aug 1, 2024
91afcc8
docs/tutorial.md: fix typo
hishamhm Aug 5, 2024
d0856b5
tl types: do not crash if given input file doesn't exist
hishamhm Aug 5, 2024
5f85333
local type require() accepts dot notation for nested record
hishamhm Aug 5, 2024
448fe84
fix: delay resolution of type arguments
hishamhm Aug 7, 2024
9736418
stricter checks for shadowing of type arguments
hishamhm Aug 7, 2024
49c2f4e
refactor: rename "declaration" narrowing to "localizing"
hishamhm Aug 7, 2024
ccb5081
fix: localizing a record does not make the new local a type
hishamhm Aug 7, 2024
7ae8974
tests: add regression test for #749
hishamhm Aug 7, 2024
877e785
only check against shadowing base types when declaring types
hishamhm Aug 11, 2024
71cc244
interfaces: enforce them to be abstract
hishamhm Aug 7, 2024
93c83ba
fix: do not close nested types too early
hishamhm Aug 13, 2024
1d2dffd
fix: avoid crash in `is` check
hishamhm Aug 15, 2024
06d6778
fix: string equality check
hishamhm Aug 16, 2024
8b0fe09
use unknown only in lax mode
hishamhm Aug 8, 2024
7cbedd2
minor tweak: no need to copy non-method functions
hishamhm Aug 8, 2024
0478bd4
Type: add __tostring metamethod
hishamhm Aug 8, 2024
f8d5665
no need to type_at(a_typedecl())
hishamhm Aug 8, 2024
bbe6c07
begin cleaning up generics in type declarations
hishamhm Aug 18, 2024
a12249f
internal debugging improvements: identify named types
hishamhm Aug 19, 2024
f84fede
fix propagation of type arguments
hishamhm Aug 19, 2024
4a829b0
self: introduce self type
hishamhm Aug 8, 2024
b6d5d7b
self: use self type
hishamhm Aug 8, 2024
30e603a
self: remove magic behavior for nominal "@self"
hishamhm Aug 8, 2024
1b27f3e
tests: add test case using self
hishamhm Aug 19, 2024
333b9e8
fix: self heuristic: ensure that non-nominal types are detected
hishamhm Aug 19, 2024
a4a780f
fix: do not crash when failing to read input
hishamhm Aug 19, 2024
8c2d4c6
self: fix: accept self type in function signature as a method indicator
hishamhm Aug 19, 2024
ee5c8fc
tests: add regression test for #752.
hishamhm Aug 19, 2024
2ff0df4
tests: split `local` and `local type` tests
hishamhm Aug 21, 2024
6430aaa
tests: add regression test for #754
hishamhm Aug 21, 2024
234ee2a
fix: tl types: never trigger ICE on bad files
hishamhm Aug 26, 2024
d12f534
fix: do not infer type variables as boolean in boolean contexts
hishamhm Aug 26, 2024
b8b5bb8
generate inline compat code for table.pack
hishamhm Aug 28, 2024
7a6a19f
pragma: introduce --#pragma syntax
hishamhm Jun 16, 2024
1b12d76
pragma: arity on/off
hishamhm Jun 16, 2024
e5ee053
docs: pragmas (#798)
hishamhm Sep 2, 2024
6719333
docs: explain type aliasing (#799)
hishamhm Sep 2, 2024
a84ac18
fix: resolve type aliases as early as possible
hishamhm Sep 1, 2024
dc15bc4
lexer: # is an operator
hishamhm Sep 2, 2024
1dd8f9e
optional arity: lax arg check in functions with feat-arity off
hishamhm Sep 1, 2024
e49ac8b
fix: do not generate Lua table for interfaces
hishamhm Sep 1, 2024
d04fa0f
pragma: ignores other --# lines
hishamhm Sep 2, 2024
3cc78b6
lexer: simplify TokenKind
hishamhm Sep 2, 2024
fcdd861
metatables: check metamethod types in metatable definition
hishamhm Sep 3, 2024
fe86b72
compiler debugging: TL_DEBUG_FACTS environment variable
hishamhm Sep 3, 2024
ed93fb3
generics: begin generalizing code with HasTypeArgs
hishamhm Sep 3, 2024
86b01a7
facts: avoid generating dummy And facts
hishamhm Sep 3, 2024
6861032
parser: we don't need enum_item
hishamhm Sep 4, 2024
311fcd1
parser: refactor special behaviors in type declarations
hishamhm Sep 4, 2024
6d6d5d4
parser: minor refactor
hishamhm Sep 4, 2024
f5a1d51
parser: refactor setting declname
hishamhm Sep 4, 2024
73faece
parser: simplify parse_type_constructor signature
hishamhm Sep 4, 2024
9a89cf0
parser: refactor parse_type_body
hishamhm Sep 4, 2024
98044e3
minor cleanup
hishamhm Sep 4, 2024
8f89ec7
refactor: remove TypeAliasType (unify with TypeDeclType)
hishamhm Sep 4, 2024
64de559
API: expose Teal 0.15.x-like API for backwards compatibility
hishamhm Sep 1, 2024
65a904e
API: expose Token record and Node abstract interface
hishamhm Sep 1, 2024
775ceda
funcall: minor simplification
hishamhm Sep 5, 2024
ae05702
typevar_resolver: drop unused argument
hishamhm Sep 6, 2024
34193b7
funcall: narrow type of check_call
hishamhm Sep 5, 2024
96baa3d
funcall: feat_lax=on implies feat_arity=off
hishamhm Sep 5, 2024
bcaac58
funcall: split logic for FunctionType and PolyType
hishamhm Sep 5, 2024
033681e
infer_at: minor cleanup
hishamhm Sep 6, 2024
373e06d
find_type: narrow return type
hishamhm Sep 8, 2024
b783313
refactor: ensure_not_method
hishamhm Sep 9, 2024
cb33692
refactor: are_same_nominals
hishamhm Sep 9, 2024
7a117b9
check_poly_call: simplify use
hishamhm Sep 9, 2024
f157d84
refactor: move {} inference out of assert_is_a, into is_a
hishamhm Sep 10, 2024
9e7c645
tests: add #cli tag in spec/cli tests
hishamhm Sep 10, 2024
ece9e50
pending test: detect mismatch in function generics
hishamhm Sep 10, 2024
1c3ba3d
refactor: store_field_in_record
hishamhm Sep 10, 2024
8f63a86
assert: ignores additional arguments
hishamhm Sep 18, 2024
f0d714e
fix: do not infer typevars on failed poly match
hishamhm Sep 18, 2024
cfc986c
string.gsub: accept functions with no returns
hishamhm Sep 18, 2024
ccd2417
improve inference of nested emptytables
hishamhm Sep 18, 2024
58ebd4b
return: module returns the nominal's type, including typeargs
hishamhm Sep 19, 2024
af5aed5
fix: can assign an emptytable to an emptytable
hishamhm Sep 19, 2024
d8965fa
types: fix reporting of if block end position
hishamhm Sep 19, 2024
b618edb
tests: add testcase for `tl types -p` if-block
hishamhm Sep 19, 2024
6117c8c
types: reports record functions in record field list
hishamhm Sep 21, 2024
78c08f9
fix: do not corrupt record type object when assigning a method
hishamhm Oct 3, 2024
74a44d3
accept 'is userdata' syntax in records
hishamhm Oct 3, 2024
d05df56
fix: macroexp can resolve self for methods
hishamhm Oct 7, 2024
737a987
fix: macroexp resolve metamethods correctly
hishamhm Oct 7, 2024
ab9bf51
types: reports reference of a nominal type
hishamhm Oct 7, 2024
2c5219f
docs: macro expressions
hishamhm Oct 7, 2024
4fa35a4
docs: updates for next release
hishamhm Oct 3, 2024
e289caf
docs: self type
hishamhm Oct 7, 2024
261dacd
docs: prepare changelog for Teal 0.24.0
hishamhm Oct 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,123 @@
# 0.24.0 - Teal Spring '24

2024-10-07

This a big release! It is a culmination of work that has spanned multiple
years, and it would not be possible without the amazing feedback given by
the community on the `next` branch, where this was developed. This release
does include that "lot of new code" that I wanted to merge in the "near
future", as stated in the previous release's changelog.

The main feature is the addition of interfaces, which introduces a model for
subtyping table types in the language. That should allow for representing the
various kinds of object models that are used across the Lua ecosystem, without
promoting any particular class/object system over the others.

This release features commits by François Perrad, Victor Ilchev and Hisham
Muhammad.

## What's New

### Language

* **Interfaces**: you can now declare abstract interfaces, and record types
can implement them. This allows you to declare subtyping relations, and
better support inheritance models (e.g. `local record Circle is Shape`).
* Records and interfaces may declare a `where` clause with an expression
over `self`, which allows the `is` operator to discriminate the type.
* With discriminated record types, it is now possible to declare unions
over multiple record types.
* "Arrayrecords" are no longer a distinct type: they are just records
that implement an array interface (e.g. `local record R is {T}`)
* Type variables in functions can now have constraints on interfaces
(e.g. `function my_func<F is MyInterface>(...)`)
* `self` is now a valid type that can be used when declaring arguments
in functions declared in interfaces and records
* When a record is declared to be a subtype of an interface using `is`,
any function arguments using `self` in the parent interface type will
then resolve to the child record's type.
* **Optional/required arguments in function calls** - functions may declare
arguments as optional, affecting the required arity of function calls.
The rightmost arguments of a function can have their variable names
annotated with a `?` sign, indicating that the argument does not need
to be passed in a function call. This refers only to the presence of the
argument in a call, and not to its type or nullability: a required
argument may still be called with an explicit `null`, but an optional
argument may be elided.
* In previous versions of Teal, all function arguments were effectively
optional. This means that Teal is now stricter which checking for
function calls. You can use `--feat-arity=off` in the command line
or `feat_arity = "off"` in `tlconfig.lua` to obtain the previous
behavior.
* To convert code that uses the old arity checking rules to the new
behavior, you can also use compiler pragmas in the code,
`--#pragma arity off` and `--#pragma arity off` to disable or enable
stricter arity checks.
* **Macro expressions**: you can declare a restricted form of function called
a `macroexp`, which is always expanded inline. These can also be used
to declare compile-time metamethods, which expand without requiring
a metatable at runtime. The `where` clauses used in interfaces and
records are syntax sugar for macro expressions that implement a
pseudo-metamethod `__is`.
* Dynamic `require` calls that do not take a module name as a literal
string are now allowed, and return `any`; you can load a static type
definition using `local type MyType = require("...")` and then cast
your dynamic require like so: `local my_mod = require(var) as MyType`
* The type system is more nominal: all named types are now treated nominally,
except for unions, which are always type aliases. Previously record types
were nominal, but a named typed that resolved to a primitive such as
`integer` was structural. There are still subleties on the rules for when
a `local type` produces a new distinct type or an alias; they are
[explained in the docs](docs/aliasing.md).
* The `<total>` attribute for variables can now only be applied when
initializing variables with literal tables. This is a minor breaking change,
but the usefulness of this attribute in other cases was very limited,
and it also produced misleading results.
* Improved type signatures in the standard library
* `select` produces variadic returns

### API

* Simplified API in the `tl` module.
* The 0.15 API is still supported for backwards compatibility.

### Tooling

* `tl build` was removed. [Cyan](https://github.com/teal-language/cyan)
should be used instead as the build tool for Teal projects.

### Fixes

* Fix commits included in this release refer both to bugs present
in the `master` branch as well as fixes specific to the 0.24 series,
reported by the community during the beta testing of this release.
The Git history and the GitHub issues list contain a more detailed
accounting of the bugfixes that went into this release.
Some of the fixes include:
* `tl check` now reports if the input file does not exist.
* Reporting location of the end of an `if` block correctly.
* No longer crash if a `require()` epression fails to resolve (#778).
* `tl types` now reports the types of variables in `for` loops.
* Improved error message when calling functions with insufficient
arguments.
* Disallowing using a base type as a type variable name.
* Type arguments resolving correctly in recursive functions.
* Type arguments resolving correctly in nested records (#754).
* `or` type inference between types with a subtyping relation
resolves to whichever is the larger type.
* Reporting an error if an iterator used in a generic `for` does not
declare a return value type (#736).
* When checking `<total>` values, no longer reporting record
function and metamethods as missing fields (#749).
* Localizing a record no longer makes the local a type (#759).
* Bad assignments of record tables are reported (#752).
* Nominal type alias declarations work as expected (#238).
* Nominals with generics can be resolved correcty (#777).
* Nested types are not closed too early (#775).
* Not failing when resolving nested empty tables.
* Exporting generics and type aliases from modules correctly
using `return` at the toplevel (#804).

# 0.15.3

2023-11-05
Expand Down
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
LUA ?=

ifeq ($(OS), Windows_NT)
BUSTED = busted.bat --suppress-pending --exclude-tags=unix
else
Expand All @@ -8,8 +10,8 @@ all: selfbuild suite

selfbuild:
cp tl.lua tl.lua.bak
./tl gen --check tl.tl && cp tl.lua tl.lua.1 || { cp tl.lua tl.lua.1; cp tl.lua.bak tl.lua; exit 1; }
./tl gen --check tl.tl && cp tl.lua tl.lua.2 || { cp tl.lua tl.lua.2; cp tl.lua.bak tl.lua; exit 1; }
$(LUA) ./tl gen --check tl.tl && cp tl.lua tl.lua.1 || { cp tl.lua tl.lua.1; cp tl.lua.bak tl.lua; exit 1; }
$(LUA) ./tl gen --check tl.tl && cp tl.lua tl.lua.2 || { cp tl.lua tl.lua.2; cp tl.lua.bak tl.lua; exit 1; }
diff tl.lua.1 tl.lua.2

suite:
Expand Down
145 changes: 145 additions & 0 deletions docs/aliasing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
# Type aliasing rules in Teal

## The general rule

In Teal we can declare new types with user-defined names. These are called
_nominal types_. These nominal types may be unique, or aliases.

The `local type` syntax produces a new _nominal type_. Whenever you assign to
it another user-defined nominal type, it becomes a _type alias_. Whenever you
assign to it a type constructor, it becomes a new unique type. Type
constructors are syntax constructs such as: block constructors for records,
interfaces and enums (e.g. `record` ... `end`); function signature
declarations with `function()`; applications of generics with `<>`-notation;
declarations of array, tuple or map types with `{}`-notation; or a primitive
type name such as `number`.

Syntax such as `local record R` is a shorthand to `local type R = record`, so
the same rules apply: it declares a new unique type.

Nominal types are compared against each other _by name_, but type aliases are
considered to be equivalent.

```lua
local record Point3D
x: number
y: number
z: number
end

local record Vector3D
x: number
y: number
z: number
end

local p: Point3D = { x = 1.0, y = 0.3, z = 2.5 }

local v: Vector3D = p -- Teal compile error: Point3D is not a Vector3D

local type P3D = Point3D

local p2: P3D

p2 = p -- ok! P3D is a type alias type Point3D
p = p2 -- ok! aliasing works both ways: they are effectively the same type
```

Nominal types are compared against non-nominal types _by structure_, so that
you can manipulate concrete values, which have inferred types. For example,
you can assign a plain function to a nominal function type, as long as the
signatures are compatible, and you can assign a number literal to a nominal
number type.

```lua
local type MyFunction = function(number): string

-- f has a nominal type
local f: MyFunction

-- g is inferred a structural type: function(number): string
local g = function(n: number): string
return tostring(n)
end

f = g -- ok! structural matched against nominal
g = f -- ok! nominal matched against structural
```

You can declare structural types for functions explicitly:

```lua
local type MyFunction = function(number): string

-- f has a nominal type
local f: MyFunction

-- h was explicitly given a structural function type
local h: function(n: number): string

f = h -- ok!
h = f -- ok!
```

By design, there is no syntax in Teal for declaring structural record types.

## Some examples

Type aliasing only happens when declaring a new user-defined nominal type
using an existing user-defined nominal type.

```lua
local type Record1 = record
x: integer
y: integer
end

local type Record2 = Record1

local r1: Record1
assert(r1 is Record2) -- ok!
```

This does not apply to primitive types. Declaring a type name with the same
primitive type as a previous declaration is not an aliasing operation. This
allows you to create types based on primitive types which are distinct from
each other.

```lua
local type Temperature = number

local type TemperatureAlias = Temperature

local type Width = number

local temp: Temperature

assert(temp is TemperatureAlias) -- ok!
assert(temp is Width) -- Teal compile error: temp (of type Temperature) can never be a Width
```

Like records, each declaration of a function type in the program source code
represents a distinct type. The `function(...):...` syntax for type
declaration is a type constructor.

```lua
local type Function1 = function(number): string

local type Function2 = function(number): string

local f1: Function1

assert(f1 is Function2) -- Teal compile error: f1 (of type Function2) can never be a Function1
```

However, user-defined nominal names referencing those function types can be
aliased.

```lua
local type Function1 = function(number): string

local type Function3 = Function1

local f1: Function1
assert(f1 is Function3) -- ok!
```
52 changes: 0 additions & 52 deletions docs/build_file.md

This file was deleted.

Loading
Loading