c#-to-f#

In F# how can I produce an expression with a type of Func<obj>?

元气小坏坏 提交于 2019-12-03 23:58:29
I'm working with an api that requires a value of type Func. (Specifically, I'm trying to call ModelMetadataProviders.Current.GetMetadataForType() . How can I construct that value in F#? When calling a method that takes any delegate of the Func you shouldn't need to explicitly create the delegate, because F# implicitly converts lambda expressions to delegate type (in member calls). I think that just calling the method with lambda function should work (if it doesn't, could you share the error message?) Here is a simple example that demonstrates this: type Foo() = member x.Bar(a:System.Func<obj>)

Translating async-await C# code to F# with respect to the scheduler

自作多情 提交于 2019-12-03 19:11:26
问题 I wonder if this is too a broad question, but recently I made myself to come across a piece of code I'd like to be certain on how to translate from C# into proper F#. The journey starts from here (1) (the original problem with TPL-F# interaction), and continues here (2) (some example code I'm contemplating to translate into F#). The example code is too long to reproduce here, but the interesting functions are ActivateAsync , RefreshHubs and AddHub . Particularly the interesting points are

Argument validation in F# struct constructor

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-03 16:56:22
问题 Here is a trivial C# struct that does some validation on the ctor argument: public struct Foo { public string Name { get; private set; } public Foo(string name) : this() { Contract.Requires<ArgumentException>(name.StartsWith("A")); Name = name; } } I've managed to translate this into an F# class: type Foo(name : string) = do Contract.Requires<ArgumentException> (name.StartsWith "A") member x.Name = name However, I can't translate this to a structure in F#: [<Struct>] type Foo = val Name :

No argument names in abstract declaration?

亡梦爱人 提交于 2019-12-03 15:54:01
问题 This is the typical declaration of an abstract member in F#: abstract member createEmployee : string -> string -> Employee You define the argument types but not their names. Without names, how do you tell what each parameter is when you implement the interface? In other words, how do you know if the interface expects to be implemented as 1- or 2-? 1- member this.createEmployee firstName lastName = ... 2- member this.createEmployee lastName firstName = ... Am I looking the problem from a wrong

Argument validation in F# struct constructor

烈酒焚心 提交于 2019-12-03 06:09:49
Here is a trivial C# struct that does some validation on the ctor argument: public struct Foo { public string Name { get; private set; } public Foo(string name) : this() { Contract.Requires<ArgumentException>(name.StartsWith("A")); Name = name; } } I've managed to translate this into an F# class: type Foo(name : string) = do Contract.Requires<ArgumentException> (name.StartsWith "A") member x.Name = name However, I can't translate this to a structure in F#: [<Struct>] type Foo = val Name : string new(name : string) = { do Contract.Requires<ArgumentException> (name.StartsWith "A"); Name = name }

F# equivalent of the C# typeof(IEnumerable<>)

眉间皱痕 提交于 2019-12-03 02:02:48
I have a piece of code where I need to figure out if a given type implements IEnumerable<T> (I don't care about the T) I've tried ( t:System.Type in case you wonder) let interfaces = t.GetInterfaces() let enumerbale = interfaces.Any(fun t -> t.GetGenericTypeDefinition() = typeof<IEnumerable<>> ) however that won't compile (the compile doesn't like the <>). I then tried let interfaces = t.GetInterfaces() let enumerbale = interfaces.Any(fun t -> t.GetGenericTypeDefinition() = typeof<IEnumerable<'a>> ) but get's a warning that 'a is constraint to obj. I Don't want to figure out if IEnumerable<obj

XML documentation for interface methods and record properties

点点圈 提交于 2019-12-01 21:50:21
It seems the XML documentation works fine for the most cases, but not always. I wanted to make the Intellisense fully available for the parts that are designed for interoperating with C#. So, here's a small (and maybe a bit contrived) example: ///<summary>Well, it's a summary</summary> type Summary = { ///<summary>Gets a short name</summary> Name : string; ///<summary>Gets whether the action was successful or not</summary> IsSuccessful : bool; } ///<summary>Represents path filtering action</summary> type IPathFilter = ///<summary>Runs the filtering through the list of <paramref name="paths"/><

C# to F# Convert public partial class Device : MarshalByRefObject

人盡茶涼 提交于 2019-12-01 18:41:49
public partial class Device : MarshalByRefObject { internal bool FindTagName(string name, OneTag tag) { foreach (FuncSect fs in listFuncSect) { foreach (OneTag ot in fs.listTags) { if (ot != tag && ot.Name == name) return true; } } return false; } still have no idea how to convert this "partial" and "internal" to F# thank you As leppie says, there's no direct support for partial , although you could achieve a similar effect with intrinsic type extensions . F# does support internal methods, so your example would look like: // primary definition somewhere type Device() = inherit

Mono : is there a System.Tuple?

老子叫甜甜 提交于 2019-12-01 17:35:07
I am trying to do some interop between C# and F# in Mono. Is there a System.Tuple in Mono C#? I can see the one in Mono.CSharp, but that doesn't seem to be the same type as F# (a' * b'). So, (a) Is there a System.Tuple in Mono C# or (b) Is there a cast between tuples in Mono C# and F#? Yes Mono supports a Tuple type. I know it's in 4.0 but I've seen comments of it's availability since version 2.6. Tuple Documentation It also depends on what version of F# compiler do you use. If you compile your F# code using F# compiler for .NET 2.0, then it will use FSharp.Core.dll for .NET 2.0, which

Mono : is there a System.Tuple?

ぃ、小莉子 提交于 2019-12-01 16:32:09
问题 I am trying to do some interop between C# and F# in Mono. Is there a System.Tuple in Mono C#? I can see the one in Mono.CSharp, but that doesn't seem to be the same type as F# (a' * b'). So, (a) Is there a System.Tuple in Mono C# or (b) Is there a cast between tuples in Mono C# and F#? 回答1: Yes Mono supports a Tuple type. I know it's in 4.0 but I've seen comments of it's availability since version 2.6. Tuple Documentation 回答2: It also depends on what version of F# compiler do you use. If you