我有几个通用的内联函数,例如:
let inline getRatesImpl<'T when 'T : (member enantiomer : 'T) and 'T : equality>
(d : Dictionary<'T, (ReactionRate option * ReactionRate option)>)
(calculateRates : 'T -> RelatedReactions<'T>)
(r : 'T) =
match d.TryGetValue r with
| true, rates ->
rates
| false, _ ->
updateRelatedReactions d (calculateRates r) r
函数的用法如下:
type FoodCreationModel (p : FoodCreationParam) =
let rateDictionary = new Dictionary<FoodCreationReaction, (ReactionRate option * ReactionRate option)>()
let calculateRates _ = getRates (Some p.foodCreationRate, Some 1.0) (None, None)
member __.getRates r =
getRatesImpl rateDictionary calculateRates r
member __.inputParams = p
当我在getRatesImpl
内休息时,它在FSI中未被击中。例如,即使我在getRates
(在行getRatesImpl rateDictionary calculateRates r
上)稍作休息,然后按F11键,FSI也不会进入inline getRatesImpl
内。但是,它确实有效,我可以通过查看字典在调用之前和之后的变化来检查它。
其中一些内联函数非常复杂,我确实需要不时地对其进行调试。
问题是如何?
答案 0 :(得分:1)
您的代码可能可以更改,以避免需要复杂的inline
。
避免使用SRTP的一种方法是将所需的函数作为参数而不是约束传递:
let getRatesImpl
(d : Dictionary<'T, (ReactionRate option * ReactionRate option)>)
(getEnantiomer: 'T -> 'T)
(calculateRates : 'T -> RelatedReactions<'T>)
(r : 'T) =
match d.TryGetValue r with
| true, rates ->
rates
| false, _ ->
updateRelatedReactions d getEnantiomer (calculateRates r) r
现在getRatesImpl
无需内联。
如果需要多个功能,则可以传递功能记录。