F# use CLIMutable to create new objects

답변됨 F# use CLIMutable to create new objects

  • Thursday, September 20, 2012 7:58 PM
     
     

    Hello,

    I have some tuple data I want to find a short way to get all of them.  They have the following structure:

    (id, name, color, quantity, usage). 

    For example: (1, "A", "red", 200, "toy"); (2, "A", "red", 300, "toy"); …

    The total number of items is only about 300, they share this feature: the id and quantity are different, but name, color and usage are the same string value.

    I see F# 3.0 has a new feature CLIMutable, so I want to see if I can find a better way to list all the items.

    I don’t want to use the following like:

    let item1 = (1, "A", "red", 200, "toy")

    let item2 = (2, "A", "red", 300, "toy")

    let item3 = (3, "A", "red", 400, "toy")

    I want to do use CLIMutable:

    [<CLIMutable>]

    type item = { id : int; name: string; color: string; year: int; usage: string }

    But I don’t want do as the old way, like:

    let item1 = { id = 1,  name = "A", color = "red", quantity = 200, usage = "toy" }

    let item2 = { id = 2,  name = "A", color = "red", quantity = 300, usage = "toy" }

    let item3 = { id = 3,  name = "A", color = "red", quantity = 400, usage = "toy" }

    I want to do something like this:

    type items(ID: int, volume: int) = class

         let default = { id = 0; name = "A", color = "red", quantity = 0, usage = "toy" }

         let special = { id = ID; name = "A", color = "red", quantity = volume, usage = "toy" }

         member this.thing (ID, volume) = special

    end

    Therefore, I can use the following way to get any item I want, for example:

    let item5 = new items (5, 500)

    So I can see that item5 = {id = 5;  name = "A"; color = "red"; quantity = 500; usage = "toy" }

    But my code could not get compiled, I got error message:

    The record label 'item' is not defined.

    I think CLIMutable is rather new, I don’t know how to use it.

    Let me know if my way of thinking works or not, if not, if there any way to simplify the way to get the job done.

    Thanks,

                  

All Replies

  • Thursday, September 20, 2012 9:12 PM
     
     Answered Has Code

    I think this is what you want

    type item = { id : int; name: string; color: string; quantity: int; usage: string }
    let defaultValue = { id = 0; name = "A"; color = "red"; quantity = 0; usage = "toy" }
    let generate (id:int) (volume:int) = { defaultValue with id = id; quantity = volume }

    with no need for CLIMutable, just copy and update record.

    > generate 23 17;;
    val it : item = {id = 23;
                     name = "A";
                     color = "red";
                     quantity = 17;
                     usage = "toy";}
    >

    • Proposed As Answer by ildjarnMVP Thursday, September 20, 2012 11:54 PM
    • Marked As Answer by zydjohn Wednesday, October 10, 2012 8:22 PM
    •  
  • Friday, September 21, 2012 12:19 AM
     
     

    Also CLIMutable attribute just emits default constructor and property setters for fields into generated IL code but these features are not available from F# code itself. This attribute is mostly useful in interop with other .NET libraries (for example serialization/deserialization etc.). See about this and other new F# 3.0 features here: http://blogs.msdn.com/b/fsharpteam/archive/2012/07/19/more-about-fsharp-3.0-language-features.aspx


    Petr