Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why scala people don't like annotation?

Tags:

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. lift-json don't use it. lift-record don't use it. Squeryl don't use it. subcut don't use it.(it has annotation for compiler plugin) ... Just named a few.

They use annotation only when they need some compiler magic. @tailrec, @inline, @BeanProperty, @Inject(in subcut) ...

Scala has super flexible type system, trait, implicit and Menifest[X]. So they don't need runtime meta-data?

Is there any scala project use annotation heavily?

p.s. I think the Dynamic should be annotation but not trait.

like image 431
iron9light Avatar asked Dec 22 '11 20:12

iron9light


People also ask

How does @ inline annotation work in Scala?

Using the annotation @inline does not ensure that a method will be inlined, but it will cause the compiler to do it if and only if some heuristics about the size of the generated code are met. When writing Scala code which interoperates with Java, there are a few differences in annotation syntax to note.

Why Scala is a beneficiary language to learn?

Let see why Scala is a beneficiary language to learn and what it offers that you. You can share your own reasons to why Scala with us. 1. Type Inference Scala automatically infers (detects) the data type of an expression partially or fully. This means that we don’t need to declare it ourselves.

How does Scala determine the type of an expression?

Scala automatically infers (detects) the data type of an expression partially or fully. This means that we don’t need to declare it ourselves. Such a facility lets the programmer leave out type annotations while still allowing type checking.

What is Scala programming?

Scala is a language of a functional paradigm. It treats its functions as first-class citizens. This is why it also lets us create higher-order functions- ones that can return a function, or take it as a parameter.


2 Answers

In general, we don't use annotations because we don't really need them for a lot of things.

The few places I've seen annotations used:

  • Extra type systems (e.g. the CPS plugin for delimited continuations or the effect tracking plugin.
  • Interfacing with legacy Java interfaces. (scala-mojo-support)
  • Enforcing/enabling compiler optimisations, like @inline or @tailrec.

In Scala, we don't really need a dependency injection framework, as there's a few ways to do dependency injection that doesn't require an external tool. You can have the DI config separate from core code, but still be written in Scala. See: https://github.com/jsuereth/scala-in-depth-source/blob/master/chapter11/src/main/scala/scalax/config/Test.scala

So the basic answer is, there's nothing wrong with annotations, we just don't need them that often (yet).

like image 136
jsuereth Avatar answered Oct 12 '22 06:10

jsuereth


For me it's often an issue of compiler enforced type safety. Take a look at Squeryl and how it differs from a Java ORM like Hibernate. Where Hibernate would use an @Id annotation to denote a primary key, in Squeryl you create an id member, which is specified by the KeyedEntity trait. Methods that require an entity with a primary key (update & delete for instance) will crash loudly at compile time if one hasn't been defined. There are several other places within Squeryl where typesafe constructs replace annotations like collection mapping and date/time handling.

I think that this is a common way of thinking in the Scala community. Annotations are more of a run time feature and aren't as well regarded. The Scala compiler is very powerful and pushing more of your code into constructs it can validate makes sense for those who are willing to accept the complexity that comes along with that.

like image 32
Dave Whittaker Avatar answered Oct 12 '22 07:10

Dave Whittaker