hd-lang

Tour · 9 of 18

Traits and generics

The program on the right adds up prices for books and for services.

#Traits

trait Priced:
    fn cents(&self) -> i32
    fn label(&self) -> string

A trait names behavior that many types can share.

#Implementations

impl Priced for Book:
    fn cents(&self) -> i32: self.price
    fn label(&self) -> string: "book"

impl Priced for Book opts Book into the trait. The compiler checks that it writes every required method. Methods outside any trait go in a plain impl Book: block.

#Generic functions

fn total[T < Priced](items: List[T]) -> i32:
    let sum = +0
    for item in items:
        sum += item.cents()
    sum

total works for any T with the bound T < Priced. It knows nothing about books or services, only that each item has cents().

Run it. The books cost 1200 and 800, so it prints 2000. Then it prints 5000 for the service.

Try it: delete the line fn cents(&self) -> i32: self.price and press Run.

Run: Ctrl+Enter · Pages: Alt+← Alt+→