How do you replicate this in F#?
interface IMarker
{
// No members here
}
class MyClass : IMarker
{
// can contain code
}
Update: The following code does define marker interface, but none of the answers so far manages to producde class that implements this marker interface (see MyClass above)
type IMarker = interface end
Can we create custom marker interface? Of course you can write a marker interface. A marker interface is generally just a Interface with no methods at all (so any class could implement it).
It is also known as a tagging interface and is used to indicate or inform the JVM that a class implementing this interface will have some special behaviour. An efficient way to classify code can be achieved using the marker interface. Examples of such an interface are: Serializable, Cloneable and Remote Interface.
Marker interface is used as a tag that inform the Java compiler by a message so that it can add some special behavior to the class implementing it.
An old question, but the required syntax (F# 2.0) for this special case is actually:
type IMarker = interface end
type Marker =
class
interface IMarker
end
Or alternatively (including a constructor for Marker):
type IMarker = interface end
type Marker() =
interface IMarker
Just writing
type Marker =
interface IMarker
is not possible because it looks like you want to create a new interface type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With