In a groovy tutorial, I encountered the following code:
class DateTagLib {
def thisYear = {
out << Calendar.getInstance().get(Calendar.YEAR)
}
}
I don’t know what the << means, and I’m having no luck with google.
Edit: I now know that << sometimes is a bit shift. But what does it mean here?
In groovy, the bitwise operators can be overridden with the
leftShift (<<)andrightShift (>>)methods defined on the class. It’s idiomatic groovy to use theleftShiftmethod for append actions on strings, buffers, streams, arrays, etc and thats what you’re seeing here.For example:
leftShiftmethods onOutputStreamwhich are used to append bytes, anInputStream, or anObjectto the stream.List, which also uses it as an appendYou are looking at a grails tag lib, so out represents the page that’s being rendered. The results of this taglib will be added to the output buffer that will be rendered to the client.