I rarely encounter this fight at present with F #, but again, the type of inheritance is much less common with F #, so maybe I was just lucky. Or I have no obvious. Usually, when the compiler complains about not knowing a certain type, I reverse the order of pipes or composition operands, and I am done.
Basically, when calling a function that works like g(fx) , it also works like x |> f |> g or (f >> g) x . But today itโs not ...
Here is the dirty proof of what I mean:
module Exc = open System type MyExc(t) = inherit Exception(t) let createExc t = new MyExc(t) type Ex = Ex of exn type Res = Success of string | Fail of Ex with static member createRes1 t = Ex(createExc(t)) |> Fail
This usually works (at least in my experience). Lines with a "fail" throw:
error FS0001: type mismatch. Waiting for MyExc โ 'a, but considering exn โ Ex. Type "MyExc" does not match type "exn"
It doesn't matter, it's not difficult to manage, but I have to write a lot of code, where composition is a simpler and more understandable approach, and I don't want to write a bunch of useful functions that I need to put everywhere.
I looked at flexible types as I think this is a contravariance problem, but I donโt see how I can apply it here. Any ideas to keep this idiom?
Please note that if I reorder, i.e. like Ex << createExc >> Fail or using the return channel operator, I get the same error in another part.
inheritance contravariance function-composition f # piping
Abel
source share