Overload operator in F#: (/)

后端 未结 4 1122
情书的邮戳
情书的邮戳 2020-12-17 15:08

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.Com         


        
4条回答
  •  时光说笑
    2020-12-17 16:11

    I do not think this is possible in F#, based on a reading of the overloading documentation.

    I would instead suggest that you create your own function which looks like / but isn't. Something like:

    let () path1 path2 = Path.Combine (path1,path2)
    

    This is likely to be less annoying in the long run because it doesn't mess with the implicit type inference that the human reader is running--/ means that the result is a floating point, and remembering that it's sometimes a string is a burden*. But after the first time the reader sees , it's easy to remember that it does something related to the symbol embedded in the middle.

    *I think the only reason + for strings looks OK is over-exposure. After using Haskell or Caml for a long time, the first few minutes after switching to another language makes "foo" + "bar" look jarringly bad.

提交回复
热议问题