Skip to content

Commit

Permalink
document interface default functions
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent committed Aug 16, 2022
1 parent 94cea1c commit dffc120
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion docs/language/interfaces.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ resource interface OuterInterface {
struct interface InnerInterface {}
}
// Declare a resource named `SomeOuter` that implements the interface `OuterInterface`
// Declare a resource named `SomeOuter` that implements the interface `OuterInterface`.
//
// The resource is not required to implement `OuterInterface.InnerInterface`.
//
Expand All @@ -467,3 +467,44 @@ resource SomeOuter: OuterInterface {}
struct SomeInner: OuterInterface.InnerInterface {}
```

## Interface Default Functions

Interfaces can provide default functions:
If the concrete type implementing the interface does not provide an implementation
for the function required by the interface,
then the interface's default function is used in the implementation.

```cadence
// Declare a struct interface `Container`,
// which declares a default function `getCount`.
//
struct interface Container {
let items: [AnyStruct]
fun getCount(): Int {
return self.items.length
}
}
// Declare a concrete struct named `Numbers` that implements the interface `Container`.
//
// The struct does not implement the function `getCount` of the interface `Container`,
// so the default function for `getCount` is used.
//
struct Numbers: Container {
let items: [AnyStruct]
init() {
self.items = []
}
}
let numbers = Numbers()
numbers.getCount() // is 0
```

Interfaces cannot provide default initializers or default destructors.

Only one conformance may provide a default function.

0 comments on commit dffc120

Please sign in to comment.