Internal Types for .NET Plugin Loading

Viewed 69

I'm developing a plugin-loading system for my F# .NET 5 app, following this Microsoft guide. I wanted to provide an interface ICore that would have some internal, opaque state type that I would provide an interface for manipulating.

In Scala, I might do this like

trait ICore {
  type State

  def reset: State
}

and then call this method like

def resetCoreState(c: Core): c.State = c.reset

and implement a core like

object StringCore extends ICore {
  override type State = String

  override def reset: String = ""
}

however F# doesn't have this feature.

How should I design my interface,

type ICore() =
  interface

  // abstract type State

  abstract method reset : State

  end

let resetCoreState : ???

type StringCore() = ???

so that plugins can manage their own state?

1 Answers

I don't think there's any simple way to make the state type opaque, but here's how I would define these types in F#:

type ICore<'state> =
    abstract member Reset : 'state

type StringCore() =
    interface ICore<string> with
        member __.Reset = ""

let resetCoreState (core : ICore<_>) =
    core.Reset
Related