内联函数是否有任何好的方法来访问私有或内部值? [英] Is there any good way for an inline function to access private or internal values?

查看:102
本文介绍了内联函数是否有任何好的方法来访问私有或内部值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是遇到一个问题:当我尝试从内联函数访问私有或内部值时,出现错误值'xxx'被标记为内联,但是其实现使用了内部或私有函数,无法充分访问".虽然合乎逻辑,但我想知道是否有人能解决这个问题.我能想到的最好的办法是将这些值公开地放在一个嵌套模块中,并希望没人能随意修改(我也不太担心,因为这些值是不可变的).我想反射是一个选择,但是如果不能缓存调用(使用...私人委托),那么性能损失会太大.

I just ran into an issue: when I try to access a private or internal value from an inline function, I get the error "The value 'xxx' was marked inline but its implementation makes use of an internal or private function which is not sufficiently accessible". While logical, I wonder if anyone has a good work around. The best thing I can think to do is place the values publicly in a nested module and just hope no-one goes poking around (which I'm not too worried about anyway, since these values are immutable). I suppose reflection is an option, but without being able cache calls (using... private delegates) the performance hit is too much.

推荐答案

简短的回答:否,因为该值将以内联方式插入到呼叫站点中,所以它不能使用私有值,也没有真正的解决方法它.

Short answer: no, since the value will be inserted inline into the call-site, it can't use private values and there's no real way to work around it.

更长的答案:如果您不介意编写令人难以置信的丑陋代码,并且每次使用都可以处理一些方法调用的开销,那么另一种选择是创建动态实现(例如,核心库中的OperatorIntrinsics.AbsDynamicTableImpl),可以是私人的.然后,您可以将动态实现包装在不透明的公共通用方法中(例如OperatorIntrinsics.AbsDynamic<'T>),然后创建一个内联值,该值会向动态实现中添加适当的类型约束和延迟(例如let inline abs< ^t when ^t : (static member Abs : ^t -> ^t)> x = AbsDynamic x).现在,当内联abs时,您仅看到对AbsDynamic的调用,但没有进一步的实现细节.在大多数情况下,我希望这将是一个比将您的价值公开而不是私有化更糟糕的解决方案.

Longer answer: if you don't mind writing incredibly ugly code and you can handle the overhead of a few method calls per use, one alternative would be to create a dynamic implementation (e.g. OperatorIntrinsics.AbsDynamicTableImpl in the core library), which can be private. You can then wrap the dynamic implementation in a public opaque generic method (e.g. OperatorIntrinsics.AbsDynamic<'T>), and then create an inline value which adds the proper type constraints and defers to the dynamic implementation (e.g. let inline abs< ^t when ^t : (static member Abs : ^t -> ^t)> x = AbsDynamic x). Now when you inline abs you just see a call to AbsDynamic but none of the further implementation details. In most cases I would expect this to be a much worse solution than just making your value public instead of private.

这篇关于内联函数是否有任何好的方法来访问私有或内部值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆