c#-to-f#

Type inheritance in F#

故事扮演 提交于 2019-12-07 08:25:23
问题 I can't find the proper syntax for coding a type D that inherits a base class B (written in C#) and his constructors other than the base class implicit constructor: C# code: public class B { private int _i; private float _f; public B() { _i = 0; _f = 0.0f; } public B(int i) { _i = 0; _f = 0.0f; } public B(int i, float f) { _i = i; _f = f; } } F# code: type D() = inherit B() //how to inherit from other constructors ? Thanks 回答1: I found a way to do it thanks this blog ! type D = class inherit

Type inheritance in F#

谁说胖子不能爱 提交于 2019-12-05 14:38:26
I can't find the proper syntax for coding a type D that inherits a base class B (written in C#) and his constructors other than the base class implicit constructor: C# code: public class B { private int _i; private float _f; public B() { _i = 0; _f = 0.0f; } public B(int i) { _i = 0; _f = 0.0f; } public B(int i, float f) { _i = i; _f = f; } } F# code: type D() = inherit B() //how to inherit from other constructors ? Thanks I found a way to do it thanks this blog ! type D = class inherit B new () = { inherit B() } new (i : int) = { inherit B(i) } new ((i,f) : int*single) = { inherit B(i, f) }

Converting C# code to F# (if statement)

天大地大妈咪最大 提交于 2019-12-05 07:46:20
I'd like to know how to convert this code line by line from C# to F#. I am not looking to use any kind of F#'s idioms or something of the like. I am trying to understand how to map directly C#'s constructs to F#. Here is the C# code: //requires l.Length > 0 int GetMinimumValue(List<int> l) { int minVal = l[0]; for (int i = 0; i < l.Length; ++i) { if (l[i] > minValue) { minVal = l[i]; } } return minVal; } And here is my F# attempt: let getMinValue (l : int list) = let minVal = l.Head for i = 0 to (l.Length-1) do if (l.Item(i) > minVal) then minVal = col.Item(i) minVal Now, this ain't working.

C#-style event accessors for CLI events in F#

牧云@^-^@ 提交于 2019-12-05 03:43:22
I am exposing an event from F# to C# like this: let event = new DelegateEvent<EventHandler>() member x.Ping() = event.Trigger([| x; EventArgs.Empty |]) [<CLIEvent>] member x.PingEvent = event.Publish But I'd like some code to run whenever handlers are added or removed. I think this can be done in C# like this: public event EventHandler PingEvent { add { //do something } remove { //do something } } How do I write the above in F#? In F#, you can define events with implementations of the IDelegateEvent<_> interface (or the IEvent<_> or IEvent<_,_> interfaces which derive from it). So you can do

What is F# equivalent of C# “public event ”

旧城冷巷雨未停 提交于 2019-12-05 00:52:56
问题 I'm trying to translate to F# code from this article http://www.codeproject.com/Articles/35532/C-COM-Object-for-Use-In-JavaScript-HTML-Including I've stumbled on this lines: public delegate void MyFirstEventHandler(string args); public event MyFirstEventHandler MyFirstEvent; I've tried to translate this in F# as: type MyFirstEventHandler = delegate of string -> unit type MyFsComComponent () = let my_event = new Event<MyFirstEventHandler,string> () [<CLIEvent>] member x.MyFirstEvent = my_event

How to consume HttpClient from F#?

ぃ、小莉子 提交于 2019-12-05 00:40:57
I'm new to F# and stuck in understanding async in F# from the perspective of a C# developer. Say having the following snippet in C#: var httpClient = new HttpClient(); var response = await httpClient.GetAsync(url); response.EnsureSuccessStatusCode(); string content = await response.Content.ReadAsStringAsync(); How to write the same in F#? Here is a function that should do what you're looking for (note that you'll have to wrap the code in an asynchronous computation expression in order to use the let! syntax): let getAsync (url:string) = async { let httpClient = new System.Net.Http.HttpClient()

Global constants in F# - how to

心不动则不痛 提交于 2019-12-04 16:39:40
问题 I need to set a version number to be used in the AssemblyVersion attribute by several related projects. In C# I use the following public class Constants { public const string Version = "1.2.3.4"; } then it can be used as follows: [assembly:AssemblyVersion(Constants.Version)] What would be the equivalent construct in F#. All my attempts to come up with a binding which can be accepted as an attribute argument did not work. 回答1: Use the attribute Literal: [<Literal>] let version = "1.2.3.4" [

Simple XNA 2D demo: why is my F# version slower than C# version?

六眼飞鱼酱① 提交于 2019-12-04 13:09:43
When running this XNA application it should display a rotated rectangle that moves from top-left corner to bottom-right corner. It looks like my F# version is noticeably much slower. It seems that the Draw method skips a lot of frames. I am using VS 2012 RC, XNA 4.0, .NET 4.5, F# 3.0. I am trying to make it as functional as possible. What could be the reason for poor performance? C#: class Program { static void Main(string[] args) { using (var game = new FlockGame()) { game.Run(); } } } public class FlockGame : Game { private GraphicsDeviceManager graphics; private DrawingManager

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

前提是你 提交于 2019-12-04 08:47:57
问题 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

F# Equivalent of Destructor

不想你离开。 提交于 2019-12-04 00:28:45
I am translating a C# class that wraps an unmanaged library to F#. I have run into the seemingly simple problem of rewriting the destructor that follows. class Wrapper { // P/Invoke ellided private SomeType x; public Wrapper() { x = new SomeType(); Begin(); } public ~Wrapper() { End(); } The simplified F# code I have at this point is as follows: type Wrapper() = [<Literal>] static let wrappedDll = "Library.dll" [<DllImport(wrappedDll , EntryPoint = "Begin")>] static extern void Begin() [<DllImport(wrappedDll , EntryPoint = "End")>] static extern void End() let x = new SomeType() do Begin() How