mechanisms for handing functions with multiple optional parameters

I’m writing a library where there will be some functions that take many (more than 50) parameters (not my design but I need to model it).

I want to minimise the pain for the caller calling these functions, taking a simple example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>type Person =
{ name : string option
address : string option
}
</code>
<code>type Person = { name : string option address : string option } </code>
type Person = 
    {   name : string option 
        address : string option
    }

how can I create these records with the least pain.

Attempt 1 (consciously bad)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>module Person =
let make : string option -> string option -> Person =
fun name address ->
{ name = name
address = address
}
let p1 = Person.make (Some "Jim") None
</code>
<code>module Person = let make : string option -> string option -> Person = fun name address -> { name = name address = address } let p1 = Person.make (Some "Jim") None </code>
module Person = 
    let make : string option -> string option -> Person = 
        fun name address ->
            {   name = name
                address = address
            }

let p1 = Person.make (Some "Jim") None

this is how I would often write such code, but obviously the issue here is with many many parameters the client call to create a person would have lots of “Some” and “None”, painful

Attempt 2 (from microsoft documention)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>type Person =
{ name : string option
address : string option
}
static member make (?name : string,?address : string) =
{ name = name
address = address
}
let p1 = Person.make(name = "bill bloggs")
</code>
<code>type Person = { name : string option address : string option } static member make (?name : string,?address : string) = { name = name address = address } let p1 = Person.make(name = "bill bloggs") </code>
type Person = 
    {   name : string option 
        address : string option
    }
    static member make (?name : string,?address : string) = 
        {   name = name 
            address = address
        }

let p1 = Person.make(name = "bill bloggs")

This is better, but I’ve suddenly had to flip my style to use explicit static members, I don’t have a massive problem with this, but I wouldn’t want to mix the previous style with this style based on how I feel. I could write all my code like this, but if feels unidiomatic of F#.

Attempt 3 (can’t remember where I saw this)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>type Person2 =
{ name : string option
address : string option
}
module Person2 =
let make (setter : Person2 -> Person2) =
setter { name = None; address = None }
let p2 = Person2.make (fun p -> { p with name = Some "bill bloggs" })
</code>
<code>type Person2 = { name : string option address : string option } module Person2 = let make (setter : Person2 -> Person2) = setter { name = None; address = None } let p2 = Person2.make (fun p -> { p with name = Some "bill bloggs" }) </code>
type Person2 = 
    {   name : string option 
        address : string option
    }

module Person2 = 
    let make (setter : Person2 -> Person2) = 
        setter { name = None; address = None }


let p2 = Person2.make (fun p -> { p with name = Some "bill bloggs" })

This uses the record update syntax to optionally ‘update’ parameters with default values.
It preserves the idiomatic style of attempt 1, but really is more ‘noisy’ than attempt 2.

Question:

which of these is idiomatic? (or is another style idiomatic not listed)?

I like record update syntax, but without the noise. Just break out the “empty” instance as a starter:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>type Person =
{
name : string option
address : string option
}
let empty =
{
name = None
address = None
}
let p3 =
{ empty with
name = Some "bill bloggs" }
</code>
<code>type Person = { name : string option address : string option } let empty = { name = None address = None } let p3 = { empty with name = Some "bill bloggs" } </code>
type Person = 
    {
        name : string option 
        address : string option
    }
    
let empty =
    {
        name = None
        address = None
    }
    
let p3 =
    { empty with
        name = Some "bill bloggs" }

4

Other ideas (used in github.com/jet/* quite a bit – https://github.com/jet/FsKafka/blob/master/src/FsKafka/FsKafka.fs is a decent example, but I applied that lots of other places in having APIs with lots of options without reams of fluent trainwreck support code)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>type Person(?name, ?address) =
()
</code>
<code>type Person(?name, ?address) = () </code>
type Person(?name, ?address) =
    ()

And/or you can chain constructors and add some logic/helpers on top of that:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>type Person(?name, ?address) =
new(?name, ?address) = Person(name = name, address = address)
</code>
<code>type Person(?name, ?address) = new(?name, ?address) = Person(name = name, address = address) </code>
type Person(?name, ?address) =
    new(?name, ?address) = Person(name = name, address = address)

If you definitely want records and you don’t have a good reason for a module, I’d stick with the factory method on the type as a type‘s Methods can have optional args whereas a module‘s functions do not make that possible. (And don’t use lower case method names, you’ll hate yourself eventually if you do!)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>type Person =
{ name: string option; address: string option }
static member Create(?name, ?address): Person =
{ name = name; address = address }
</code>
<code>type Person = { name: string option; address: string option } static member Create(?name, ?address): Person = { name = name; address = address } </code>
type Person =
    { name: string option; address: string option }
    static member Create(?name, ?address): Person =
        { name = name; address = address }

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật