Skip to content
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

Make >, >=, <, <= operate only on Ints #171

Closed
lpil opened this issue May 11, 2019 · 8 comments · Fixed by #207
Closed

Make >, >=, <, <= operate only on Ints #171

lpil opened this issue May 11, 2019 · 8 comments · Fixed by #207
Labels
help wanted Contributions encouraged
Milestone

Comments

@lpil
Copy link
Member

lpil commented May 11, 2019

Order operators current behaviour can be confusing as the automatic ordering may not align with user expectations:

enum Order =
  | Gt
  | Eq
  | Lt
Gt > Lt  // => False

Restrict them to Ints only, and introduce >. etc that operate on Floats.

@lpil lpil added area:compiler discussion The approach has not yet been decided labels May 11, 2019
@MainShayne233
Copy link
Contributor

MainShayne233 commented May 11, 2019

The ability to compare all types in Elixir came up during my company’s last engineering meeting :p

I personally feel that the trade off of being able to accidentally produce incorrect assertions due to a misunderstanding is not worth the limitation this flexibility. I can’t think of a case where comparing values of different types in this way has major advantages over other methods of defining the same control flow or what have you.

@lpil
Copy link
Member Author

lpil commented May 11, 2019

To be clear I'm not suggesting a > b be valid, I'm wondering if > should be restricted from a > b to Int > Int so that enums such as the above could not be compared in a fashion that would be unexpected to the user.

In my example above "less than" is greater than "greater than" because atoms are larger if they are alphabetically larger.

@MainShayne233
Copy link
Contributor

What about Elm's approach to comparables, where the comparison operators can only be used on "numbers, characters, strings, lists of comparable things, and tuples of comparable things"?

@lpil
Copy link
Member Author

lpil commented May 15, 2019

I don't like that Elm has these type classes but doesn't allow the user to implement new members of the class.

Any language features should be usable and extendable by the users rather than just the language designers. More rules with fewer exceptions :)

@mbuhot
Copy link

mbuhot commented May 16, 2019

Could you define a general purpose compare : a -> a -> bool function that will use the beams comparison primitives, and the > operators to be a convenience syntax only for numeric types?

@lpil
Copy link
Member Author

lpil commented May 16, 2019

We could do that. It could also return order:Order, which may be a little clearer.

We couldn't have > work only on numeric types because this would require overloading or a type class again. It could work for ints and there be another >. operator for floats.

@tim2CF
Copy link

tim2CF commented May 21, 2019

Does this discussion mean that Gleam will have type classes? :)
Because type classes is prerequisite for polymorphism generally and Ord type class is prerequisite for polymorphic >, >=, <, <= functions particularly.

@lpil
Copy link
Member Author

lpil commented May 21, 2019

Gleam does not currently have type classes, though may have something similar in future.

Implementations for this kind of polymorphism will have to be explicitly supplied by the programmer (e.g. sort_by) rather than implicitly supplied by the compiler, like in Erlang or OCaml.

@lpil lpil added help wanted Contributions encouraged and removed discussion The approach has not yet been decided labels Jul 16, 2019
@lpil lpil changed the title Should >, >=, <, <= work on all types? Make >, >=, <, <= operate only on Ints Jul 16, 2019
dirk added a commit to dirk/gleam that referenced this issue Aug 14, 2019
Restrict the comparison operators to only operate on integers to
prevent confusion and/or unexpected results when values of different
types are compared.

Fixes gleam-lang#171
@lpil lpil closed this as completed in #207 Aug 14, 2019
lpil pushed a commit that referenced this issue Aug 14, 2019
Restrict the comparison operators to only operate on integers to
prevent confusion and/or unexpected results when values of different
types are compared.

Fixes #171
@lpil lpil added this to the v0.4 milestone Aug 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Contributions encouraged
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants