Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you declare and consume marker interface in f#?

Tags:

f#

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
like image 506
bh213 Avatar asked Jan 04 '09 21:01

bh213


People also ask

Can you declare your own marker interface?

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).

What is the use of marker interface in Java with example?

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.

When would you use a marker 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.


1 Answers

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.

like image 81
wmeyer Avatar answered Oct 10 '22 18:10

wmeyer