首页
登录 | 注册

Scala11

Extractor

  • 这一章主要讲 Extractor,extractor使得你可以定义自己的模式,而不用创建对应的 case class,即: Extractor let you define new patterns for preexisting types, where the pattern need not follow the internal representation of the type.

  • An extractor in Scala is an object that has a method called unapply as one of its members. The purpose of that unapply method is to match a value and take it apart. Often, the extractor object will define a method apply for building vlaues, but that’s optional.

  • Whenever pattern matching encounters a pattern referring to an extractor object, it invokes the extractor’s unapply method on the selector expression.

  • The apply method is called an injection, because it takes some arguments and yields an element of a given set. The unapply method is called an extraction, because it takes an element of the same set and extracts some if its parts.

  • 定义 unapplySeq ,这样就可以匹配任意长的参数了

  • 在模式匹配中,我们可以使用 sequence patterns 来匹配 List或者 Array是像 scala.List

companion object is an extractor that defines an unapplySeq method. unapplySeq method that returns all elements of the list as a sequence. That’s what supports List(…) patterns.

Extractors versus case classes
  • case class的缺点在于:They expose the concrete representation of data. This means that the name of the class in a constructor pattern corresponds to the concrete represetation type of the selector object.

  • Extractor具有 representation independence,所以不存在这样的问题

  • 不过case class也有自己的好处: First, they are much easier to set up and to define, and they require less code. Second, they usually lead to more efficient pattern matches than extractor. Third, if your case classes inherit from a sealed base class, the Scala compiler will check your pattern matches for exhaustiveness and will complain if some combination of possible vlaues is not covered by a pattern.

Regular expressions
  • 正则表达式有定义 unapplySeq 方法,利用这个方法,可以按组将已匹配的文本提取出来
Conclusion
  • Extractors let you define your own kinds of patterns, which need not correspond to the type of the expressionyou select on.This gives you more flexibility in the kinds of patterns you can use for matching. In effect it’s like having different possible views on the same data. It also gives you a layer between a type’s representation and the way clients view it. This lets you do pattern matching while maintaining representation independence, a property which is very useful in large software systems.

  • 感觉 extractor 也是一个好用的工具,不过自己现在还是理解的模模糊糊的,还是要写写代码来理解

Annotations

  • 这一章主要讲什么是注解以及为什么需要注解,不涉及如何设计自己的注解

  • Annotations are structrured information added to program source code.

  • 注解被称为所谓的元编程工具。对于Scala编译器来说,The compiler understands just one feature, annotations, but it doesn’t attach any meaning to individual annotations.

  • Annotations are allowed on any kind of declaration or definition, including vals, vars, defs, classes, objects, traits and types

  • 注解的一般形式如下:

@annot(exp1, exp2, ...) { val name1=const1, ... }

The annot specifies the class of annotation. All annotations must include that much. The exp parts are arguments to the annotation.

Standard annotations
  • Deprecation

  • Volatile fields

  • Binary serialization

  • Automatic get and set methods – 添加了这个注解后,编译器会把你生成 get 和 set 方法,但是要注意这些 get 和 set 方法只有在编译完成后才能使用

  • Unchecked – It tells the compiler not to worry if the match expression seems to leave out some cases.



2020 jeepxie.net webmaster#jeepxie.net
10 q. 0.009 s.
京ICP备10005923号