Is there any good way for an inline function to ac

2019-02-24 18: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 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.

1条回答
Lonely孤独者°
2楼-- · 2019-02-24 19:37

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.

查看更多
登录 后发表回答