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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:59:54+00:00 2026-05-17T18:59:54+00:00

In my code I’m passing around some structures by reference, declaring them mutable and

  • 0

In my code I’m passing around some structures by reference, declaring them mutable and using the & symbol. The problem is that in some place the fields are corrupted (happens only in release mode) and I don’t know absolutely why.

I have found a fix, using ref keyword instead of the address-of operator. I understand that you can interchange them freely (in case of instance member parameters) but why did it fix my issue?

Here’s a small code sample illustrating this:

[<Struct>]
type MyStruct =
    val mutable private i : int
    val mutable private f : float
    new (a, b) = { i = a; f = b }
    member t.I = t.i
    member t.F = t.f

type Printer () =
    member t.Print(data : MyStruct byref) = printfn "%d %f" data.I data.F

let bar (p : Printer) =
    let mutable x = new MyStruct(2, 8.0)
    p.Print(&x)

let foo (p : Printer) =
    let mutable y = new MyStruct(2, 8.0)
    p.Print(ref y) // What is exactly the difference, under the hood?

let main () =
    foo (new Printer())
    bar (new Printer())
do main ()

Passing structures with byref seems useful only for interop scenarios or if you want to mutate structure’s fields. This is not my case however. Should I consider passing structure types by value instead (about 20 bytes or so)?

Thanks!

  • 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-17T18:59:55+00:00Added an answer on May 17, 2026 at 6:59 pm

    The use of ref in your example may not be exactly what you wanted to write:

    let modify (data:int byref) = data <- 10
    
    let foo() = 
      let mutable n = 15 // Note: Compiles without 'mutable'
      modify (ref n)
      printfn "%d" n // Prints '10'!!
    

    You probably wanted something like this:

    let foo() = 
      let n = ref 15
      modify n
      printfn "%d" (!n) // Prints '15'
    

    So, what is the difference? ref is a function that takes a value and creates a heap allocated reference cell. In the second example, the type of n is ref<int>, which is the reference cell. F# allows you to pass reference cells as arguments to byref parameters – in which case, it creates a pointer to a field of the heap-allocated (reference cell) object.

    In the second example, we create reference cell, pass a pointer to the reference cell to the modify function and then use the !<ref> syntax to get value from the reference cell. In the first example, we create a new reference cell when calling the modify function (and the value of n is copied from the stack to the heap allocated cell). The cell is not used after the call (and the value of n stays 15)

    On the other hand, a mutable variable is simply stored on the stack and can be mutated. The main difference is that ref is always heap allocated – using mutable could be (in principle) a bit faster, because called function taking byref directly modifies value on the stack.

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

Sidebar

Related Questions

Code and preview: <html> <head> <title>Testing some CSS</title> <style type=text/css> .dDay { font-size:205% }
Code that is untestable really annoys me. The following things make oo-code untestable: global
(code examples are python) Lets assume we have a list of percentages that add
Code below does not run correctly and throws InvalidOperationExcepiton . public void Foo() {
Code: <html xmlns=http://www.w3.org/1999/xhtml> <head> <title>Unusual Array Lengths!</title> <script type=text/javascript> var arrayList = new Array();
Code I have: cell_val = CStr(Nz(fld.value, )) Dim iter As Long For iter =
Code below is not working as expected to detect if it is in design
Code like this often happens: l = [] while foo: # baz l.append(bar) #
code snippet: //byte[] myByteArray = byte array from database (database BLOB) myByteArray = (byte[])
Code: for i in {0..3}; do ping http://www.pythonchallenge.com/pc/def/$i.html; done A host should be 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.