I was trying to call this function from f#
The function signature is:
CloudStorageAccount.SetConfigurationSettingPublisher
(Action<string, Func<string, bool>>) : unit
The C# call goes something like this:
CloudStorageAccount.SetConfigurationSettingPublisher((configName,
configSettingPublisher) =>
{
string configValue = "something"
configSettingPublisher(configValue);
});
whereas in F#, I had to do something like this:
let myPublisher configName (setter:Func<string, bool>) =
let configValue = RoleEnvironment.GetConfigurationSettingValue(configName)
setter.Invoke(configName) |> ignore
let act = new Action<string, Func<string, bool>>(myPublisher)
CloudStorageAccount.SetConfigurationSettingPublisher(act)
Can this be written more concisely in f#?
F# automatically converts lambda functions created using the
fun ... -> ...syntax to .NET delegate types such asAction. This means that you can use lambda function as an argument toSetConfigurationSettingPublisherdirectly like this:A function of multiple arguments can be converted to a delegate of multiple arguments (the arguments shouldn’t be treated as a tuple). The type of
setteris stillFunc<...>and not a simple F# function, so you need to call it using theInvokemethod (but that shouldn’t be a big deal).If you want to turn
setterfromFunc<string, bool>to an F# functionstring -> bool, you can define a simple active pattern:…and then you can write: