DiscoverLearnDocumentationGet OpenPLXSearch Contact

Advanced syntax

Method declarations

A method can be declared indented under a model prefixed with the fn keyword and potentially the static keyword as well, in which case it is put before fn, i.e. static fn. The fn keyword is followed by the name of the method followed by a parameter list. After the parameter list follows an optional return value consisting of a right arrow -> and the return type.

The parameter list is wrapped with parenthesis, (), and each parameter is separated by a comma ,. Each parameter is defined by a name followed by : and a type.

Foo:
    x is Real: 1.0

ModelWithMethods:
    fn my_method(x: Real, y: Foo, z: Bool[]) -> Int
    static fn my_static_method() -> Foo

    # Methods without return types are special and cannot be called from within OpenPLX
    static fn my_void_method(foo: Foo)
    fn onInit()

Operator overloads

To declare an operator overload use the operator keyword followed by the operator to overload, +, -, * or / followed by a parameter list.

Foo:
    x is Real: 1.0

Bar:
    y is Int: 2.0

operator +(lhs: Foo, rhs: Foo) -> Foo
operator -(unary: Foo) -> Foo
operator -(lhs: Foo, rhs: Bar) -> Foo
operator *(lhs: Bar, rhs: Foo) -> Bar
operator /(lhs: Foo, rhs: Foo) -> Bar

Grammar

method   → INDENT ("static")? "fn" IDENTIFIER "(" params? ")" ("->" typeref)
operator → "operator" ("+" | "-" | "*" | "/") "(" params? ")" ("->" typeref)
params   → IDENTIFIER ":" typeref ( "," IDENTIFIER ":" typeref )*
typeref  → (IDENTIFIER ".")* IDENTIFIER ("[]")?