Skip to content

Latest commit

 

History

History
53 lines (40 loc) · 988 Bytes

File metadata and controls

53 lines (40 loc) · 988 Bytes

Traits in TechScript

Traits define a set of methods that can be shared across multiple classes to enable polymorphism.


🏗️ Declaring a Trait

Use the trait keyword. Traits specify method signatures without providing an implementation, or they can optionally provide default implementations:

trait Speaker
    # Method signature (empty body)
    do speak()
    end
end

🧬 Implementing a Trait

Classes implement traits using the with keyword during declaration:

class Dog with Speaker
    do speak()
        say "Woof!"
    end
end

class Cat with Speaker
    do speak()
        say "Meow!"
    end
end

🎨 Polymorphism

You can write functions that accept any object implementing a specific trait:

do make_sound(speaker: Speaker)
    speaker.speak()
end

dog = new Dog()
cat = new Cat()

make_sound(dog) # Woof!
make_sound(cat) # Meow!

For contract checks without implementations, see Interfaces.