Module iface

This module allow creation of interface types. Every interface type has a set of methods that can be called on it. Every object can be converted at runtime to the interface type, provided that there are procs that implement all interface methods.

Example

# Define interface type Duck
type Duck = distinct Interface

interfaceMethods Duck:
  quack(number: int): string

# Make implementation of Duck interface
# Implementation objects have to inherit from RootRef.
type AmericanDuck = ref object of RootObj
  name: string

proc quack(d: AmericanDuck, number: int): string =
  return "duck " & d.name & " quacks " & $number & " times"

let myDuck: AmericanDuck = AmericanDuck(name: name)
let myDuckAsInterface: Duck = myDuck.asDuck # or myDuck.asInterface[Duck]

```

Types

Interface* = object
  vtable*: ptr VTableBase
  obj*: RootRef
  Source Edit
VTableBase* = object {.
inheritable
.} typeName*: string
  Source Edit
SomeInterface* = concept x
    isInterface(x)
Typeclass that matches every interface.   Source Edit

Procs

proc pprintInterface*[T](self: T): string
  Source Edit
proc isNil*(a: SomeInterface): bool
  Source Edit
proc implements*(ty: typedesc; superty: typedesc)
  Source Edit
proc getImpl*(iface: SomeInterface): RootRef
Returns implementation object of this interface.   Source Edit

Macros

macro interfaceMethods*(nameExpr: untyped; body: untyped): untyped
Declare method list of an interface type. (The type should be declared before in a type section as ExampleType = distinct Interface).   Source Edit