Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/fsharp/language-reference/interfaces.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ You can implement one or more interfaces in a class type by using the `interface

Interface implementations are inherited, so any derived classes do not need to reimplement them.

## Implementing empty or marker interfaces

Empty interfaces, also known as marker interfaces, can be used to identify a set of types without requiring any specific behavior. These interfaces have no members and serve as a way to mark or tag types for categorization purposes.

You define an empty interface using the `interface end` syntax:

[!code-fsharp[Main](~/samples/snippets/fsharp/lang-ref-1/snippet2806.fs)]

In the example above, `IMarker` is defined as an empty interface. Both `MyRecord` and `MyClass` implement this interface without needing to provide any method implementations, since the interface has no members. This allows you to use the interface type to identify and work with these types in a common way.

## Calling Interface Methods

Interface methods can be called only through the interface, not through any object of the type that implements the interface. Thus, you might have to upcast to the interface type by using the `:>` operator or the `upcast` operator in order to call these methods.
Expand Down
13 changes: 13 additions & 0 deletions samples/snippets/fsharp/lang-ref-1/snippet2806.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Define an empty interface (also known as a marker interface)
type IMarker =
interface end

// Implement the empty interface in a record type
type MyRecord =
{ Name: string }
interface IMarker

// Implement the empty interface in a class type
type MyClass(value: int) =
member _.Value = value
interface IMarker
Loading