Skip to content

Commit 8bf0623

Browse files
authored
Specify not unsound (#2883)
Reword the rationale paragraph about Dart typing and soundness. The new text indicates that Dart has soundness properties (the old text just said that it was 'unsound', which is unfair). It also indicates that the evolution of Dart has been, and likely will be, in the direction of more static type safety.
1 parent 625f5ba commit 8bf0623

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

specification/dartLangSpec.tex

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20116,15 +20116,31 @@ \section{Types}
2011620116
\LMLabel{types}
2011720117

2011820118
\LMHash{}%
20119-
Dart supports optional typing based on interface types.
20119+
Dart supports static typing based on interface types.
2012020120

2012120121
\rationale{%
20122-
The type system is unsound, due to the covariance of generic classes.
20123-
This is a deliberate choice (and undoubtedly controversial).
20124-
Experience has shown that sound type rules for generics
20125-
fly in the face of programmer intuition.
20126-
It is easy for tools to provide a sound type analysis if they choose,
20127-
which may be useful for tasks like refactoring.%
20122+
The type system is sound in the sense that
20123+
if a variable of type $T$ refers to an object of type $S$ at run time,
20124+
then $S$ is a subtype of $T$.
20125+
In other words, the contents of the heap satisfies the expectations
20126+
expressed by static typing.
20127+
20128+
However, type parameters are covariant
20129+
(e.g., \SubtypeNE{\code{List<int>}}{\code{List<num>}})
20130+
and this implies that certain operations are subject to dynamic type checks
20131+
(such as \code{myList.add(1.5)}, which will throw at run time
20132+
if \code{myList} has declared type \code{List<num>},
20133+
but it is actually a \code{List<int>}).
20134+
Hence, a program can be free of compile-time errors,
20135+
and it may still incur a type error at run time.
20136+
20137+
This choice was made deliberately during the early days of Dart
20138+
(and it is undoubtedly controversial).
20139+
It represents a trade-off where
20140+
the potential for run-time type errors is the cost,
20141+
and the benefit is simpler programs.
20142+
In recent years, Dart has evolved to have more and more static type safety,
20143+
e.g., null safety, and this trend is likely to continue.%
2012820144
}
2012920145

2013020146

0 commit comments

Comments
 (0)