f#

Extension methods for specific generic types

二次信任 提交于 2019-12-10 01:59:00
问题 I'm attempting to create various extension method for a generic type bound to specific generic type parameters in F#, but the language does not seem to be allowing me: What I want to do is something like the following: type IEnumerable<int> with member this.foo = this.ToString() Yet it gives me the compiler error (underlining the int keyword): Unexpected identifier in type name. Expected infix operator, quote symbol or other token. The following does work, though it does not specifically bind

F# - Can I return a discriminated union from a function

半城伤御伤魂 提交于 2019-12-10 01:50:06
问题 I have the following types: type GoodResource = { Id:int; Field1:string } type ErrorResource = { StatusCode:int; Description:string } I have the following discriminated union: type ProcessingResult = | Good of GoodResource | Error of ErrorResource Then want to have a function that will have a return type of the discriminated union ProcessingResult: let SampleProcessingFunction value = match value with | "GoodScenario" -> { Id = 123; Field1 = "field1data" } | _ -> { StatusCode = 456;

F#, Pipe forward a match case without using temp variable

两盒软妹~` 提交于 2019-12-10 01:50:05
问题 I would like to pipe-forward a variable to a match case without using a temp variable or a lambda. The idea: let temp = x |> Function1 |> Function2 // ........ Many functions later. |> FunctionN let result = match temp with | Case1 -> "Output 1" | Case2 -> "Output 2" | _ -> "Other Output" I hope to write something similar to the following: // IDEAL CODE (with syntax error) let result = x |> Function1 |> Function2 // ........ Many functions later. |> FunctionN |> match with // Syntax error

How do I do I execute tests in Debug mode using .Net Core and VSCode?

喜夏-厌秋 提交于 2019-12-10 01:39:59
问题 How do I execute tests in Debug mode using .Net Core and VSCode? I am currently running the following on the command line: dotnet Test However, this is not executing the tests in debug mode. Do I attach a debugger? If so... How? 回答1: If necessary, convert the test project to be a console app, instead of a library. For example, use <TargetFramework>netcoreapp2.0</TargetFramework> Add a main method or function. // C# class Program { static void Main(string[] args) { } } // F# module Program = [

Why does the F# compiler fail with this infix operator?

…衆ロ難τιáo~ 提交于 2019-12-10 01:34:59
问题 I have a class and a record defined like so: namespace Foo type internal MyRecord = { aValue1 : int aValue2 : int } static member (+) (left : MyRecord, right : MyRecord) : MyRecord = {aValue1 = left.aValue1 + right.aValue1; aValue2 = left.aValue2 + right.aValue2;} type internal Bar() = member this.Baz() = let myRecord1 = {aValue1 = 2; aValue2 = 3;} let myRecord2 = {aValue1 = 7; aValue2 = 5;} let sum = myRecord1 + myRecord2 //Does not compile 0 This fails to compile with: The member or object

“Consider app.config remapping of assembly …” warning in F#

我怕爱的太早我们不能终老 提交于 2019-12-10 01:06:35
问题 After I installed VS11, I started to get the following error: Consider app.config remapping of assembly "FSharp.Core, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" from Version "2.0.0.0" [C:\Program Files (x86)\Reference Assemblies\Microsoft\FSharp\2.0\Runtime\v2.0\FSharp.Core.dll] to Version "4.0.0.0" [C:\Program Files (x86)\Reference Assemblies\Microsoft\FSharp\2.0\Runtime\v4.0\FSharp.Core.dll] to solve conflict and get rid of warning. C:\Windows\Microsoft.NET\Framework\v4.0.30319

How to do multiline lambda expressions in f#?

空扰寡人 提交于 2019-12-10 00:57:11
问题 How would I do this (C#) in F# public class MyClass { void Render(TextWriter textWriter) { Tag(() => { textWriter.WriteLine("line 1"); textWriter.WriteLine("line 2"); }); Tag(value => { textWriter.WriteLine("line 1"); textWriter.WriteLine(value); }, "a"); } public void Tag(Action action) { action(); } public void Tag<T>(Action<T> action, T t) { action(t); } } 回答1: A multi-line lambda in F# is just (fun args -> lots of code here ) The whole code would be something like open System.IO type

Compiler bug in F# 4?

可紊 提交于 2019-12-10 00:47:55
问题 I have some F# 4.0 source that compiles fine in Debug, but not in Release. There are no conditional defines, no changes in the inferred types, and nothing else I can think of, that can explain this difference to me. Did I really stumble on a compiler bug? This is a snippet with the problematic code. let oldItems = userDisplayItems |> Seq.toList for newItem in newItems do match List.tryFind (fun (itemOfOld: UserDisplay.UserDisplayItem) -> itemOfOld.Id = newItem.Id) oldItems with | Some oldItem

F#: combining together discriminated unions and class hierarchies?

江枫思渺然 提交于 2019-12-09 20:05:43
问题 Let's say I have a significant class hierarchy: Tag ControlFlowTag IfTag ForTag JumpTag HTMLTag DivTag and I want to make a list interspersed with these and strings. let MyList = [tagA, tagB, "some text", tagC] and I thought I could discriminated union it type Node = | Tag of Tag | String of String let MyList: list<Node> = [tagA, tagB, "some text", tagC] but alas, it doesn't work without let MyList: list<Node> = [Tag tagA, Tag tagB, String "some text", Tag tagC] Obviously the Tag and String

F# Async.RunSynchronously with timeout and cancellationToken

社会主义新天地 提交于 2019-12-09 18:32:03
问题 When calling Async.RunSynchronously with a timeout and a CancellationToken, the timeout value seems to be ignored. I can work around this by calling CancelAfter on the CancellationToken, but ideally I'd like to be able to distinguish between exceptions that occur in the workflow, TimeOutExceptions and OperationCanceledExceptions. I believe the sample code below demonstrates this. open System open System.Threading let work = async { let endTime = DateTime.UtcNow.AddMilliseconds(100.0) while