Commit 1f5115a3 authored by Kim Nguyễn's avatar Kim Nguyễn
Browse files

add 'return' statements to javascript code.

parent e775469d
......@@ -18,13 +18,13 @@ instance, the JavaScript code of~\eqref{foo} and~\eqref{foo2} in the introductio
typed by using gradual typing:\vspace{-.4mm}
\begin{alltt}\color{darkblue}\morecompact
function foo(x\textcolor{darkred}{ : \pmb{\dyn}}) \{
(typeof(x) === "number")? x++ : x.trim() \refstepcounter{equation} \mbox{\color{black}\rm(\theequation)}\label{foo3}
return (typeof(x) === "number")? x++ : x.trim() \refstepcounter{equation} \mbox{\color{black}\rm(\theequation)}\label{foo3}
\}\negspace
\end{alltt}
``Standard'' gradual typing inserts two dynamic checks since it compiles the code above into:
\begin{alltt}\color{darkblue}\morecompact
function foo(x) \{
(typeof(x) === "number")? (\textcolor{darkred}{\Cast{number}{{\color{darkblue}x}}})++ : (\textcolor{darkred}{\Cast{string}{{\color{darkblue}x}}}).trim()
return (typeof(x) === "number")? (\textcolor{darkred}{\Cast{number}{{\color{darkblue}x}}})++ : (\textcolor{darkred}{\Cast{string}{{\color{darkblue}x}}}).trim()
\}\negspace
\end{alltt}
where {\Cast{$t$}{$e$}} is a type-cast that dynamically checks whether the value returned by $e$ has type $t$.\footnote{Intuitively, \code{\Cast{$t$}{$e$}} is
......
Typescript and Flow are extensions of JavaScript that allow the programmer to specify in the code type annotations used to statically type-check the program. For instance, the following function definition is valid in both languages
\begin{alltt}\color{darkblue}
function foo(x\textcolor{darkred}{ : number | string}) \{
(typeof(x) === "number")? x++ : x.trim() \refstepcounter{equation} \mbox{\color{black}\rm(\theequation)}\label{foo}
return (typeof(x) === "number")? x++ : x.trim(); \refstepcounter{equation} \mbox{\color{black}\rm(\theequation)}\label{foo}
\}
\end{alltt}
Apart from the type annotation (in red) of the function parameter, the above is
......@@ -46,10 +46,12 @@ occurrence typing both in TypeScript and in Flow and serve to give, among other,
more precise types to functions such as \code{foo}. For instance,
since \code{++} returns an integer and \code{trim()} a string, then our
function \code{foo} has type
\code{(number|string)$\to$(number|string)}.\footnote{\label{null}Actually,
both Flow and TypeScript deduce as return type
\code{number|string|void}, since they track when operations may yield \code{void} results. Considering this would be easy but also clutter our presentation, which is why we omit
such details.} But it is clear that a more precise type would be
\code{(number|string)$\to$(number|string)}.
% \footnote{\label{null}Actually,
% both Flow and TypeScript deduce as return type
% \code{number|string|void}, since they track when operations may yield \code{void} results. Considering this would be easy but also clutter our presentation, which is why we omit
% such details.}
But it is clear that a more precise type would be
one that states that \code{foo} returns a number when it is applied to
a number and returns a string when it is applied to a string, so that the type deduced for, say, \code{foo(42)} would be \code{number} rather than \code{number|string}. This is
exactly what the \emph{intersection type}
......@@ -59,7 +61,7 @@ exactly what the \emph{intersection type}
states (intuitively, an expression has an intersection of types, noted \code{\&}, if and only if it has all the types of the intersection) and corresponds in Flow to declaring \code{foo} as follows:
\begin{alltt}\color{darkblue}
var foo : \textcolor{darkred}{(number => number) & (string => string)} = x => \{
(typeof(x) === "number")? x++ : x.trim() \refstepcounter{equation} \mbox{\color{black}\rm(\theequation)}\label{foo2}
return (typeof(x) === "number")? x++ : x.trim(); \refstepcounter{equation} \mbox{\color{black}\rm(\theequation)}\label{foo2}
\}
\end{alltt}
For what concerns negation types, they are pervasive in the occurrence
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment