Join GitHub today
Add a multi-value equality check #675
Add a multi-value equality check
I propose we add a multi-value equality-check, similar to
Which could be rewritten (behind the scenes, so-to-speak) to:
x = 1 || x = 4
While there are alternatives (see below), none of these make the intent completely obvious, or create more complexity and cause for errors. (Easy enough to accidentally do
The existing way of approaching this problem in F# is to create multiple
x = 1 || x = 4
match x with | 1 | 4 -> true | _ -> false
There is also an alternative using
List.exists ((=) x) [1; 4]
One can also write this trivially with some sort of function:
let ``in`` s x = Seq.exists ((=) x) s
This complicates the use a bit, and it's easiest to do with
x |> ``in`` [1; 4]
I'm sure there are others, but the point is we already have several longer, less-direct ways of testing for this.
Pros and Cons
The advantages of making this adjustment to F# are a more fluid short-hand for comparing direct equality against multiple values, and it can make the intent more clear. ("I want to make sure
The disadvantages of making this adjustment to F# are an additional keyword of some-sort, and it will increase compilation complexity. I suspect that there will have to be specific rules for what values can be used here, otherwise it would very-likely substantially increase the work on the compiler. I.e. any type of dynamic-expression in the
Estimated cost (XS, S, M, L, XL, XXL): Unsure, but my limited knowledge assumes
Related suggestions: N/A
Affidavit (please submit!)
Please tick this by placing a cross in the box:
Please tick all that apply:
@jwosty Thanks, I have already included an example of aliasing for this in my original request. That doesn't make it ideal, and it also doesn't make it quick: performance on
(That was the syntax proposed in Slack, and I like it better than the proposed alternatives.)
If you add equality you should think about inequality too, and perhaps the magnitude comparisons. And when you allow the right hand side to take a list, why not taking it one step further and allow lists or sequences on both sides?
I didn't try it, but I'd assume this can be done by creating an operator that operates on two lists, say
@abelbraaksma That's a fair point, does F# support
The inequality could be done via
If we want to support a facets, I'm down. If we didn't have piping I'd suggest
The more common approach to inequality of sets or sequences is that inequality is true of at least one item of set one is unequal to at least one item of set two, since this is the inverse of equality ( which is true of at least one item compares true).
F# doesn't have a notion of overloaded operators (as opposed to redefined and notto be confused with member overloading of operators), and doesn't have a notion of ternary operators, so the 'between' syntax will not be a light hearted change to the language, if it's even considered (the syntax you gave is valid, but will raise a type exception as Booleans do not have magnitude relationships with integers). You can have multiple arguments though, but then you'd need to write something like
I'm not a 'functional-' anything, so I defer to your better judgment. I don't know that I'm as interested in set-to-set comparison as I am item-to-set comparisons, which are the more frequent use-case (for me). Ideally, I'd probably think that in the case of
This would move towards my purpose: a rewrite to
I'm starting to think this might be fruitless, because we'd especially want composition support, and that substantially complicates implementation.
Probably would be better if made it to be somewhat limited but very good at its job. If the set has the
Perl 6 sort-of has a syntax for this like
Hmm, F# already has something like this:
That's a bit long though. I think that a more useful feature, that covers the usecases of this proposal and potentially many others, would just be a shorthand syntax for checking if a value matches a single pattern, e.g.