Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
F#使用区分并集的基类型_F#_Discriminated Union - Fatal编程技术网

F#使用区分并集的基类型

F#使用区分并集的基类型,f#,discriminated-union,F#,Discriminated Union,我正在学习F#并努力尝试使用受歧视的工会。我有一个简单的例子,我试图在类型映射的一个简单的有区别的并集上使用Map.Map,但是它说存在类型不匹配。我基本上只是想把价格作为地图 以下是一个简化的示例: type Prices = Prices of Map<string, int> let GetSalePrice (prices: Prices) = prices |> Map.map (fun k v -> (k, v * 2)) 类型价格=地图价格 让GetSa

我正在学习F#并努力尝试使用受歧视的工会。我有一个简单的例子,我试图在类型映射的一个简单的有区别的并集上使用
Map.Map
,但是它说存在类型不匹配。我基本上只是想把价格作为地图

以下是一个简化的示例:

type Prices = Prices of Map<string, int>

let GetSalePrice (prices: Prices) = prices |> Map.map (fun k v -> (k, v * 2))
类型价格=地图价格
让GetSalePrice(prices:prices)=prices |>Map.Map(funkv->(k,v*2))
给我这个错误:

/Users/luke/code/chronos/Chronos.Mining/Chronos.Mining.Actors/Untitled-1(22,47): error FS0001: Type mismatch. Expecting a
    'Prices -> 'a'    
but given a
    'Map<'b,'c> -> Map<'b,'d>'    
The type 'Prices' does not match the type 'Map<'a,'b>'
/Users/luke/code/chronos/chronos.Mining/chronos.Mining.Actors/Untitled-1(22,47):错误FS0001:类型不匹配。期待
“价格->a”
但是给一个
'地图->地图'
类型“Prices”与类型“Map”不匹配
考虑到我在map函数中所做的只是返回值*2,我不明白为什么会出现这个错误

您不能将
价格
用作
地图
”,因为
价格
不是
地图
。按照您定义它的方式,
Prices
是一种不同的类型,与
Map
完全不同,但它包含一个
Map
的实例

如果这确实是您的意思,那么为了从
Prices
值中获得
Map
,您需要对其进行模式匹配。像这样:

let GetSalePrice (Prices theMap) = theMap |> Map.map ...
哇,这是怎么回事?
Prices-theMap
Prices:Prices
有何不同?为什么我们要通过冒号将类型名放在参数前面而不是后面?这不是类型在F#中的表示方式吗

您可能有点困惑,因为您对类型及其构造函数使用了相同的名称
Prices
。为了澄清这一点,让我重新定义您的类型如下:

type PricesType = PricesCtor of Map<string, int>
let GetSalePrice (prices: PricesType) = prices |> Map.map ...
你看,这不是我们放在参数前面的类型。这是构造器。这个声明-
(PricesCtor theMap)
-告诉编译器我们需要一个类型为
PricesType
(因为那是
PricesCtor
所属的)的参数,当我们得到这个参数时,它应该被展开,其中包含的映射应该被命名为
theMap

整个过程称为“模式匹配”。这里,我们在构造函数
PricesCtor
上进行匹配


另一方面,您的原始函数只指定了参数的类型。使用我的新类型定义,我可以这样编写您的原始函数:

type PricesType = PricesCtor of Map<string, int>
let GetSalePrice (prices: PricesType) = prices |> Map.map ...
在这里,我们指定参数的类型应为
PricesType
,但我们尝试将其用作
Map.Map
的参数,该参数需要
Map
类型的参数。难怪会出现类型不匹配


模式匹配也不必出现在参数声明中。您可以在代码中的任何位置进行模式匹配。为此,请使用
match
关键字。这就是您的函数可以这样编写的方式:

let GetSalePrice prices =
    match prices with
    | PricesCtor theMap -> theMap |> Map.map ...
只要您的类型有多个构造函数,
match
关键字就变得重要。例如:

type PricesType = PricesAsAMap of Map<string, int> | SinglePrice as int
编译器将警告您模式匹配不完整。实际上,您的函数知道在给定
SinglePrice
值时应该做什么,但在给定
ConstantPrice
值时应该做什么?你还没有定义,所以编译器会抱怨

此设置可用于使用
匹配
关键字:

let GetSalePrice prices = 
    match prices with
    | PricesAsAMap theMap -> theMap |> Map.map ...
    | SinglePrice p -> "single item", p

哇,谢谢你的精彩报道!这更有意义。我来自C#背景,因此我将类型的单个
视为继承。我要打破它!很高兴我能帮忙