What does comparable mean in Elm?

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

    UPDATE: thanks to @charlie inputs i include this:

    Compare any two comparable values. Comparable values include String, Char, Int, Float, Time, or a list or tuple containing comparable values. These are also the only values that work as Dict keys or Set members.

    taken from the elm docs: - here

    Older version:

    Comparable types includes numbers, characters, strings,~~ lists of comparable things, and tuples of comparable things. Note that tuples with 7 or more elements are not comparable; why are your tuples so big?

    This means that:

    [(1,"string"), (2, "another string")] : List (Int, String) - is comparable

    But having

    (1, "string", True) : (Int, String, Bool) or

    [(1,True), (2, False)] : List (Int, Bool ) - are not comparable yet.

    This issue is discussed here

    Note: Usually people encounter problems with the comparable type when they try to use a union type as a Key in a Dict.

    Tags and Constructors of union types are not comparable. So the following doesn't even compile.

    type SomeUnion = One | Two | Three
    Dict.fromList [ (One, "one related"), (Two, "two related") ] : Dict SomeUnion String
    

    Usually when you try to do this, there is a better approach to your data structure. But until this gets decided - an AllDict can be used.

提交回复
热议问题