fslang-suggestions icon indicating copy to clipboard operation
fslang-suggestions copied to clipboard

Allow Unicode symbols to be used as operators

Open voronoipotato opened this issue 2 years ago • 13 comments

I propose we revisit the proposal of allowing unicode symbols to be used as operators with a simplified precedence model.

The existing way of approaching this problem in F# is to create an inline function with backticks

let ``∫`` xs = xs |> Seq.sum
//and..
let ``∪`` a b =  Set.union a b
let x = a |> ``∪`` <| b

the proposed way of writing this is

let (∫) xs = xs |> Seq.sum
let l = ∫ [1..10]
//and..
let (∪) a b = Set.union a b 
let x = a ∪ b

(yes my examples are silly :P , you know the real ones)

The proposal is novel in that operator precedence of unicode operators would simply be left to right. If that's undesirable perhaps a attribute for the precedence with level 1 being lowest and defaulting to whatever ~ is when unspecified. We could then document each of the levels in https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/symbol-and-operator-reference/ so that the creators of operators could set up precedence in intuitive ways.

[<OperatorPrecedence(1)>]
let (∫) xs = xs |> Seq.sum

[<OperatorPrecedence(3)>]
let (∪) a b = Set.union a b 

Pros and Cons

The advantages of making this adjustment to F#

  • It would help allow mathematically inclined users to more naturally express things in code.
  • Mathematical symbols are easier to type in windows now that win + ; exists.
  • People who have difficulty reading mathematical operators would also have difficulty reading the math
  • People who want mathematical notation are already writing code like this using ``∫``.
  • Removing backticks makes it easier to read

The disadvantages of making this adjustment to F# are ...

  • Operator precedence would basically be left to right for unicode operators
  • More operators.
  • Math
  • A more APL style programming experience (left to right) or a new attribute
  • let x = 🧨7

Extra information

Full disclosure, This has been kind of proposed before, please review. I'm hoping to revisit it now that data science and scientific computing is a target audience and that I'm proposing a simplified approach to operator precedence.

#224

As discussed in the comments in the original submission, this is a minefield.... Deciding the precedence for such operators is really hard. It would also create swathes of unreadable F# code. I'll close this since we've previously decided not to do this in F# 2.0, and there has not yet been a major change of circumstance to warrant altering this which addresses the concerns. - Don Syme

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

Related suggestions: (put links to related suggestions here) #224

Affidavit (please submit!)

Please tick this by placing a cross in the box:

  • [X] This is not a question (e.g. like one you might ask on stackoverflow) and I have searched stackoverflow for discussions of this issue
  • [X] I have searched both open and closed suggestions on this site and believe this is not a duplicate
  • [X] This is not something which has obviously "already been decided" in previous versions of F#. If you're questioning a fundamental design decision that has obviously already been taken (e.g. "Make F# untyped") then please don't submit it.

Please tick all that apply:

  • [X] This is not a breaking change to the F# language design
  • [X] I or my company would be willing to help implement and/or test this

For Readers

If you would like to see this issue implemented, please click the :+1: emoji on this issue. These counts are used to generally order the suggestions by engagement.

voronoipotato avatar Sep 21 '21 15:09 voronoipotato