What does comparable mean in Elm?

后端 未结 3 1212
庸人自扰
庸人自扰 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:55

    I think this question can be related to this one. Int and String are both comparable in the sense that strings can be compared to strings and ints can be compared to ints. A function that can take any two comparables would have a signature comparable -> comparable -> ... but within any one evaluation of the function both of the comparables must be of the same type.

    I believe the reason f2 is confusing above is that 1 is a number instead of a concrete type (which seems to stop the compiler from recognizing that the comparable must be of a certain type, probably should be fixed). If you were to do:

    i = 4 // 2
    f1 = (<) i   -- type Int -> Bool
    f2 = (<) "a" -- type String -> Bool
    

    you would see it actually does collapse comparable to the correct type when it can.

提交回复
热议问题