5

I'd like to be able to extend types from other libraries with static methods to enable generic arithmetic. Take, for example, the newly minted SIMD-friendly fixed-size VectorN types from Microsoft. They define Zero, they define (+), they define (/), but I can't use Array.average on them because they don't define DivideByInt, which I'd be happy to:

open System.Numerics
type Vector2f with 
  static member DivideByInt (v:Vector2f) (i:int) = v / Vector2f(single i, single i)
let bigArray : Vector2f[] = readABigFile()
printf "the average is %A" (Array.average bigArray)

But it won't let me compile, complaining

error FS0001: The type 'Vector2f' does not support the operator 'DivideByInt'

Why does this limitation exist in the F# compiler?

(Edit: essentially the same question was asked previously.)

Community
  • 1
  • 1
Sebastian Good
  • 6,310
  • 2
  • 33
  • 57
  • I suspect that `DivideByInt` needs to be an operator rather than a function. Maybe try either defining an operator, or I think `op_DivideByInt` might work. – John Palmer Aug 17 '14 at 04:21
  • `DivideByInt` isn't a valid name for an operator in F# – Sebastian Good Aug 17 '14 at 05:12
  • 1
    Does doing `DivideByInt (v:Vector2f,i:int)` (tuple form) work? I have got a minimal example with type extensions that works. – John Palmer Aug 17 '14 at 06:22
  • 1
    Looking at the source will show the constraints you need to satisfy: https://github.com/fsharp/fsharp/blob/master/src/fsharp/FSharp.Core/array.fsi – John Palmer Aug 17 '14 at 06:39
  • No, unfortunately it's not possible. Possible duplicate of http://stackoverflow.com/questions/3681142/static-extension-methods-supporting-member-constraints – Gus Aug 17 '14 at 07:17

1 Answers1

6

It is not currently possible to define an operator overload in a type extension. There is an F# language user voice item for this (with quite a lot of votes) and so this is something that might change in future versions of F# (I think it would be great addition that fits nicely with the F# design).

If you absolutely need something like this today, you can either create a lightweight wrapper for your type that adds the operators, or you can use a (somewhat scary) trick that lets you hide the standard operator with a new overloaded one. The following question has both examples: Global operator overloading in F#

Community
  • 1
  • 1
Tomas Petricek
  • 240,744
  • 19
  • 378
  • 553