本文介绍了在F#3.0中中断的活动模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此活动模式使用F#2.0进行编译:

This active pattern compiles with F# 2.0:

let (|Value|_|) value = // 'a -> 'T option
  match box value with
  | :? 'T as x -> Some x
  | _ -> None

但是在F#3.0中,会发出错误:

but, in F# 3.0, emits the error:

我尝试过:

let (|Value|_|) value : 'T option = ...

和:

let (|Value|_|) (value: 'U) = ...

如何解决?

环境:Visual Studio 2012(RTM)和FSI v11.0.50727.1

Environments: Visual Studio 2012 (RTM) and FSI v11.0.50727.1

这是一个更简单的再现:

Here's a simpler repro:

let (|X|) x = unbox x

推荐答案

F#2.0编译器中存在一个错误,该错误针对某些活动模式(其结果带有自由类型变量)进行了错误的分析和错误的代码生成;一个简单的复制就是

There was a bug in the F# 2.0 compiler where the compiler did incorrect analysis and bad code generation for certain Active Patterns with free type variables in the result; a simple repro is

let (|Check|) (a : int) = a, None
//let (|Check|) (a : int) = a, (None : int option)

let check a = 
    match a with
    | Check (10, None) -> System.Console.WriteLine "10"
    | Check (20, None) -> System.Console.WriteLine "20"

check 10
check 20

会在编译时生成一个奇怪的警告,并将其编译为看似不正确的代码.我猜想我们在F#3.0中修复此错误(并限制一些疯狂的情况)的尝试也破坏了一些法律代码,因为该修复附带了损害.

which generates a weird warning at compile-time and compiles into seemingly incorrect code. I am guessing that our attempt to fix this bug (and restrict some crazy cases) in F# 3.0 also broke some legal code as collateral damage of the fix.

我将提交另一个错误,但是对于F#3.0,听起来您需要使用其他答案中提到的一种解决方法.

I'll file another bug, but for F# 3.0, it sounds like you'll need to use one of the workarounds mentioned in other answers.

这篇关于在F#3.0中中断的活动模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 16:07