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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:11:24+00:00 2026-06-06T06:11:24+00:00

I know that variables in F# are immutable by default. But, for example in

  • 0

I know that variables in F# are immutable by default.
But, for example in F# interactive:

  > let x = 4;;

val x : int = 4

> let x = 5;;

val x : int = 5

> x;;
val it : int = 5
> 

So, I assign 4 to x, then 5 to x and it’s changing. Is it correct? Should it give some error or warning? Or I just don’t understand how it works?

  • 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-06T06:11:25+00:00Added an answer on June 6, 2026 at 6:11 am

    When you write let x = 3, you are binding the identifier x to the value 3. If you do that a second time in the same scope, you are declaring a new identifier that hides the previous one since it has the same name.

    Mutating a value in F# is done via the destructive update operator, <-. This will fail for immutable values, i.e.:

    > let x = 3;;
    
    val x : int = 3
    
    > x <- 5;;
    
      x <- 5;;
      ^^^^^^
    
    stdin(2,1): error FS0027: This value is not mutable
    

    To declare a mutable variable, add mutable after let:

    let mutable x = 5;;
    
    val mutable x : int = 5
    
    > x <- 6;;
    val it : unit = ()
    > x;;
    val it : int = 6
    

    But what’s the difference between the two, you might ask? An example may be enough:

    let i = 0;
    while i < 10 do
        let i = i + 1
        ()
    

    Despite the appearances, this is an infinite loop. The i declared inside the loop is a different i that hides the outer one. The outer one is immutable, so it always keeps its value 0 and the loop never ends. The correct way to write this is with a mutable variable:

    let mutable i = 0;
    while i < 10 do
        i <- i + 1
        ()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We all know that global variables are anything but best practice. But there are
I know that local variables and paramters of methods live in stack, but I
We know that automatic variables are destroyed upon the return of the function. Then,
So, I know you can't access variables outside scope, that they're immutable, that XSLT
I know that global variables are bad. But if I am using node's module
I know that variables allocated on that stack of a function become inaccessible when
I know that, in Delphi, instance variables and global variables are initialized to zero
Is there a runtime performance differance between public and private variables/methods? I know that
I know that it's a subject that can raise a lot of debate, but
I know that some say that class variables (e.g. @@class_var ) should be avoid

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.