2

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
Guy Coder
  • 24,501
  • 8
  • 71
  • 136
Scott Nimrod
  • 11,206
  • 11
  • 54
  • 118
  • 5
    The answer could be easily found in documentation https://msdn.microsoft.com/en-us/library/dd548047.aspx – Petr Apr 12 '16 at 00:57
  • 1
    speed for one (sometimes) – John Palmer Apr 12 '16 at 01:07
  • 1
    http://stackoverflow.com/questions/10192390/make-function-work-with-all-numeric-types-int-float-long and http://stackoverflow.com/questions/6285493/type-of-addition-in-fthere – s952163 Apr 12 '16 at 01:15
  • 1
    Overloaded functions (and operators) must be marked inline in F# – Scott Nimrod Apr 12 '16 at 03:26
  • 2
    Of interest: [Inline Fun Part I](http://nut-cracker.azurewebsites.net/blog/2011/10/05/inlinefun/) Also check out parts II, III, and IV. – Guy Coder Apr 12 '16 at 12:03
  • 2
    Of interest: [Use of `inline` in F#](http://stackoverflow.com/questions/3754862/use-of-inline-in-f) – Guy Coder Apr 12 '16 at 12:19
  • 1
    The use of `inline` for performance is obvious. The use of `inline` for typing is not so obvious or is even known to people new to F#. As such when I went to search previous SO questions tagged with `inline` I was surprised at how many had useful information but were missing the `inline` tag. So I judiciously added the `inline` tag. If you now read the results of this [query](http://stackoverflow.com/questions/tagged/f%23+inline) you will find lots of useful information related to `inline`. Once you have a specific Q&A open search for inline, most of the good info is in the answers. – Guy Coder Apr 12 '16 at 13:44

1 Answers1

11

In this case, it may be easier to understand what inline gives you if you try to remove the keyword:

let (|Positive|Neutral|Negative|) x =
    match sign x with
    | 1 -> Positive
    | -1 -> Negative
    | _ -> Neutral

This active pattern has the type float -> Choice<unit,unit,unit>. Notice that the compiler has inferred that it only works for float input.

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:

let isNatural = function
    | Positive -> true
    | _ -> false

This function has the type float -> bool, which means that you can use it only with float input:

> isNatural 1.;;
val it : bool = true
> isNatural 1;;

>   isNatural 1;;
  ----------^

stdin(4,11): error FS0001: This expression was expected to have type
    float    
but here has type
    int

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 inline the functions:

let inline (|Positive|Neutral|Negative|) x =
    match sign x with
    | 1 -> Positive
    | -1 -> Negative
    | _ -> Neutral

let inline isNatural x =
    match x with
    | Positive -> true
    | _ -> false

Because of the inline keyword, the compiler keeps the type of the functions generic:

> 
val inline ( |Positive|Neutral|Negative| ) :
  x: ^a -> Choice<unit,unit,unit> when  ^a : (member get_Sign :  ^a -> int)
val inline isNatural : x: ^a -> bool when  ^a : (member get_Sign :  ^a -> int)

This means that you can use any type for input, as long as there exists a function get_Sign that takes that type as input, and returns int.

You can now call the functions with both float, int, and other numeric types:

> isNatural 1.;;
val it : bool = true
> isNatural 1;;
val it : bool = true
Mark Seemann
  • 225,310
  • 48
  • 427
  • 736
  • 1
    Thanks mark. I interpreted the documentation for inline to be for performance only. I still think this was an important question regardless of people voting it down. Thanks again. – Scott Nimrod Apr 12 '16 at 11:44
  • 1
    @ScottNimrod I agree that what you were seeking was valid, but the question was specific to Active Patterns and that was easily answered. If you had asked the question differently I think you would have achieved the desired result. It took me a while to get better at asking certain types of questions without stepping on the mines. For example someone recently asked a [question](http://stackoverflow.com/questions/36538273/is-there-any-documentation-on-how-to-use-glimpse-with-f-and-suave) about documentation and the question was closed, yet I asked similar questions and received up-votes. – Guy Coder Apr 12 '16 at 13:53
  • 1
    @ScottNimrod I find that if I have a question to ask I do all of the work I can, e.g. write code, fix bugs, read documents, and then only after all of that fails, I write up what I did and where I need help and above all make sure it is an objective question, then I get good quick answers, usually with upvotes. Yes I did ask about the site being shut down and get lots of love (sarcasm) but I fully expected it. – Guy Coder Apr 12 '16 at 13:56
  • @ScottNimrod It is not mob mentality it is an ethos. – Guy Coder Apr 12 '16 at 13:57