Is there any good way for an inline function to access private or internal values?

自闭症网瘾萝莉.ら 提交于 2019-12-01 22:01:16

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!