What does comparable mean in Elm?

后端 未结 3 1258
庸人自扰
庸人自扰 2020-12-17 17:28

I\'m having trouble understanding what exactly a comparable is in Elm. Elm seems as confused as I am.

On the REPL:

> f1 = (<)
&l         


        
3条回答
  •  我在风中等你
    2020-12-17 17:54

    Normally when you see a type variable in a type in Elm, this variable is unconstrained. When you then supply something of a specific type, the variable gets replaced by that specific type:

    -- says you have a function:
    foo : a -> a -> a -> Int
    -- then once you give an value with an actual type to foo, all occurences of `a` are replaced by that type:
    value : Float
    foo value : Float -> Float -> Int
    

    comparable is a type variable with a built-in special meaning. That meaning is that it will only match against "comparable" types, like Int, String and a few others. But otherwise it should behave the same. So I think there is a little bug in the type system, given that you get:

    > f2 "a"
    As I infer the type of values flowing through your program, I see a conflict
    between these two types:
    
        comparable
    
        String
    

    If the bug weren't there, you would get:

    > f2 "a"
    As I infer the type of values flowing through your program, I see a conflict
    between these two types:
    
        Int
    
        String
    

    EDIT: I opened an issue for this bug

提交回复
热议问题