githwxi / ATS-Postiats

ATS2: Unleashing the Potentials of Types and Templates
www.ats-lang.org
Other
352 stars 54 forks source link

Weird tuple behavior #214

Closed antoyo closed 6 years ago

antoyo commented 6 years ago

Hi. It seems there's an issue with tuples when they are inside an if:

fun func(tuple: (int, int)) = {
    val a = tuple.0
    val b = tuple.1
}

implement main0() = {
    val tuple =
        if true then
            (1, 2)
        else
            (3, 4)
    val () = func(tuple)
    val a = tuple.0
    val b = tuple.1
}

The previous code triggers this error:

tuple.dats: 228(line=13, offs=18) -- 230(line=13, offs=20): error(3): [0] cannot be found: the type [S2EVar(1)] is expected to be a tyrec(record).
tuple.dats: 248(line=14, offs=18) -- 250(line=14, offs=20): error(3): [1] cannot be found: the type [S2EVar(1)] is expected to be a tyrec(record).
patsopt(TRANS3): there are [2] errors in total.
exit(ATS): uncaught exception: _2tmp_2ATS_2dPostiats_2src_2pats_error_2esats__FatalErrorExn(1025)

While this code does not:

fun func(tuple: (int, int)) = {
    val a = tuple.0
    val b = tuple.1
}

implement main0() = {
    val tuple = (3, 4)
    val () = func(tuple)
    val a = tuple.0
    val b = tuple.1
}

Thanks to fix this issue.

githwxi commented 6 years ago

Strictly speaking, each if-then-else (or case-of) expression needs a type annotation:

val tuple = ( if ... then ... else ... ) : (int, int)

Sometimes, the type can be inferred, though.

antoyo commented 6 years ago

Ok, thanks. Is it normal that this version does not work as well:

    val tuple: (int, int) =
        if true then
            (1, 2)
        else
            (3, 4)

?

githwxi commented 6 years ago

In the following code, the annotation is for the pattern x (not for e):

val x: T = e

This type annotation means that the type for e is required to be a subtype of T.

antoyo commented 6 years ago

Thanks.