Type annotation for patterns and expressions

29 views
Skip to first unread message

gmhwxi

unread,
Mar 10, 2018, 1:11:23 PM3/10/18
to ats-lang-users

I have seen so many people tripped on this one.

In the following code, the if-expression is given the type X,
which is checked to be a subtype of 'int':

    val newN: int =
    if nn % 2 = 0 then nn / 2 else 3 * nn + 1

When you do the following later,

    val () = println! ("newN = ", newN)

you will get a type-error message stating that the print function
for newN cannot be found.

Instead, please use the following style of type annotation:

   val newN = (if nn % 2 = 0 then nn / 2 else 3 * nn + 1): int

I hope to find a way to address this issue in ATS3.

Reply all
Reply to author
Forward
0 new messages