I still cannot understand why I would use the keyword inline for a function.
What does it give me that I don't already have?
let inline (|Positive|Neutral|Negative|) x =
match sign x with
| 1 -> Positive
| -1 -> Negative
| _ -> Neutral
I still cannot understand why I would use the keyword inline for a function.
What does it give me that I don't already have?
let inline (|Positive|Neutral|Negative|) x =
match sign x with
| 1 -> Positive
| -1 -> Negative
| _ -> Neutral
In this case, it may be easier to understand what
inlinegives you if you try to remove the keyword:This active pattern has the type
float -> Choice<unit,unit,unit>. Notice that the compiler has inferred that it only works forfloatinput.The consequences of this may be most apparent if we also define a function that uses this pattern, e.g. one that determines if a number is a natural number:
This function has the type
float -> bool, which means that you can use it only withfloatinput:What if you want to be able to determine that both
float,int,int64, etcetera, are natural numbers? Should you duplicate these functions for all input types?You don't have to. You can
inlinethe functions:Because of the
inlinekeyword, the compiler keeps the type of the functions generic:This means that you can use any type for input, as long as there exists a function
get_Signthat takes that type as input, and returnsint.You can now call the functions with both
float,int, and other numeric types: