XenoPuTtSs
XenoPuTtSs

Reputation: 1284

How do i generate a new immutable object based from a object defined with type in f#?

I have this code which isn't working

type Option(xsdLocation:string, xmlDirectory:string) =
    member this.XsdLocation = xsdLocation 
    member this.XmlDirectory = xmlDirectory

let a1 = new Option("xsd","xml")
let a2 = {a1 with XsdLocation = "xsd2"}

i get the error error FS1129: The type 'Option' does not contain a field 'XsdLocation'

Upvotes: 1

Views: 51

Answers (1)

TheInnerLight
TheInnerLight

Reputation: 12184

The object you defined is a standard .NET class, not a record. If you want to use with syntax you should define it as such:

type Option  = {XsdLocation : string; XmlDirectory : string}

let a1 = {XsdLocation = "xsd"; XmlDirectory = "xml"}
let a2 = {a1 with XsdLocation = "xsd2"}

PS: I'd also recommend picking a name other than Option for your type since Option is a built-in type.

Upvotes: 4

Related Questions