Scala - Annotations

This chapter we will introduce the annotations, annotations are structured added to program source code. Like comments , they can be sprinkled throughout a program and attached to any variable, method, expression, or other program. Unlike comments, they have structure, thus making them easier to machine process. 

Why we need annotations. 

such tools are called meta-programming code, becasue they are programs that take other programming as input, Annotions support these tools by letting the programmer 
sprinkle directives to the tool throughout their source code. 

  • A documentation generator could be instructed to document certain methods are deprecated 
  • a pretty printer could be instructed to skip over parts of the program that have been carefully hand formatted. 
  • A checker for non-closed files could e instructed to ignore a particular file that has been manually verified to be closed. 
  •  A side-effects checker could be instructed to verify that a specified method has no side-effects .

Syntax of annotations. 

let's take the @depecation for examples. 


@deprecated def bigMistake() = // ...


// a method is annotated as @deprecated. Annotation are allowed in other places, annotations are allowed on any kind of declaration or definitions . 
// Trait, class, method, defs, objects, traits, vals, vars, and types.  
@deprecated class QuickAndDirty { 
  // ...
}

and as with the @unchecked annotation for examples. 

// annotation can also applieed to expressions. 
(e : @unchecked) match { 
  // non-exhaustive errors...
}

and the general annot is the general annotation is like ? 

// general form of annotation is
//  
@annot(expr1, expr2)
the precise argument to the annotation can be any arbitrary expression, as long as they type check . 

// @annot is like a new constructor calles. 
@cool val normal = "hello"

@coolerThan(normal) val fronzy = "heeyyy"
while you cannot directly equal annotation to the constructor because the @annot is a expression.

// while you cannot directly equal annotation to the constructor 
// because the @annot is a expression 
//
import annotation._
class Strategy(arg : Annotation) extends Annotation 

class delayed extends Annotation 

@strategy(@delayed) def f() { }

@strategy(new delayed) def f() { }

Standard annontations

First is the @deprecated is like this:  

// Deprecation 
//

@deprecated def bigMistake() = // ... 

@deprecated("use newShinyMethod() instead")
def bigMistake() = //...
to test the deprecation scalac 

// scalac -depecation Deprecation2.scala 
//   you will get message on the deprecation
Volatile fileds 

// Volatile fields
// this is for concurrent programming
Binary serialization 

// Binary serialization 
// @serializable 
// serialize XML .
BeanProperty 

// @BeanProperty 
// @scala.reflect.BeanProperty  annotation 
// automatically get and set .
TailRec /tailrec tail recursive. 

// @TailRec
// @tailrec
// tell the optimize the tail recursive.
Unchecked 

// @unchecked 
// it tells the compiler not to worry if the match expression seems to leave out some cases, See Section 15.5 for details.
Native methods 

// Native methods 
//   @native anotations informs the compiler that a method's implemenation that is supplied by the runtime rather than in Scala code. 
// and it will be up to the developer to supply the implementation using a mechanism such as the a java Native Interface (JNI).
// 
@native 
def beginCountdown() { }




// annotation can also applieed to expressions. (e : @unchecked) match { // non-exhaustive errors... }

转载于:https://my.oschina.net/u/854138/blog/142965

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值