16

Consider the following code:

type Test () =
  member o.fn1 (?bo) = 1
  member o.fn2 (?bo) = o.fn1 bo

  member o.fn3 (?bo) = 1 + bo.Value
  member o.fn4 (?bo) = o.fn3 bo

While fn1 and fn2 work just fine, fn4 produces the following error:

init.fsx(6,30): error FS0001: This expression was expected to have type int but here has type 'a option

MSDN states:

Optional parameters are interpreted as the F# option type, so you can query them in the regular way that option types are queried, by using a match expression with Some and None.

To me, optional parameters are not interpreted as the F# option type otherwise the code would compile. Moreover I do not understand why, when I hover over ?bo in fn3 the tooltip says val bo: int option but from outside expects only int. I would expect a behavior of accepting nothing, int, Some int and None. And as the last note, I do not understand why fn2 works but fn4 does not.

Thanks for clarification

3 Answers 3

11

I have to reconsider the correct answer. Based on this question (and answer):

Propagating optional arguments

it seams that the correct answer is the following:

type Test () =
  member o.fn1 (?bo) = 1
  member o.fn2 (?bo) = o.fn1 bo

  member o.fn3 (?bo) = 1 + bo.Value
  member o.fn4 (?bo) = o.fn3 (?bo = bo)

It is a neat feature and credits for the answer go to desco!

5
  1. fn2 works because fn1 does not use its parameter, which is thus generic 'b option.

    type Test () =
       member o.fn1 (?bo1) = 1  --> bo1: 'b option, here 'b = 'a option
       member o.fn2 (?bo) = o.fn1 bo  -->bo: 'a option
    
  2. fn4 complains that the parameter passed to fn3 should be an int, but not int option because when you specify the parameter, you of course need to pass in a specific one. But you have the option to omit the parameter. The definition/type signature of fn3 does not know whether you have specify bo or not, so it is a int option. Notice that you may have the following usage:

    type Test () =
       member o.fn1 (?bo) = 1
       member o.fn2 (?bo) = o.fn1 bo
    
       member o.fn3 (?bo) = 
       match bo with
         | Some v -> 1 + bo.Value
         | None -> 1
    
       member o.fn4 (?bo) = o.fn3()
    

where you don't specify the parameter for fn3, but when you specify it, it is a concrete int, not int option.

Think about a plotting function with three parameters:

let plot(?x,?y,?color)

because the parameters are optional, you can have the following usage:

plot(data)
plot(y=data)
plot(x=data, color='r')

But not:

plot(Some data)
plot(y=Some data)
plot(x=Some data, color=Some 'r')
2
  • 1
    I get your comment but I still don't like the way it is done. I would much more prefer to be able to send nothing, value, Some value and None. When I want to resend optional parameter from one member to another I have to pattern match it in the first member and call the second member with and without parameter or I have to define another member with option type and resend all members to this one. Commented Jun 15, 2011 at 6:54
  • 2
    optional parameter is a syntax that is friendly to library users, but not library writers. You stand on the library writers' point to judge this feature. The more dynamic the interface, the more checking in the underlying implementation.
    – Yin Zhu
    Commented Jun 15, 2011 at 6:59
2

F# does not require matching to pass optional arguments. Using the plot-example:

type Plotter() =
    static member Plot(?x,?y,?color) = 
        printfn $"{x}, {y}, {color}" // Do something "plottish" here

type PlotUser() =
    static member MyPlotUser(?x, ?y, ?color) =
        Plotter.Plot(?x = x, ?y = y, ?color = color)

/// Perform calls
PlotUser.MyPlotUser(10) // => Some(10), , 
PlotUser.MyPlotUser(10, color = "Green") // => Some(10), , Some(Green)

The above lets you call plot with your own optional arguments. /JEE, Sharp#Soft

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.