F# 转换/转换问题
在下面的代码片段中,我试图提取嵌套元组中包含的recid,该元组构成dict键。 嵌套元组格式为(Int32,(boolean,boolean))- 我正在寻找Int32项的值(即 实际上是数据库记录的行ID) 在下面的匹配代码中,我试图将recid添加到列表中,但首先我将对象强制转换为整数。F# 转换/转换问题,f#,dictionary,casting,match,tuples,F#,Dictionary,Casting,Match,Tuples,在下面的代码片段中,我试图提取嵌套元组中包含的recid,该元组构成dict键。 嵌套元组格式为(Int32,(boolean,boolean))- 我正在寻找Int32项的值(即 实际上是数据库记录的行ID) 在下面的匹配代码中,我试图将recid添加到列表中,但首先我将对象强制转换为整数。 但是,这会产生以下错误-不确定原因 错误: 此运行时强制或类型测试从类型“a”到int32 涉及基于此程序点之前的信息的不确定类型。 某些类型上不允许进行运行时类型测试。需要进一步的类型注释。 此处引用的
但是,这会产生以下错误-不确定原因 错误: 此运行时强制或类型测试从类型“a”到int32
涉及基于此程序点之前的信息的不确定类型。 某些类型上不允许进行运行时类型测试。需要进一步的类型注释。 此处引用的词典定义为:
// Create Dict
let rdict = new Dictionary<_,_>()
// Add elements
rdict.Add( (x.["PatientID"],(true,true) ),ldiff)
// Extract Dict items
let reclist = new ResizeArray<int32>()
for KeyValue(k,v) in rdict do
match k with
| ((recid,(true,true)) ->
printfn "Found a matching Record: %A " recid; // <- prints correct result
let n = (recid:?> int32) // <- coercion error
reclist.Add(n)
//创建Dict
设rdict=newdictionary()
//添加元素
rdict.Add((x.[“PatientID”],(true,true)),ldiff)
//提取Dict项
let reclist=new ResizeArray()
对于rdict do中的键值(k,v)
匹配
|((recid,(true,true))->
printfn“找到匹配的记录:%a”recid;//int32)//假设rdict
是一个Dictionary
然后生成一个ResizeArray
我建议:
let reclist =
(ResizeArray<_>(), rdict.Keys)
||> Seq.fold(fun list (id,_) -> list.Add id; list)
而reclist
将改为:
let reclist =
(ResizeArray<_>(), rdict.Keys)
||> Seq.fold(fun list (id,_,_) -> list.Add id; list)
为了完整性和将来的参考,我只想在进一步测试的基础上发布我的结果。
通过装箱/拆箱,我能够成功地修改我以前发布的代码
希望这会对将来的其他人有所帮助
// Add initial value note the box call here
diff_dict.Add( (box x.["PatientID"],(true,true) ),ldiff)
let reclist = new ResizeArray<int32>()
for KeyValue(k,v) in rdict do
//printfn "Difference Dictionary - Key: %A; Value: %A; " k v
match k with
// extract the result - note the 'unbox' call
| (recid,(true,false)) -> let n:int32 = unbox recid
reclist.Add(n)
//添加初始值注意此处的框调用
差异指令添加((方框x.[患者编号],(正确,正确)),ldiff)
let reclist=new ResizeArray()
对于rdict do中的键值(k,v)
//printfn“差异字典-键:%A;值:%A;”k v
匹配
//提取结果-注意“unbox”调用
|(recid,(true,false))->设n:int32=unbox recid
重新列表添加(n)
行rdict.Add((x.[“PatientID”],(true,true))
的左括号和右括号数量不匹配。字典的键和值类型是什么?是int*(bool*bool)
只打算作为键类型,或者int
是键类型而bool*bool
是值类型?另外,x的实际类型是什么。[“PatientID”]
?返回的是int
还是带框的int
(即,obj
,可以转换为int
)?Oops-我在发布rdict.add行时出错-现在已更正。(我正在实际/构建一个行/列对列表作为dict值。dict键实际上是一个复合(rid,(布尔,布尔))值。(布尔定义rec是否存在于一对表中)。x.[“PatientID”]是一个(未绑定)从db表中检索的int值。实际上,还有一个问题——如果伴随的bool
对是true,true
,而不是任何其他组合,那么您是否打算只将ID从rdict
复制到reclist
o为几种不同的匹配条件(例如,相交计数;a不在b计数中;b不在a计数中)构建单独的重新列表。顺便说一句,我认为装箱和取消装箱值可能会起作用(不确定当printfn正确处理cvn时为什么需要这样做)-我需要进一步测试。printfn
正确地处理了recid
,因为您正在使用%A
作为格式说明符,它适用于任何类型;我怀疑如果您使用%d
作为格式说明符,它会失败,这意味着x.“PatientID”]
实际上并没有返回int
。假设您使用的是Visual Studio,难道您不能将鼠标悬停在rdict
上并查看工具提示中的类型吗?dict键是从初始版本演变而来的,只有recid作为值。从逻辑上讲,我查看的布尔值与recid有些不同,因此我认为我将它们放在了一个独立的嵌套元组。我可以看到您的方法的一些优点-到目前为止,谢谢您的帮助。@BrendanC:Efficiency(减少分配计数)是int*bool*bool
相对于int*(bool*bool)的唯一真正优点
。从风格上讲,你的想法让我觉得很奇怪,但如果你觉得有道理,我看不出有什么重大危害。:-]
let reclistOnlyA, reclistOnlyB, reclistBoth, reclistNeither =
((ResizeArray<_>(), ResizeArray<_>(), ResizeArray<_>(), ResizeArray<_>()), rdict.Keys)
||> Seq.fold(fun (a, b, both, neither as lists) (id, bools) ->
(match bools with
| true, false -> a
| false, true -> b
| true, true -> both
| false, false -> neither).Add id
lists)
// Add initial value note the box call here
diff_dict.Add( (box x.["PatientID"],(true,true) ),ldiff)
let reclist = new ResizeArray<int32>()
for KeyValue(k,v) in rdict do
//printfn "Difference Dictionary - Key: %A; Value: %A; " k v
match k with
// extract the result - note the 'unbox' call
| (recid,(true,false)) -> let n:int32 = unbox recid
reclist.Add(n)