首页 > 解决方案 > Map.change is not defined

问题描述

I'm trying to invoke the Map.change function. It's clearly defined in the documentation but it just doesn't work for me. I've tried opening different namespaces (such as FSharp.Core, Microsoft.FSharp.Collections), with no success.

open FSharp.Collections

[<EntryPoint>]
let main argv =
    let testMap = Map.empty.Add("test", true).Add("otherTest", false)

    let newTestMap = 
        testMap 
        |> Map.change // Here's the error!
                "otherTest" 
                (fun v -> 
                    match v with
                    | Some b -> Some (not b)
                    | None -> None )

    printf(newTestMap)
    0

I'm getting the following error when trying to run the sample code above:

error FS0039: The value, constructor, namespace or type 'change' is not defined.

How do I use Map.change?

标签: dictionaryf#

解决方案


Your code works (apart from the printf). Maybe check your F# or .NET (Core) version. The Collections namespace doesn't have to be open.

let testMap = Map.empty.Add("test", true).Add("otherTest", false)

let newTestMap = 
    testMap 
    |> Map.change // Here's the error!
            "otherTest" 
            (fun v -> 
                match v with
                | Some b -> Some (not b)
                | None -> None )

printf "%A" newTestMap // map [("otherTest", true); ("test", true)]val it : unit = ()

Map.change


推荐阅读