Overload operator in F#: (/)

f#, inline, operator-overloading

Solution

You cannot provide overloaded operators for existing types. One option is to use another operator name (as Natahan suggests). However, you can also define a new type to represent paths in your F# code and provide the `/` operator for this type:

open System    

// Simple type for representing paths
type Path(p) =
  // Returns the path as a string
  member x.Path = p 
  // Combines two paths
  static member ( / )(p1:Path, p2:Path) = 
    Path(IO.Path.Combine(p1.Path, p2.Path))

let n = 4 / 2
let p = Path("C:\\") / Path("Temp")

This has one important benefit - by making the types more explicit, you give the type checker more information that it can use to verify your code. If you use strings to represent paths, then you can easily confuse path with some other string (e.g. name). If you define your `Path` type, the type-checker will prevent you from making this mistake.

Moreover, the compiler won't allow you to (simply) combine paths incorrectly (which can easily happen if you represent paths as strings), because `p + p` is not defined (you can use only `/`, which correctly uses `Path.Combine`).

Problem

I would like to overload the (/) operator in F# for strings and preserve the meaning for numbers. ``` /// Combines to path strings let (/) path1 path2 = Path.Combine(path1,path2) let x = 3 / 4 // doesn't compile ``` If I try the following I get "Warning 29 Extension members cannot provide operator overloads. Consider defining the operator as part of the type definition instead." ``` /// Combines to path strings type System.String with static member (/) (path1,path2) = Path.Combine(path1,path2) ``` Any ideas? Regards, forki

Original source

Related problems