Some examples related to my tweet rant https://twitter.com/dsymetweets/status/1294276915260522496
In project programming this hit me this week with a bug:
using System; | |
using System.Threading.Tasks; | |
namespace System.Collections.Concurrent | |
{ | |
public static class ConcurrentDictionaryExtensions | |
{ | |
/// <summary> | |
/// Provides an alternative to <see cref="ConcurrentDictionary{TKey, TValue}.GetOrAdd(TKey, Func{TKey, TValue})"/> that disposes values that implement <see cref="IDisposable"/>. | |
/// </summary> |
// General hints on defining types with constraints or invariants | |
// | |
// Just as in C#, use a private constructor | |
// and expose "factory" methods that enforce the constraints | |
// | |
// In F#, only classes can have private constructors with public members. | |
// | |
// If you want to use the record and DU types, the whole type becomes | |
// private, which means that you also need to provide: | |
// * a constructor function ("create"). |
Some examples related to my tweet rant https://twitter.com/dsymetweets/status/1294276915260522496
In project programming this hit me this week with a bug:
type StepfulBuilder() = | |
member _.Zero() = () | |
member _.Yield x = x | |
[<CustomOperation("toInt")>] | |
member inline _.ToInt(_, value) = | |
int value | |
[<CustomOperation("transform")>] | |
member _.Transform(x, f) = |
type EventStream<'T> ()= | |
let evt=Channel.CreateUnbounded<'T>() | |
let writer = evt.Writer | |
let reader = evt.Reader | |
let mutable key = 0 | |
let mutable subscriptions = Map.empty : Map<int, IObserver<'T>> | |
let thisLock = new obj() | |
let obs = |
#r @"C:\Users\ayrat.hudaygulov\.nuget\packages\fsharp.compiler.service\26.0.1\lib\net45\FSharp.Compiler.Service.dll" | |
open System.IO | |
open Microsoft.FSharp.Compiler.Ast | |
open Microsoft.FSharp.Compiler.SourceCodeServices | |
let parse text = | |
let fileName = "whatever.fs" | |
let checker = FSharpChecker.Create() | |
let opts = |
(* | |
WHAT'S GOING ON HERE?! | |
Sometimes you don't care about a particular type, you're interested in one field only, let's say `EntityId`. | |
Instead of using interface (which isn't even possible if don't own a type), | |
we can do structural typing in F# using SRTP and Active Patterns. | |
Active patterns are not required for this, but they do make code much easier to use. | |
*) | |
// So we have 2 types with field `EntityId: string`: |