:: Article Deprecated!

:: The issue mentioned in this article has been fixed in F4 v1.1.

:: Article Deprecated!

F4 Logo F4 by Xored is an awesome IDE for Fantom, I use it every day - but it does have it's quirks...

One quirk is the keyword facet.

In Fantom, the facet keyword is used to denote a facet class, as in:

facet class MyFacet { }

Which is fine. facet is a language keyword and F4 respects that.

But for some inexplicable reason Fantom also created methods named facet() to the Type and Slot classes! And F4 has trouble with these. If you try to use these methods, even though the code is valid, F4 reports a compile Err.

select all
@MyFacet
class MyClass {

  @MyFacet
  Str? myField

  Void main() {
    classFacet := (MyFacet) MyClass#.facet(MyFacet#)         // --> compile Err in F4
    fieldFacet := (MyFacet) MyClass#myField.facet(MyFacet#)  // --> compile Err in F4
  }
}

The problem is reported as an issue on F4's GitHub but until it is fixed and released, we need a workaround.

Xored (Ivan) suggested using a command-line builder in F4 but if you ever tried it, you'd know it's super slow and not an option. Instead I suggest a code workaround.

My first thought was to use the facets() method (note the additional s) to loop over all the facets, returning the one we want:

Void main() {
  classFacet := (MyFacet) MyClass#.facets.findType(MyFacet#).first
  fieldFacet := (MyFacet) MyClass#myField.facets.findType(MyFacet#).first
}

This is fine until you read the docs for the facets() method:

... If looking up a facet by type, then use the 'facet'
    method which will provide better performance ...

Okay, so not such a great idea after all!

Next we'll try using reflection to call facet(). Thankfully, because we're coding in Fantom and method overloading is not allowed, this is a breeze!

Void main() {
  classFacet := (MyFacet) Type#.method("facet").callOn(MyClass#, [MyFacet#])        // Stoopid F4
  fieldFacet := (MyFacet) Slot#.method("facet").callOn(MyClass#myField, [MyFacet#]) // Stoopid F4
}

Ta daa!

Now we can still call the facet() method in F4 without performance loss!

Edits:

  • 18 Aug 2016 - Deprecated.

Discuss