F#:奇型推理行为
首先,我有一个更好的方法来处理这个问题,所以这不是一个问题 然而,这是我不明白的。有人能解释一下吗 当我将交换函数定义为:F#:奇型推理行为,f#,types,F#,Types,首先,我有一个更好的方法来处理这个问题,所以这不是一个问题 然而,这是我不明白的。有人能解释一下吗 当我将交换函数定义为: namespace Utilities module Misc let Swap (left : 'a byref) (right : 'a byref) = let temp = left left <- right right <- temp 但当我定义模块时,如: namespace Utilities type Misc
namespace Utilities
module Misc
let Swap (left : 'a byref) (right : 'a byref) =
let temp = left
left <- right
right <- temp
但当我定义模块时,如:
namespace Utilities
type Misc =
static member Swap (left : 'a byref) (right : 'a byref) =
let temp = left
left <- right
right <- temp
通过将函数移动到类型中,调用方参数的类型推断是如何变化的?这可能是与F#编译器对类方法执行的元组转换的交互作用 Reflector将
杂项掉期的类型报告为:
public static void Swap<a>(ref a left, ref a right);
公共静态无效交换(左参考a,右参考a);
因此,我们可以在这里看到,编译器已经将curried参数转换为元组形式
使用元组参数定义方法可避免此问题:
type Misc =
static member Swap(left : 'a byref, right : 'a byref) =
let temp = left
left <- right
right <- temp
> let s = Array.init 3 (fun i -> i)
> val s : int array = [|0; 1; 2|]
> Misc.Swap (&s.[2], &s.[0])
> s;;
> val s : int array = [|2; 1; 0|]
类型杂项=
静态成员交换(左:byref,右:byref)=
让温度=左
左(i)
>val s:int数组=[|0;1;2 |]
>杂项掉期(&s.[2],&s.[0])
>s;;
>val s:int数组=[| 2;1;0 |]
那么我不能用元组arg调用原始问题类型吗?我不确定我是否理解您希望调用的内容。你能说得更具体些吗?谢谢。我实际上正在使用第一个工作版本。我只是想弄明白为什么事情没有加起来。很清楚,反射器看到的东西并不总是对F#中的合法性有影响。F#将curried方法编译成tuple方法,但存储额外的元数据,表示从F#查看时实际上是curried方法。要点是,仅仅因为Reflector看到了一个元组方法,并不意味着您可以使用F#的元组参数调用它。(这有意义吗?@Brian:目前有没有办法查看F#元数据?有关于反射器插件的计划吗?PowerPack反射库是一个不错的选择吗?谢谢你的提问;我们可能会将语言规范更改为不允许使用“byref”curried参数,因为部分应用程序会尝试捕获byref,这是不好的。:)
public static void Swap<a>(ref a left, ref a right);
type Misc =
static member Swap(left : 'a byref, right : 'a byref) =
let temp = left
left <- right
right <- temp
> let s = Array.init 3 (fun i -> i)
> val s : int array = [|0; 1; 2|]
> Misc.Swap (&s.[2], &s.[0])
> s;;
> val s : int array = [|2; 1; 0|]