elm-tooling / elm-language-server

Language server implementation for Elm
https://www.npmjs.com/package/@elm-tooling/elm-language-server
MIT License
419 stars 65 forks source link

Type inference gets confused by aliases #362

Open miniBill opened 3 years ago

miniBill commented 3 years ago

Consider these two modules:

module LocalizedHtml exposing (Html, Language(..), div, text)

import Html

type Language
    = Italian
    | English

type alias Html a =
    Language -> Html.Html a

type alias Attribute a =
    Language -> Html.Attribute a

div : List (Attribute msg) -> List (Html msg) -> Html msg
div attributes children language =
    Html.div (List.map (\a -> a language) attributes) (List.map (\c -> c language) children)

text : String -> String -> Html msg
text en it language =
    Html.text <|
        case language of
            English ->
                en

            Italian ->
                it

and

module Main exposing (main)

import LocalizedHtml exposing (Html, Language(..), div, text)

main =
    div [] [ hello, world ] English

hello : Html msg
hello =
    text "Hello" "Ciao"

world =
    text "World" "Mondo"

notice how Main does not import Html.

Expected Behavior

The type of world is inferred to be Html msg.

Current Behavior

The type of world is inferred to be Language -> Html msg.

Context

I'm wrapping elm-ui to add translations.

razzeee commented 3 years ago

@jmbockhorst can this be closed?

jmbockhorst commented 3 years ago

No, I had to "unfix" this. The type inference is technically correct in this case, but it can maybe be rendered different because of the aliases. It's not a simple solution without adding more to the inference algorithm.