.net - How do I use F#'s "with" assignment keywords with list properties in records? -


i'm trying learn basics of f#, , stumbled on "with" keyword seems extremely elegant way of returning new instances modified copies of original object (or record). might have misunderstood, though, or i'm getting syntax wrong.

the compilation errors snippet below are

error fs0001: expression expected have type string here has type string list , error fs0001: expression expected have type string list here has type string

let nokids = { name = "alex something"; age = 23; kids = [] } printfn "%a" nokids  let married = { nokids name = "alex newname" }; printfn "%a" married  let withkids = { married kids = married.kids :: "john"} 

why not create new record instance "kids" list containing "john"?

your "with" syntax fine, list construction syntax not ;)

let withkids = { married kids = "john" :: married.kids } 

left side new element, right side rest of list. using :: can append elements head side (which usual pattern cons lists, f# lists example of those).


Comments

Popular posts from this blog

c - How to retrieve a variable from the Apache configuration inside the module? -

c# - Constructor arguments cannot be passed for interface mocks -

python - malformed header from script index.py Bad header -