degory / ghul

compiler for the ghūl programming language
https://ghul.dev
GNU Affero General Public License v3.0
4 stars 0 forks source link

Implicit import of union variants #1138

Open degory opened 8 months ago

degory commented 8 months ago

To avoid having to write out potentially wordy qualified union variants and to avoid having to specify union / variant actual generic arguments in contexts where they're implied, we should implicitly import a union's variants into scope where it's clear they will be referenced.

For example within the bodies of functions or methods that return a union type, and within case expression where the value being matched is of a union.


union Option[T] is
    SOME(value: T);
    NONE;
si

maybe_get_half_an_int(i: int) -> Option[int] =>
    if i % 2 == 0 then
        SOME(i / 2) // implicitly Option[int].SOME
    else
        NONE // implicitly Optoin[int].NONE
    fi;

get_int_or_zero(o: Option[int]) -> int =>
    case o
    when SOME(i): i   // implicitly Option[int].SOME
    when NONE: 0    // implicitly Option.SOME
    esac;

We could also implicitly import union variants where a function or method has arguments of union types, provided the variant names are distinct. However this may not be a common use-case.

Depends on #1137 as is essentially implicit use