f#

How do I implement a method with a variable number of arguments?

ぐ巨炮叔叔 提交于 2021-01-27 12:04:23
问题 How do I implement a method with a variable number of arguments? In C#, we can use the params keyword: public class MyClass { public static void UseParams(params int[] list) { for (int i = 0; i < list.Length; i++) { Console.Write(list[i] + " "); } Console.WriteLine(); } } So how can I do this in F#? type MyClass() = member this.SomeMethod(params (args:string array)) = () I receive the following error from the code above: The pattern discriminator 'params' is not defined 回答1: You can use

Can JsonProvider deserialise to a Generic.Dictionary?

安稳与你 提交于 2021-01-27 08:07:52
问题 I'm learning about type providers and it looks like a ground breaking feature. However, I can't manage to deserialise json with JsonProvider so that the target type has a Generic.Dictionary property. It can be done with Json.NET. Here is the code: type ByProv = JsonProvider<"""{"dict":{"A":1,"B":2}}"""> type ByHand(d:Dictionary<string,int>) = member val dict = d with get,set let text = """{"dict":{"C":3,"D":4}}""" let newt = JsonConvert.DeserializeObject<ByHand> text let prov = ByProv.Parse

Can JsonProvider deserialise to a Generic.Dictionary?

送分小仙女□ 提交于 2021-01-27 07:58:49
问题 I'm learning about type providers and it looks like a ground breaking feature. However, I can't manage to deserialise json with JsonProvider so that the target type has a Generic.Dictionary property. It can be done with Json.NET. Here is the code: type ByProv = JsonProvider<"""{"dict":{"A":1,"B":2}}"""> type ByHand(d:Dictionary<string,int>) = member val dict = d with get,set let text = """{"dict":{"C":3,"D":4}}""" let newt = JsonConvert.DeserializeObject<ByHand> text let prov = ByProv.Parse

Can JsonProvider deserialise to a Generic.Dictionary?

时光毁灭记忆、已成空白 提交于 2021-01-27 07:58:25
问题 I'm learning about type providers and it looks like a ground breaking feature. However, I can't manage to deserialise json with JsonProvider so that the target type has a Generic.Dictionary property. It can be done with Json.NET. Here is the code: type ByProv = JsonProvider<"""{"dict":{"A":1,"B":2}}"""> type ByHand(d:Dictionary<string,int>) = member val dict = d with get,set let text = """{"dict":{"C":3,"D":4}}""" let newt = JsonConvert.DeserializeObject<ByHand> text let prov = ByProv.Parse

How to determine the size of a System.Type value?

心不动则不痛 提交于 2021-01-27 07:57:28
问题 How to determine the size of a Type value? let typ = if true then (123).GetType() else "asdf".GetType();; let sz = sizeof<typ> let sz = sizeof<typ>;; ----------------^^^ error FS0039 One solution is to use Marshal.SizeOf(obj), but may be another solution exists? 回答1: I think Marshal.SizeOf is what you are looking for here. Note that there are two overloads of the method. Marshal.SizeOf(t) (MSDN) takes System.Type as an argument and returns the size of the type that is represented by the

How to determine the size of a System.Type value?

a 夏天 提交于 2021-01-27 07:56:20
问题 How to determine the size of a Type value? let typ = if true then (123).GetType() else "asdf".GetType();; let sz = sizeof<typ> let sz = sizeof<typ>;; ----------------^^^ error FS0039 One solution is to use Marshal.SizeOf(obj), but may be another solution exists? 回答1: I think Marshal.SizeOf is what you are looking for here. Note that there are two overloads of the method. Marshal.SizeOf(t) (MSDN) takes System.Type as an argument and returns the size of the type that is represented by the

F# nameof operator not a first-class function

元气小坏坏 提交于 2021-01-27 07:52:02
问题 I'm using F# 4.7 with <LangVersion>preview</LangVersion> in my project file. I have a type like this: type Record = { Name : string Description : string FieldNotInterestedIn: int } I'd like to get the names of certain fields in a type-safe way, but not all of them. I know I can get all the field names using reflection. Here's the most concise code I came up with. Can it be any more concise? let certainFieldNames = let r = Unchecked.defaultof<Record> [ nameof r.Name nameof r.Description ] 回答1:

F# nameof operator not a first-class function

Deadly 提交于 2021-01-27 07:50:49
问题 I'm using F# 4.7 with <LangVersion>preview</LangVersion> in my project file. I have a type like this: type Record = { Name : string Description : string FieldNotInterestedIn: int } I'd like to get the names of certain fields in a type-safe way, but not all of them. I know I can get all the field names using reflection. Here's the most concise code I came up with. Can it be any more concise? let certainFieldNames = let r = Unchecked.defaultof<Record> [ nameof r.Name nameof r.Description ] 回答1:

Does Async.RunSynchronously method block?

只愿长相守 提交于 2021-01-27 07:01:38
问题 From the documentation it appears that the Async.RunSynchronously runs the async computation and awaits its result. I've also read that it's similar to await in C#. I'm curious if this blocks the thread until it's run to completion? 回答1: Yes, Async.RunSynchronously blocks. A simple illustration: let work = async { printfn "Async starting" do! Async.Sleep(1000) printfn "Async done" } printfn "Main starting" work |> Async.RunSynchronously printfn "Main done" This will print: Main starting Async

Does Async.RunSynchronously method block?

十年热恋 提交于 2021-01-27 07:00:16
问题 From the documentation it appears that the Async.RunSynchronously runs the async computation and awaits its result. I've also read that it's similar to await in C#. I'm curious if this blocks the thread until it's run to completion? 回答1: Yes, Async.RunSynchronously blocks. A simple illustration: let work = async { printfn "Async starting" do! Async.Sleep(1000) printfn "Async done" } printfn "Main starting" work |> Async.RunSynchronously printfn "Main done" This will print: Main starting Async