Skip to content

New Builtins

revusky edited this page Nov 19, 2023 · 1 revision

As of this writing, FreeMarker 3 introduces exactly one new built-in. That is:

The ?instanceof built-in

It looks like this:

 someValue?instanceof("java.util.List")

And, as you could easily guess, it returns a boolean value that is exactly the same as if you had:

 someValue instanceof java.util.List

in Java code. You do have to use the fully qualified name of the class, since, of course, the FTL layer has no concept of importing packages.

I anticipate that this built-in will quite frequently come in handy in conjunction with assertions. Sometimes when debugging a template, you just want to double-check your understanding of what is going on in a template, so you assert that something is the case, as in:

 #assert thisObject?instanceof("my.business.logic.Client")

You can pepper your template code with things like this to cement your understanding of what is going on at a given juncture in the code.

At the time of this writing, the situation with built-ins is liable to be a tad confusing. One of the things that the "Apache FreeMarker" community actually did over the last decade plus is add plenty of built-ins. They are currently missing in FreeMarker 3 for the part -- I mean any built-in added since about 2009. I think that my approach will be that the squeaky wheel gets the grease. If people start vocally complaining that built-ins that they found very useful are missing, I'll probably put them in -- at least assuming that it is not very technically difficult, which usually it won't be.