Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Give a method its own type parameters

A method may declare type parameters of its own, in addition to those on its class. Arguments determine them just as they do for generic functions. A parameter that appears only in the result must be chosen explicitly:

class Number:
    value: i64

    def convert[T: IntType](self) -> T:
        T(self.value)

def main() -> Result[(), IoError]:
    number = Number(7)
    small: u8 = number.convert[u8]()
    print(small)?
    Ok(())
7

Method parameters cannot shadow class parameters. Constructors and destructors use only the class parameters. See the functions-as-values path for a generic method that accepts a callback.