f#:如何匹配类型值?

时间:2022-02-08 12:56:35

I have a function that takes a generic parameter, and inside it I need to execute one of two functions depending on the type of the parameter.

我有一个函数,它接受一个泛型参数,在它里面,我需要根据参数的类型执行两个函数中的一个。

member this.Load<'T> _path =
    let hhType = typeof<HooHah>
    match typeof<'T> with
        | hhType -> this.LoadLikeCrazy<'T> _path
        | _ -> this.LoadWithPizzaz<'T> _path

.... where LoadLikeCrazy and LoadWithPizzaz both return a 'T.

....当LoadLikeCrazy和LoadWithPizzaz都返回一个“T”时。

VS informs me that the wildcard case will never get executed, since I am apparently getting the type of a generic at compile time, and not the actual type at runtime. How do I go about this?

VS告诉我通配符的情况永远不会被执行,因为我很明显在编译时获得了泛型的类型,而不是运行时的实际类型。我该怎么做呢?

3 个解决方案

#1


13  

In your code , the first pattern match rule doesn't compare typeof<'T> against hhType. Instead, it will introduce a new value called hhType. That's the reason you got warning. I'd rather modify the code like this.

在您的代码中,第一个模式匹配规则不比较<'T>与hhType的类型。相反,它将引入一个名为hhType的新值。这就是你得到警告的原因。我宁愿修改这样的代码。

member this.Load<'T> _path =        
    match typeof<'T> with        
    | x when x = typeof<HooHah>  -> this.LoadLikeCrazy<'T> _path        
    | _ -> this.LoadWithPizzaz<'T> _path

#2


2  

Is _path an instance of 'T? If so, Talljoe's solution will work, otherwise you'll have to do something like:

_path是T的实例吗?如果是这样,Talljoe的解决方案将会起作用,否则你将不得不做一些类似的事情:

member this.Load<'T> _path =
    if typeof<'T> = typeof<HooHah> then this.LoadLikeCrazy<'T> _path
    else this.LoadWithPizzaz<'T> _path

The reason for the error is that hhType within your match expression is shadowing the prior declaration of hhType. So, it's merely capturing the value of your match expression into a new binding. This matches everything, therefore your wildcard condition will never be hit.

错误的原因是,匹配表达式中的hhType是对hhType之前的声明的阴影。因此,它只是将匹配表达式的值捕获到一个新的绑定中。这将匹配所有的内容,因此您的通配符条件将不会受到影响。

#3


0  

What nyinyithann has mentioned is correct. I wrote the below code in F#

nyinyithann提到的是正确的。我在f#中编写了下面的代码。

let Hello name = 
let n = "Sample"
match name with
| n -> 1
| _ -> 0

Got the same warning. Used reflector to see what code is generate and found the below code (decompiled in C#)

得到了同样的警告。使用反射器来查看生成的代码,并找到下面的代码(在c#中进行反编译)

public static int Hello<a>(a name)
{
  a local = name;
  a name = local;
  return 1;
}

Not sure why compiler did this :( . Can anyone describe this behaviour ?

不确定编译器为什么这么做:(。有人能描述这种行为吗?

#1


13  

In your code , the first pattern match rule doesn't compare typeof<'T> against hhType. Instead, it will introduce a new value called hhType. That's the reason you got warning. I'd rather modify the code like this.

在您的代码中,第一个模式匹配规则不比较<'T>与hhType的类型。相反,它将引入一个名为hhType的新值。这就是你得到警告的原因。我宁愿修改这样的代码。

member this.Load<'T> _path =        
    match typeof<'T> with        
    | x when x = typeof<HooHah>  -> this.LoadLikeCrazy<'T> _path        
    | _ -> this.LoadWithPizzaz<'T> _path

#2


2  

Is _path an instance of 'T? If so, Talljoe's solution will work, otherwise you'll have to do something like:

_path是T的实例吗?如果是这样,Talljoe的解决方案将会起作用,否则你将不得不做一些类似的事情:

member this.Load<'T> _path =
    if typeof<'T> = typeof<HooHah> then this.LoadLikeCrazy<'T> _path
    else this.LoadWithPizzaz<'T> _path

The reason for the error is that hhType within your match expression is shadowing the prior declaration of hhType. So, it's merely capturing the value of your match expression into a new binding. This matches everything, therefore your wildcard condition will never be hit.

错误的原因是,匹配表达式中的hhType是对hhType之前的声明的阴影。因此,它只是将匹配表达式的值捕获到一个新的绑定中。这将匹配所有的内容,因此您的通配符条件将不会受到影响。

#3


0  

What nyinyithann has mentioned is correct. I wrote the below code in F#

nyinyithann提到的是正确的。我在f#中编写了下面的代码。

let Hello name = 
let n = "Sample"
match name with
| n -> 1
| _ -> 0

Got the same warning. Used reflector to see what code is generate and found the below code (decompiled in C#)

得到了同样的警告。使用反射器来查看生成的代码,并找到下面的代码(在c#中进行反编译)

public static int Hello<a>(a name)
{
  a local = name;
  a name = local;
  return 1;
}

Not sure why compiler did this :( . Can anyone describe this behaviour ?

不确定编译器为什么这么做:(。有人能描述这种行为吗?