开发者

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

开发者 https://www.devze.com 2023-02-07 08:47 出处:网络
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 us

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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消