Is there a good way for a built-in function to access private or internal values?

I just ran into a problem: when I try to access a private or internal value from an inline function, I get the error message "The value" xxx "was marked as inline, but its implementation uses an internal or private function that is not accessible enough." Although logical, I wonder if anyone has a good job. The best I can think of is posting values ​​in a nested module and just hoping that no one will poke around (which does not bother me at all, since these values ​​are immutable). I believe reflection is an option, but without the ability to use cache calls (using ... private delegates) there is too much of a performance hit.

+3
source share
1 answer

Short answer: no, since the value will be inserted into the queue on the call site, it will not be able to use private values, and there is no real way to get around it.

The longer answer is: if you don't mind writing incredibly ugly code, and you can handle the overhead of several method calls per use, one alternative is to create dynamic implementations (like OperatorIntrinsics.AbsDynamicTableImpl in the main library) that may be private. You can then wrap the dynamic implementation with a publicly opaque universal method (e.g. OperatorIntrinsics.AbsDynamic<'T> ), and then create an inline value that adds the correct type constraints and discards the dynamic implementation (e.g. let inline abs< ^t when ^t : (static member Abs : ^t -> ^t)> x = AbsDynamic x ). Now that you are inline abs , you just see the AbsDynamic call, but not one of the further implementation details. In most cases, I would expect it to be much worse than just making your value public, not private.

+2
source

Source: https://habr.com/ru/post/891679/


All Articles