New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bool values cannot be the member of Set #329

Closed
jinjor opened this Issue Aug 4, 2015 · 6 comments

Comments

Projects
None yet
3 participants
@jinjor
Contributor

jinjor commented Aug 4, 2015

I'm facing a problem with making a Set that contains Bool values.
And also, lists or tuples that contains Bool cause the same trouble.

Here is the simple example and the error message.

set = Set.fromList [True]
The 1st argument to function `fromList` has an unexpected type.

4| set = Set.fromList [True]
Note: comparable types include Int, Float, Char, String, lists, and tuples.

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

    comparable

    Bool

I understand what the compiler is saying, but not sure why Set or Dict keys need to be comparable. Should they always be sorted?

I think the possible solutions are...

  • Bool (or other union types) should be comparable
  • there should be another type instead of comparable

Any ideas? Or is there any workaround?

Thanks.

@jvoigtlaender

This comment has been minimized.

Show comment
Hide comment
@jvoigtlaender

jvoigtlaender Aug 4, 2015

Contributor

Set is implemented via Dict, and Dict is implemented via some sort of balanced trees, operations on which do in fact require a total order on entries/keys. So the implementation indeed requires comparable for types that should be put into a Set or used as key in a Dict.

So probably the right feature request here is that Bool should become comparable. Making all union types comparable would be more complicated, but special-casing for Bool could make sense.

Contributor

jvoigtlaender commented Aug 4, 2015

Set is implemented via Dict, and Dict is implemented via some sort of balanced trees, operations on which do in fact require a total order on entries/keys. So the implementation indeed requires comparable for types that should be put into a Set or used as key in a Dict.

So probably the right feature request here is that Bool should become comparable. Making all union types comparable would be more complicated, but special-casing for Bool could make sense.

@evancz

This comment has been minimized.

Show comment
Hide comment
@evancz

evancz Aug 4, 2015

Member

Why do you want a set of booleans? Any case I can think of can also be handled just by booleans.

Member

evancz commented Aug 4, 2015

Why do you want a set of booleans? Any case I can think of can also be handled just by booleans.

@jvoigtlaender

This comment has been minimized.

Show comment
Hide comment
@jvoigtlaender

jvoigtlaender Aug 4, 2015

Contributor

Well, what about a set of tuples, some tuple component of which is a boolean? Certainly that's a valid use case that is not covered by just booleans alone?

Contributor

jvoigtlaender commented Aug 4, 2015

Well, what about a set of tuples, some tuple component of which is a boolean? Certainly that's a valid use case that is not covered by just booleans alone?

@evancz

This comment has been minimized.

Show comment
Hide comment
@evancz

evancz Aug 4, 2015

Member

Ah, I was just looking at the example given. Nested stuff makes sense. Let's hear @jinjor's specific case though.

Member

evancz commented Aug 4, 2015

Ah, I was just looking at the example given. Nested stuff makes sense. Let's hear @jinjor's specific case though.

@jinjor

This comment has been minimized.

Show comment
Hide comment
@jinjor

jinjor Aug 4, 2015

Contributor

Yeah, exactly the case @jvoigtlaender said. (Sorry, the example above is not real one. )

Here is the specific case in my WebRTC app.

type alias Peer = Int
type alias MediaType = String
type alias Upstream = Bool
type alias Connection = (Peer, MediaType, Upstream)

type alias Model =
  {  connections : Set Connection
  ,  videoUrls : Dict Connection String

  ...

  }

Oh, now I found MediaType can also be written as an union type, like type MediaType = Audio | Video.

Contributor

jinjor commented Aug 4, 2015

Yeah, exactly the case @jvoigtlaender said. (Sorry, the example above is not real one. )

Here is the specific case in my WebRTC app.

type alias Peer = Int
type alias MediaType = String
type alias Upstream = Bool
type alias Connection = (Peer, MediaType, Upstream)

type alias Model =
  {  connections : Set Connection
  ,  videoUrls : Dict Connection String

  ...

  }

Oh, now I found MediaType can also be written as an union type, like type MediaType = Audio | Video.

@evancz

This comment has been minimized.

Show comment
Hide comment
@evancz

evancz Aug 5, 2015

Member

Okay, makes sense. I don't know what the short term solution is, but I made an aggregate issue at elm/compiler#1008 so we can keep track of all the issues along these lines. Hopefully the different perspectives will help us come to a good answer.

Member

evancz commented Aug 5, 2015

Okay, makes sense. I don't know what the short term solution is, but I made an aggregate issue at elm/compiler#1008 so we can keep track of all the issues along these lines. Hopefully the different perspectives will help us come to a good answer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment