Extending Core.Option module in F# -
i want extend 1 of existing "core" modules, core.option
:
module microsoft.fsharp.core.option let filter predicate op = match op | some(v) -> if predicate(v) some(v) else none | none -> none
(i know bind
function, think filter
method options in case more convenient).
but unfortunetely can't use filter
method without explicitely open microsoft.fsharp.core
namespace:
// commenting following line break code! open microsoft.fsharp.core let v1 = 42 let v2 = v1 |> option.filter (fun v -> v > 40) printfn "v2 is: %a" v2
in cases can't use functions module without opening appropriate namespace. f# compiler "opens" predefine (core) namespace automatically (like microsoft.fsharp.core
), not bring scope methods "module extensions" , still should open core namespaces manually.
my question is: there workarounds?
or best way extend "core" modules create such extensions in custom namespace , open namespace manually?
// lets custom option module in our custom namespace module customnamespace.option let filter predicate op = ... // on client side lets open our custom namespace. // after can use both option modules simultaneously! open customnamespace let v1 = 42 let b = v1 |> option.filter (fun v -> v > 40) // using customnamespace.option |> option.issome // using microsoft.fsharp.core.option
does if add autoopen attribute module?
[<autoopen>] module microsoft.fsharp.core.option let filter predicate op = match op | some(v) -> if predicate(v) some(v) else none | none -> none
edit
this works, across assembly borders. doesn't work within same assembly:
namespace microsoft.fsharp.core module option = let filter predicate op = match op | some(v) -> if predicate(v) some(v) else none | none -> none [<assembly:autoopen("microsoft.fsharp.core")>] ()
to call assembly:
[<entrypoint>] let main args = let f () = "" |> option.filter (fun f -> true) console.writeline("hello world!") 0
Comments
Post a Comment