active-pattern

F# active pattern as non-static member

半世苍凉 提交于 2020-01-03 16:49:43
问题 I'm not sure if non-static public member active patterns are allowed but you can define them without the compiler complaining. If they are allowed what's the syntax for matching against one? The compiler is giving me a type mismatch for Foo in FooBar2.doSomething. Expecting a 'a -> Choice<'b,'c> given 'a -> 'd -> Choice<unit,unit> // No error in this class, static works great type FooBar() = static member (|Foo|Bar|) (x, y) = match x = y with | true -> Foo | false -> Bar member x.doSomething

Active pattern broken in F# 3.0

谁都会走 提交于 2019-12-30 17:38:43
问题 This active pattern compiles with F# 2.0: let (|Value|_|) value = // 'a -> 'T option match box value with | :? 'T as x -> Some x | _ -> None but, in F# 3.0, emits the error: Active pattern '|Value|_|' has a result type containing type variables that are not determined by the input. The common cause is a [sic] when a result case is not mentioned, e.g. 'let (|A|B|) (x:int) = A x'. This can be fixed with a type constraint, e.g. 'let (|A|B|) (x:int) : Choice = A x' I tried: let (|Value|_|) value

How can I pass complex expression to parametrized active pattern?

戏子无情 提交于 2019-12-24 04:52:07
问题 I defined the active pattern "Expression" as follows: let (|Expression|_|) expression _ = Some(expression) Now I'm trying to use it in this way: match () with | Expression((totalWidth - wLeft - wRight) / (float model.Columns.Count - 0.5)) cw when cw <= wLeft * 4. && cw <= wRight * 4. -> cw | Expression((totalWidth - wLeft) / (float model.Columns.Count - .25)) cw when cw <= wLeft * 4. && cw > wRight * 4. -> cw | Expression((totalWidth - wRight) / (float model.Columns.Count - .25)) cw when cw >

Use of typeof<_> in active pattern

时光怂恿深爱的人放手 提交于 2019-12-23 10:34:07
问题 Given the following contrived active pattern: let (|TypeDef|_|) (typeDef:Type) (value:obj) = if obj.ReferenceEquals(value, null) then None else let typ = value.GetType() if typ.IsGenericType && typ.GetGenericTypeDefinition() = typeDef then Some(typ.GetGenericArguments()) else None The following: let dict = System.Collections.Generic.Dictionary<string,obj>() match dict with | TypeDef typedefof<Dictionary<_,_>> typeArgs -> printfn "%A" typeArgs | _ -> () gives the error: Unexpected type

Pattern combining type test and literal

久未见 提交于 2019-12-11 02:40:15
问题 The active pattern in this question fails to compile after upgrading to VS 2012 RTM. It provides a way to do a type test and match a literal within a single pattern. For example: let (|Value|_|) value = match box value with | :? 'T as x -> Some x | _ -> None let getValue (name: string) (r: IDataReader) = match r.[name] with | null | :? DBNull | Value "" -> Unchecked.defaultof<_> | v -> unbox v Can this be done without the active pattern? I realize a when guard could be used ( :? string as s

Is is possible to pattern match on the underlying shape of a discriminated union?

末鹿安然 提交于 2019-12-06 11:17:13
问题 Does F# support pattern matching of a discriminated union member instance by criteria other than the Identifier pattern? For example, imagine that I want to match on the underlying shape of the data and I want to consider anything with an int * int shape, regardless of how the DU classifies the value. Is Here's how I'd do it now: type ExampleDU = | BinaryCase1 of x:int * y:int | BinaryCase2 of x:int * y:int | UnaryCase1 of x:int let underlyingValue = (1,2) let asCase1 = BinaryCase1

Is is possible to pattern match on the underlying shape of a discriminated union?

痞子三分冷 提交于 2019-12-04 15:25:21
Does F# support pattern matching of a discriminated union member instance by criteria other than the Identifier pattern ? For example, imagine that I want to match on the underlying shape of the data and I want to consider anything with an int * int shape, regardless of how the DU classifies the value. Is Here's how I'd do it now: type ExampleDU = | BinaryCase1 of x:int * y:int | BinaryCase2 of x:int * y:int | UnaryCase1 of x:int let underlyingValue = (1,2) let asCase1 = BinaryCase1 underlyingValue let asCase2 = BinaryCase2 underlyingValue let shapeName = match asCase1 with | BinaryCase1 (x,y)

Active pattern broken in F# 3.0

廉价感情. 提交于 2019-12-01 17:09:53
This active pattern compiles with F# 2.0: let (|Value|_|) value = // 'a -> 'T option match box value with | :? 'T as x -> Some x | _ -> None but, in F# 3.0, emits the error: Active pattern '|Value|_|' has a result type containing type variables that are not determined by the input. The common cause is a [sic] when a result case is not mentioned, e.g. 'let (|A|B|) (x:int) = A x'. This can be fixed with a type constraint, e.g. 'let (|A|B|) (x:int) : Choice = A x' I tried: let (|Value|_|) value : 'T option = ... and: let (|Value|_|) (value: 'U) = ... How can it be fixed? Environments: Visual