是否有可能做函数重载在F#? [英] Is it possible to do function overloading in F#?

查看:118
本文介绍了是否有可能做函数重载在F#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类似于

let f x = log(x)

和后,我可以申请F到矩阵,向量或浮点数。

and later I can apply f to matrix, vector or a float.

我想这是不可能的,因为F#是严格的静态类型化。任何其他patters来克服这个问题?

I guess it is not possible since F# is strictly static typed. Any other patters to overcome this problem?

谢谢!

推荐答案

您可以使用运算符重载的类型/类的:

You can use operator overloading for types/classes:

type Fraction = 
  { n : int; d : int; }
  static member (+) (f1 : Fraction, f2 : Fraction) =
    { n = f1.n * f2.d + f2.n * f1.d; d = f1.d * f2.d }

或内联函数:

or inlined functions:

> let inline fi a b = a+b;;
val inline fi :
   ^a ->  ^b ->  ^c when ( ^a or  ^b) : (static member ( + ) :  ^a *  ^b ->  ^c)

这篇关于是否有可能做函数重载在F#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆