DiscoverLearnDocumentationGet OpenPLXSearch Contact

Trait syntax

A trait in OpenPLX allows you to extend models laterally. It is similar to an interface in object oriented programming. They are defined by declaring a name with the trait keyword.

trait MyTrait:
    x is Real: 1.0

A trait can extend other traits by using the is keyword.

trait ParentTrait:
    x is Real: 1.0

trait ChildTrait is ParentTrait:
    y is Real: 2.0

Extend a model with a trait

A model can be extended with a trait using the with keyword.

trait MyTrait:
    x is Real: 1.0

Model:
    with MyTrait
    y is Real: this.x

You can also extend a model with multiple traits.

trait TraitA:
    x is Real: 1.0

trait TraitB:
    y is Real: 2.0

Model:
    with TraitA
    with TraitB
    z is Real: this.x + this.y

Traits as types

Traits can also act as types allowing declarations of attributes with trait types.

trait MyTrait:
    x is Real: 1.0

ModelWithTrait:
    with MyTrait

Model:
    model_with_trait is ModelWithTrait
    trait_attribute is MyTrait: model_with_trait

Limitations

Traits can appear very similar to models and the only real limitations is that traits themselves cannot be extended with traits using the with keyword.

When extending a model with multiple traits the traits must not share a common ancestor.

Grammar

trait → "trait" IDENTIFIER
trait → "trait" IDENTIFIER "is" (IDENTIFIER ".")* IDENTIFIER
trait → "trait" IDENTIFIER ":" NEWLINE (INDENT (variable | assignment | method))*
trait → "trait" IDENTIFIER "is" (IDENTIFIER ".")* IDENTIFIER ":" NEWLINE (INDENT (variable | assignment))*