Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6954033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:33:14+00:00 2026-05-27T14:33:14+00:00

Suppose I have this class: type Pet (name:string) as this = let mutable age

  • 0

Suppose I have this class:

type Pet (name:string) as this =
    let mutable age = 5
    let mutable animal = "dog"

I want to be able to create a new Pet based on some serialized data, which I represent with this record:

type PetData = {
    name : string
    age : int
    animal : string
}

(TLDR: I can’t figure out the syntax to make a constructor that’ll take a PetData to populate the let bindings. My various attempts follow.)

So I make a new Pet constructor that’ll assign values to the let bindings. I try using the class initializer syntax:

new (data:PetData) =
    Pet(name,
        age = data.age,
        animal = data.animal
    )

Hmm, nope: No accessible member or object constructor named 'Pet' takes 1 arguments. The named argument 'age' doesn't correspond to any argument or settable return property for any overload.

I check to make sure I’ve got all the syntax: no missing commas, correct “assignment” (cough) operator, correct indentation.

Okay the, I’ll try the record initializer syntax.

new (data:PetData) =
    {
        name = data.name;
        age = data.age;
        animal = data.name
    }

Error: The type 'Pet' does not contain a field 'name'

Okay, so I need to call the main constructor. I guess there are probably two places I can put it, so let’s try both:

new (data:PetData) =
    {
        Pet(data.name);
        age = data.age;
        animal = data.name
    }

Nope: Invalid object, sequence or record expression

new (data:PetData) =
    Pet(data.name)
    {
        age = data.age;
        animal = data.name
    }

And nope: This is not a valid object construction expression. Explicit object constructors must either call an alternate constructor or initialize all fields of the object and specify a call to a super class constructor.

I didn’t want to have to do this, but maybe since the fields are mutable anyway, I can just assign values to the object after initializing it:

new (data:PetData) =
    let p = Pet(data.name)
    p.age <- data.age
    p.animal <- data.animal
    p

Type constraint mismatch. The type Pet is not compatible with type PetData The type 'Pet' is not compatible with the type 'PetData'

Lol, what??

Okay, let’s try this:

let assign(data:PetData) =
    this.age <- data.age
    this.animal <- data.animal

new (data:PetData) =
    let p = Pet(data.name)
    p.assign(data)
    p

The field, constructor or member 'assign' is not defined

Right, so it can’t access let bindings from outside.

Let’s try a member then:

new (data:PetData) =
    let p = Pet(data.name)
    p.Assign(data)
    p

member x.Assign(data:PetData) =
    this.age <- data.age
    this.animal <- data.animal

This is not a valid object construction expression. Explicit object constructors must either call an alternate constructor or initialize all fields of the object and specify a call to a super class constructor.

Okay… let’s try this whole thing differently then, using explicit fields:

type Pet =
    [<DefaultValue>]val mutable private age : int
    [<DefaultValue>]val mutable private animal : string
    val private name : string

    new(name:string) =
        { name = name }

    new(data:PetData) =
        {
            name = data.name;
            age = data.age;
            animal = data.animal
        }

Extraneous fields have been given values

And that’s when I punch my elderly cat in the face.

Any other ideas? These error messages are throwing me off. I can’t even find half of them on Google.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:33:15+00:00Added an answer on May 27, 2026 at 2:33 pm

    You could do this.

    type Pet =
        val mutable private age : int
        val mutable private animal : string
        val private name : string
    
        new (name:string) =
            { 
                name = name;
                age = 5; // or age = Unchecked.defaultof<_>;
                animal = "dog"; // or animal = Unchecked.defaultof<_>;
            }
    
        new (data:PetData) =
            {
                name = data.name;
                age = data.age;
                animal = data.animal;
            }
    

    F# has its own style which looks like this.

    type Pet(name:string, age:int, animal:string) =
        let mutable age = age
        let mutable animal = animal
    
        new (name:string) =
            Pet(name, 5, "dog")
    
        new (data:PetData) =
            Pet(data.name, data.age, data.animal)
    

    Edit

    Added an event used in do per comment request.

    type Pet(name:string, age:int, animal:string, start:IEvent<string>) =
        let mutable age = age
        let mutable animal = animal
    
        // all three constructors will call this code.
        do  start.Add (fun _ -> printf "Pet was started")
    
        new (name:string, start:IEvent<_>) =
            // an example of different logic per constructor
            // this is called before the `do` code.
            let e = start |> Event.map (fun x -> x + " from 'name constructor'")
            Pet(name, 5, "dog", e)
    
        new (data:PetData, start:IEvent<_>) =
            Pet(data.name, data.age, data.animal, start)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's suppose I have this object: [Serializable] public class MyClass { public int Age
Let's suppose I have xml like this one: <Server Active=No> <Url>http://some.url</Url> </Server> C# class
Suppose I have a class with a string instance attribute. Should I initialize this
Let's suppose I have an instance of the TList class (BDS 2006, so this
Suppose you have something like this: class intlist: def __init__(self,l = []): self.l =
Suppose I have code like this: template<class T, T initial_t> class Bar { //
Suppose I have a function which looks like this: template <class In, class In2>
Suppose that I have an integer variable in a class, and this variable may
Suppose I have this Windows wchar_t string: L\x4f60\x597d and L\x00e4\x00a0\x597d and would like to
Suppose I have this: public class Unit<MobileSuit, Pilot> { ... List<MobileSuit> mobileSuits; List<Pilot> pilots;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.