Search code examples
fantom

Why Fantom compiler complains about a class that has a Func field?


I wrote a Fantom script that defines a bunch of classes. As I could run the script successfully, I decided to convert this into a proper Fantom project, but one of the classes cannot be compiled and the error message is:

Expected expression, not '|'

The class has this form:

class MyClass
{
    const Func myFunc := |Foo foo, Bar bar| {
        // do stuff
    }

    MyType myVar := MyType()

    Void main() {
        // do more stuff
    }

}

I don't understand why the compiler complains when this class is part of a Fantom project, but doesn't if is part of a Fantom script instead. Can anyone shed some light, please?

Thank you


Solution

  • It's a just a bad error message on Fantom's behalf. It is actually complaining that the classes Foo and Bar don't exist. Add the following to your project and all should compile okay.

    class Foo {}
    class Bar {}
    class MyType {}