Skip to content

Commit 330c1a3

Browse files
CopilotBillWagner
andauthored
Add documentation for empty/marker interfaces in F# (#49048)
* Initial plan * Add documentation for empty/marker interfaces in F# Co-authored-by: BillWagner <[email protected]> * Update docs/fsharp/language-reference/interfaces.md --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: BillWagner <[email protected]> Co-authored-by: Bill Wagner <[email protected]>
1 parent 89c72fe commit 330c1a3

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

docs/fsharp/language-reference/interfaces.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ You can implement one or more interfaces in a class type by using the `interface
7272

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

75+
## Define empty or marker interfaces
76+
77+
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.
78+
79+
You define an empty interface using the `interface end` syntax:
80+
81+
[!code-fsharp[Main](~/samples/snippets/fsharp/lang-ref-1/snippet2806.fs)]
82+
83+
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.
84+
7585
## Calling Interface Methods
7686

7787
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.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Define an empty interface (also known as a marker interface)
2+
type IMarker =
3+
interface end
4+
5+
// Implement the empty interface in a record type
6+
type MyRecord =
7+
{ Name: string }
8+
interface IMarker
9+
10+
// Implement the empty interface in a class type
11+
type MyClass(value: int) =
12+
member _.Value = value
13+
interface IMarker

0 commit comments

Comments
 (0)