none
Any good "F# under the hood" articles/blogs? RRS feed

  • Question

  • Can anyone point me to some good blogs/articles on what exactly is happening under the hood when F# data structures are copied and updated?  I think I know what's supposed to be happening but the specifics could alter how I define my records to get the behavior I want, which is for the result of a record copy-and-update to present a new structure that points to the unchanged members of the original record as well as a new allocation for each changed member, but NOT a full brand new copy of each record as they are modified.

    Thanks!

    Ingram

    Wednesday, August 29, 2018 5:22 PM

All replies

  • Doing the obvious thing and looking at some code I have to hand, the F# code that looks like

             { restate with ModuleId = match kind with
                                       | 0 -> ComputeString0()
                                       | _ -> ComputeGeneralString() }

    decompiles to

    	FSharpList<string> field@ = restate.Field@;
    	object moduleId;
    	switch (kind)
    	{
    	default:
    		moduleId = ComputeGeneralString();
    		break;
    	case 0:
    		moduleId = ComputeString0();
    		break;
    	}
    	return new Record(field@, (string)moduleId, restate.Field2@, restate.Field3@, restate.Field4@, restate.Field5@, restate.Field6@);

    where the Record constructor just assigns the values given to the matching fields

    internal Record(FSharpList<string> field, string moduleId, Type2 field2, FSharpList<Type3> field3, Type4 field4, Type5 field5, Type6 field6)
    {
    	this.Field@ = field;
    	this.ModuleId@ = moduleId;
    	this.Field2@ = field2;
    	this.Field3@ = field3;
    	this.Field4@ = field4;
    	this.Field5@ = field5;
    	this.Field6@ = field6;
    }

    So, yes, it is doing what you want and what you would expect.



    • Edited by Mr. Tines Wednesday, August 29, 2018 6:31 PM Fix typo
    Wednesday, August 29, 2018 6:12 PM