Why scala people don't like annotation?

后端 未结 4 1174
傲寒
傲寒 2021-02-04 01:01

Attribute in .NET is a very popular feature. And Java added Annotation after 1.5 Annotations are used everywhere, see Java EE and Spring. But few scala library use annotation. l

4条回答
  •  悲&欢浪女
    2021-02-04 01:31

    All the annotations like @tailrec and @inline are compile-time only. They extend StaticAnnotation, which is, AFAIK, the only annotation support in Scala, and they won't be retained at runtime. I think the philosophy is to avoid runtime annotations because those are retrieved via reflection, a world where the compiler can no longer help you beyond standard classes due to type erasure, but most importantly, because it's runtime and the goal is to decide what you're trying to decide at compile-time.

    Look at using annotations to model a JSON output for example: In Java you would know if it works OK when you run your program. In Scala you could use type classes to model how each type is expressed in JSON. If you miss one definition, the compile will tell you. An excellent example is spray-json.

    Dave Whittaker gives another great example in his answer.

提交回复
热议问题