Today I came up with an awesome new way to do annotation constraints in Ceylon, essentially using the metamodel types (yes, I mean the metamodel types, not the actual metamodel type instances) as a kind of query language for program elements. The advantages of the new approach are:
- annotation constraints are much more expressive — as expressive as the metamodel, and
- reading annotation values at runtime is more typesafe.
Instead of writing it up here, I just went back and updated Introduction to Ceylon Part 12 to discuss the new approach. You may as well re-read it from the start.
Getting the ConstrainedAnnotation type hierarchy correct was slightly tricky, but fortunately I had the Ceylon compiler's type analyzer helping me. I got inheritance, generics, upper bound type constraints, and covariance/contravariance working in the type analyzer several months ago, but this is the first chance I had to see some value out of that. The type analyzer is already much smarter than me at reasoning about generic types.
P.S. You might notice that all the example Ceylon code on this blog now has syntax highlighting. Thanks, Stephane and Shane!