Multiple constructors in f # with property assignment

I want to have an empty constructor and constructor overload that takes a parameter and assigns it to a public property.

This is where I am stuck:

type TemplateService() = 
    interface ITemplateService with

        //Properties
        member TemplateDirectory = ""

        //Constructors
        new (templateDirectory:string) = //Error here.
            if (templateDirectory == null) then
                raise (new System.ArgumentNullException("templateDirectory"))
            TemplateDirectory = templateDirectory;

This gives me an error: “Unexpected keyword“ new ”in Object expression. Expected“ member ”,“ override ”or other token.

If I use member, the property TemplateDirectorygives this error:

This instance member needs a parameter to represent the object being invoked. Make the member static or use the notation 'member x.Member(args) = ...'

+3
source share
3 answers

You can try this.

type TemplateService(templateDirectory : string) = 
    do
        if templateDirectory = null then nullArg "templateDirectory"

    new() = TemplateService("")

    interface ITemplateService with
        member this.TemplateDirectory = templateDirectory
+6
source

Unfortunately, if you want to use the interface and pass the value to the constructor, then nyinyithann's answer is correct. You can set public properties in a constructor call like this.

type TemplateService() = 
    let mutable templateDirectory = ""

    member this.TemplateDirectory
        with get() = templateDirectory
        and set directory = 
            if directory = null then
                raise (new System.ArgumentNullException "templateDirectory")
            templateDirectory <- directory

let template = TemplateService(TemplateDirectory = "root")

, , .

type ITemplateService =
     abstract TemplateDirectory : string with get, set

type TemplateService() = 
    let mutable templateDirectory = ""

    interface ITemplateService with
        member this.TemplateDirectory
            with get() = templateDirectory
            and set directory = 
                if directory = null then
                    raise (new System.ArgumentNullException "templateDirectory")
                templateDirectory <- directory

let template = TemplateService(TemplateDirectory = "root") // error

.

let template = TemplateService()
(template :> ITemplateService).TemplateDirectory <- "root"
0

You put your constructor in the interface definition, and this is the cause of the error. Also you are trying to save the value only for the get only property, instead you should use the backup storage.

Finally, I would recommend a version of nyinyithann compared to this, as it is more related to the regular F # style (minimal variables), just hoping to provide a version closer to yours if that would be useful.

type TemplateService() = 
    let mutable directory = ""

    interface ITemplateService with

        //Properties
        member this.TemplateDirectory = directory

    //Constructors
    new (templateDirectory:string) =
        if (templateDirectory = null) then
            raise (new System.ArgumentNullException("templateDirectory"))
        directory <- templateDirectory;
0
source

All Articles