How does F# inline work?

断了今生、忘了曾经 提交于 2019-12-09 08:17:27

问题


With F# it is my understanding that you can use the inline keyword to perform type specialization at the call site. That is::

val inline (+) : ^a -> ^b -> ^c
      when (^a or ^b) : (static member (+) : ^a * ^b -> ^c)

Constrains that ^a or ^b must have a static member like op_Addition, or one of the built in primitives, that can be used to fill in the gap.

So if you have a method that has a + and you pass in an int and a short as parameters it unwraps + to an instruction to use the built in primitive for int, and if you pass in a float and a byte it uses the float primitive addition opcode.

How exactly is this done at compile time? How can you have a method in the CLR that switches what opcode or method it uses based on the type?

Is this behavior possible with Reflection.Emit? I understand that the inlining is performed at the call-site, does that mean that the code does not work with C#?


回答1:


As suggested by inline, the code is inlined at the call site. At every call site, you know the concrete type parameter ^T, so the specific code for that type is inserted there.

This is done by the F# compiler, you can't easily do it in other context (like C# or Ref.Emit).

The F# library has some inline functions that can still be called by other languages, the runtime for those implementations does dynamic dispatch based on the runtime type, see e.g. the code for AdditionDynamic in prim-types.fs in the F# Core library code to get a feel.



来源:https://stackoverflow.com/questions/4413324/how-does-f-inline-work

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