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 8611127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:17:40+00:00 2026-06-12T04:17:40+00:00

I have a simulator that keeps some state as a record with two members:

  • 0

I have a simulator that keeps some state as a record with two members: one object of Class1, and a sequence of objects of Class2.

When the simulation runs, input is read and, depending on the input, some methods of those objects are called. Such method change their internal state.

I am learning F# and I understand the importance of immutable data. However, given the complexity of such state (much more than exposed here), I think that having such objects with internal mutable state is not a big deal. At least it is hidden.

The problem is, however, another one. And it’s probably simple.

In between iterations I lose changes to “one” and “many” objects!

I guess InvokeMethodOn (obviously simplified) takes a copy of those objects.

I realize I need some kind of references here but… I am a bit lost here… State should have ref members? InvokeMethodOn should pass by ref? All of them? And what about the “many” sequence?

EDIT : There could be millions of “many” objects. Each of them has 1 or 2 KB of state (which for now is just kept in a blob of bytes).

EDIT : Changing “many” to be an array (and using Array.iter as suggested) fixed the issue. Thanks to everybody!

type State = {
    one : Class1
    many : Class2 seq
}

type Simulator() = class
    member x.run(state : State) =
        // ....
        while ...
            let input = ReadInput
            if someFuncOf(input)
                then InvokeMethodOn(state.one, input)
                else Seq.iter (fun x -> InvokeMethodOn(x, input)) state.many                

    member x.InvokeMethodOn obj input =
         obj.ChangeInternalState input
  • 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-06-12T04:17:41+00:00Added an answer on June 12, 2026 at 4:17 am

    If your Class1 and Class2 contain mutable state that you change, I don’t see a reason why such change would be discarded at each iteration, unless you are recreating new copies of Class1 somehow.

    If I try to script a similar thing than what is presented, it runs fines.
    It would be interesting to know how your code diverge from this to find where we are missing something.

    type Class1 = { mutable label : string}
    type Container = { one : Class1; many : Class1 seq}
    
    let a = { label = "a" }
    let bs = [ { label = "b1" } ; { label = "b2" }]
    
    let cont = { one =a ; many = bs}
    printfn  "%A" cont.one.label
    
    cont.one.label <- "changed a"
    cont.many |> Seq.iter (fun x -> x.label <- "changed b")
    printfn  "%A" cont
    
    cont.one.label <- "changed again a"
    printfn  "%A" cont
    

    Note that in F# ref are really just a hidden representation of ‘mutable content’

    type 'a ref = { mutable contents : 'a }
    

    You might want to read this page about mutation in FSharp
    There is little magic to it and it should clarify many things.

    Also one thing to note regarding mutable data is that array are mutable by default : no need to re-declare them mutable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been working for two years in software industry. Some things that have
So I have some neural network simulator code that works correctly on the CPU,
I have an iPhone app running in the simulator that won't quit. I also
I have some code that causes the box2d physics simulation to stutter forever after
I'm using SortedDictionaries to simulate a Queue (due to some requirements that I have),
I have an app that is running fine in the iPhone simulator by way
I have a simulation that uses OpenGL for drawing. While it is running, I
I have a program that simulates mouse click. Code is something like this: [DllImport(user32.dll,
I have an assignment that simulates a dice game. As part of the program,
I have a really long integration test that simulates a sequential process involving many

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.