Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# - Extending .NET BCL nested classes

Tags:

f#

The class System.Collections.Immutable.ImmutableHashSet<'t> has a nested type Builder. We can use it in F# without issues.

What I would like to do is add an extension method to that class in F#, like

type ImmutableHashSet<'t>.Builder with
    member this.NewMember() = ...

but this is not valid F# syntax.

If I extract the fsi I see the following definition

namespace System.Collections.Immutable.ImmutableHashSet`1
type Builder<'T> = ...

however the syntax

type ``System.Collections.Immutable.ImmutableHashSet`1``.Builder with
    member this.NewMember() = ...

is also non valid.

Is there a way to write this extension in F#?

like image 943
Franco Tiveron Avatar asked Jun 14 '26 04:06

Franco Tiveron


1 Answers

Back-ticks are confusing the markdown, so to clarify what @JL0PD wrote, the following seems to work:

open System.Collections.Immutable

type ``ImmutableHashSet`1``.Builder<'a> with
    member _.NewMember() = printfn "Hello world"

let builder = ImmutableHashSet.CreateBuilder<int>()
builder.NewMember()   // Hello world

Exposing the mangled name like this is surprising to me, so it's possible that this only works by accident and could change in the future.

like image 197
Brian Berns Avatar answered Jun 16 '26 22:06

Brian Berns



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!