Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
235 views
in Technique[技术] by (71.8m points)

What is the groovy << operator mean in this context?

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?

question from:https://stackoverflow.com/questions/3641219/what-is-the-groovy-operator-mean-in-this-context

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In groovy, the bitwise operators can be overridden with the leftShift (<<) and rightShift (>>) methods defined on the class. It's idiomatic groovy to use the leftShift method for append actions on strings, buffers, streams, arrays, etc and thats what you're seeing here.

For example:

  • The overloaded leftShift methods on OutputStream which are used to append bytes, an InputStream, or an Object to the stream.
  • List, which also uses it as an append

You 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...