Marc Clifton
Marc Clifton

Reputation: 766

How to use perhaps dynamic typing instead of Object in F#?

What I have are some types for a publisher-subscriber:

type Subscription<'T> = 'T -> unit
type Subscribers = ConcurrentBag<Object>
type Subscriptions = ConcurrentDictionary<Type, Subscribers>
type PubSub = Subscriptions * PubSubExceptionHandler

What I'd like to improve is the ConcurrentBag<Object> (ideally to ConcurrentBag<Subscription> but the type 'T that the Subscription takes is unknown, so it can't be exposed at the level of type PubSub<'T>. Is my only recourse to use a type constraint? This seems to preclude using simple records, like type Counter = {Ctr : int} as a type that can be subscribed to. I've been looking at static resolved type constraints (the ^T notation) but the solution is eluding me. (And please ignore that I'm using the mutable ConcurrentBag - that shouldn't be relevant to the question.)

Upvotes: 1

Views: 109

Answers (1)

Aslan Varoqua
Aslan Varoqua

Reputation: 191

FSharp.Interop.Dynamic https://www.nuget.org/packages/FSharp.Interop.Dynamic/ Has a DLR for dynamic types

However, the comment by Fyodor about Non-Generic Interfaces would be a good route to take.

Upvotes: 1

Related Questions