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

  • Home
  • SEARCH
  • 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 4560564
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:56:52+00:00 2026-05-21T17:56:52+00:00

I’m trying to write a game using F# and Silverlight and am struggling a

  • 0

I’m trying to write a game using F# and Silverlight and am struggling a bit with immutability.

I want to decouple the game from the view a little, so I put it in a module and made its update function return a new instance of the world-state, thus providing immutability.

The view (AppControl) is responsible for drawing the world.

However, I see no way around making the world a ref cell in the view.

Now, I think the mutable state is local enough to not cause any problems (please correct me, if I’m wrong), I am just curious, if someone can think of a way to avoid mutable state completely?

Here’s the outline of the application, I tried to reduce the problem down to the essence:

open System
open System.Windows
open System.Windows.Controls
open System.Windows.Media

module Game =
    type World = { State : int }

    let init() = 
        { State = 0 }

    // immutable update loop
    let updateWorld world = 
        { State = world.State + 1 }


type AppControl() =
    inherit UserControl()

    let canvas = new Canvas()
    let textBlock = new TextBlock()
    let world = Game.init() |> ref // mutable world

    let drawWorld (world : Game.World) = 
        textBlock.Text <- world.State.ToString()

    // mutating game loop
    let gameLoop world = 
        world := Game.updateWorld !world
        drawWorld !world
        ()

    do  
        canvas.Children.Add(textBlock)
        base.Content <- canvas
        CompositionTarget.Rendering.Add (fun _ -> gameLoop world)


type App() as this =
    inherit Application()
    let main = new AppControl()
    do this.Startup.Add(fun _ -> this.RootVisual <- main)
  • 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-21T17:56:53+00:00Added an answer on May 21, 2026 at 5:56 pm

    The structure of your code looks fine – the mutable state is localized in the user interface (which is mutable anyway), so it is fine. You’re not mutating the field from any closure, so you could use a mutable field (declared using let mutable world = ..) instead of ref cell.

    To completely avoid the mutation, you can use asynchronous workflow (running on the GUI thread):

    type AppControl() =
        inherit UserControl()
    
        let canvas = new Canvas()
        let textBlock = new TextBlock()
    
        let drawWorld (world : Game.World) = 
            textBlock.Text <- world.State.ToString()
    
        // Asynchronous loop that waits for 'Rendering', updates
        // the world & draws it and then continues waiting
        let gameLoop world = async {
            let! _ = Async.AwaitEvent CompositionTarget.Rendering
            let newWorld = Game.updateWorld world
            drawWorld newWorld 
            return! gameLoop newWorld }
    
        do  
            canvas.Children.Add(textBlock)
            base.Content <- canvas
            gameLoop Game.init() |> Async.StartImmediate
    

    The gameLoop function is asynchronous, so it doesn’t block any thread. It is started using Async.StartImmediate, which means that it will run only on GUI thread (so accessing GUI elements & events from the body is safe). Inside the function, you can wait for event occurrence (using Async.AwaitEvent) and then do some action. The last line (return!) is a tail-call, so the function will continue running until the application is closed.

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

Sidebar

Related Questions

No related questions found

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.