fsharp / fslang-suggestions

The place to make suggestions, discuss and vote on F# language and core library features
344 stars 21 forks source link

Align precedence of infix operators with a dot prefix to F# spec #1263

Open kevmal opened 1 year ago

kevmal commented 1 year ago

I propose we adjust the precedence for infix operators with a leading dot, such as (.&&) , (.||), (..&&), etc. , to make them equivalent to their counterparts without the leading dot, such as (&&), as specified in the F# language specification.

The existing way of approaching this problem in F# is to use additional brackets to achieve the correct order of operations, but this does not match the language specification and causes confusion and unexpected behavior.

Pros and Cons

The advantages of making this adjustment to F# are:

The disadvantages of making this adjustment to F# are:

Extra information

Estimated cost (XS, S, M, L, XL, XXL): S

Related suggestions: None

Affidavit (please submit!)

Please tick this by placing a cross in the box:

Please tick all that apply:

Tarmil commented 1 year ago

For reference, page 38 of https://fsharp.org/specs/language-spec/4.1/FSharpSpec-4.1-latest.pdf:

In the preceding table, leading . characters are ignored when determining precedence for infix operators. For example, .* has the same precedence as *. This rule ensures that operators such as .*, which is frequently used for pointwise-operation on matrices, have the expected precedence.

dsyme commented 1 year ago

We should clarify the specification instead to match the implementation, given the backwards compat constraints involved.

kevmal commented 1 year ago

@dsyme This was per-approved here: https://github.com/dotnet/fsharp/issues/13051 have things changed?

It's a bit awkward since, for example, a neural network lib may want an "and" operator that results in a non-bool type, but the order of operations ends up being off making readability difficult.

dsyme commented 1 year ago

Thanks @kevmal , yes I've reopened.

My bar on breaking changes shifts around a bit - this is right on the margin.

@vzarytovskii Could you bar-check you think this is a reasonable thing to change if placed under a langversion switch? It does make the language more consistent in the area of operators, which have too many corner-cases like this.