基础内容

#Scala基础 ##定义变量val def区别

val x = 1    // call by value
def y = "foo"  // call by name

例如:

def loop:Boolean = loop // 死循环
val x = loop // call by value 定义时就会block
def y = loop // 定义时不会block,用到y的时候才会block    

引用的定义方式:

def and(x:Boolean,y:Boolean) //都是by value的定义方式
def and(x:Boolean,y: => Boolean ) // by name 定义方式

##符号意义:

=>

  • 匿名函数定义,左边参数,右边是函数实现体(x:int)=>{}
  • 函数声明,左边参数类型,右边返回类型(int)=>(string)
  • by-name-parameter,f(p:=>int),p在调用时候才执行
  • case语句中,case x=>y
  • 表示转换

##函数定义方式

  • val f: int=>string = p => “test” p
  • 定义一个函数f,输入类型是int,返回类型是string,=后面是函数实现

##scala Runtime Reflection ###TypeTags

  • TypeTags可以通过objects的形式把类型信息从编译时期保持到运行时期
  • TypeTags总是被编译器生成

###marcros Learning Scala Macros

###Universes反射入口点

##Symbols, Trees, and Types

  • symbols--名称和实体的绑定--class等文件中描述的内容转换为对象后的描述
  • 包含实体所有的信息--runtime / complie time reflection
  • TypeSymbols--代表type,class,trait的申明
  • ClassSymbol--提供class or trait申明的包含信息的访问
  • TermSymbols--val,var,def,object
  • MethodSymbol--代表def的定义,提供检查方法是否是构造器,是否支持可变数组变量
  • moduleSymbol--代表object的定义,
  • free symbols--freeTermSymbol / freetpeSymbol
  • 在反射过程中动态生成的Symbol没有与之对应的class文件

###Types

  • 描述对应符号类型的信息--对象对应的scala的类型和参数的类型是什么

  • 实例types方法:

    • 2.1 t通过universe typeOf
    • 2.2 基本类型Int ,boolean,Any,通过universe
  • type的作用:

    • 3.1 比较两个type的父子关系
    • 3.2  比较两个type是否相同
    • 3.3 查询某个类型的某个成员或者内部类型
  • Trees -- class文件解析后的对象(抽象语法树)--可以修改这些AST对象并反映到代码中

    • file <== > Object
    • html <==>DOm tree
    • class<==>ASTs
    • scala抽象语法表示程序,简称ASTs
  • 使用方式:

    • scala annotations ---表示参数
    • reify -- 特殊方法,输入表达式,返回Abstract syntax trees (ASTs)
    • complie-time reflaction with macros + runtime compilation
    • trees 是不可变的,除了3个字段,pos,symbol,tpe,在tree typechecked 赋值后

##Annotations, Names, Scopes, and More ###Annotations --注解

  • scala 的申明(declarations) 可以被注解,scala.annotation.Annotation的子类实现
  • scala集成了java的annotation system 可以被java 编译器处理
  • scala自定义注解可以继承StaticAnnotation or ClassfileAnnotation
  • annotation的实例保存了类文件的特殊属性
  • java annotations -- 标注在定义上由java 编译器处理
  • scala annotations --scala 编译器处理

###Names-- 对string的简单封装-

  • name有两个子类--有不同的名称空间,两个不同类型的名字可以重名
  • TermName =>object,members
  • TypeName=>classes,traits,type members

###Quasiquotes

  • 提供了简便的方法在运行时生成tree的代码

##Scala泛型各种定义 ###type <== 抽象 type 成员 只有Scala有,在java中没有对应的写法

// 参数方式
trait Collection[T] {
  // ...
}
// 成员方式
trait Collection {
  type T
  // ...
}

##Scala的case类和普通类的区别

###case class 特殊部分:

  • 字段 ==>转换所有构造参数为public readonly (val) 作为 default fields
  • 默认方法 ==> Generate the toString(), equals() and hashcode() methods using all constructor params for each method
  • apply和unapply方法==>使用相同的类名生成companion object 包含appropriate apply() 和 unapply() 方法
  • 可以生成对象不使用new
  • scala实现了Product接口

###apply 和 unapply方法

  • apply=> tuple参数绑定到class 的 --方便简写
  • unapply => 从class抽取参数,返回 Option的tuple <==调用case 匹配的时候自动调用 unapply方法
  • scala中partterns可以独立于class

###Algebraic data type--代数数据类--复杂数据类型(结构化的)

  • 两个常见的代数类型是product(乘积)类型(比如tuples和records)和sum类型,它也被称为”tagged unions”或”variant type”
  • 最大的价值是用于“模式匹配”,即解构一个对象
  • product --乘积--元祖tuples <== 提供统一的方式访问对象, 对象的字段 <==> 字段索引映射
  • Tuple是product的子类

##泛型擦除 Java中的泛型基本上都是在编译器这个层次来实现的。在生成的Java字节码中是不包含泛型中的类型信息的。通过类型参数合并,将泛型类型实例关联到同一份字节码上。

###ClassTag

  • ClassTag[T]保存了被泛型擦除后的原始类型T,提供给运行时的。可以通过runtimeClass调用
  • ClassTag是比TypeTag更弱的一种情况。ClassTag只包含了运行时给定的类的类别信息。而TypeTag不仅包含类的类别信息,还包含了所有静态的类信息。我们在绝大多数情况下会使用ClassTag,因为ClassTag告诉我们运行时实际的类型已经足够我们在做泛型的时候去使用了。
  • ClassTag是我们最常用的。它主要在运行时指定在编译时无法确定的类别的信息。

###There exist three different types of TypeTags:

  • scala.reflect.api.TypeTags#TypeTag. A full type descriptor of a Scala type. For example, a TypeTag[List[String]] contains all type information, in this case, of type scala.List[String].

类型+参数类型 ==>保存

  • scala.reflect.ClassTag. A partial type descriptor of a Scala type. For example, a ClassTag[List[String]] contains only the erased class type information, in this case, of type scala.collection.immutable.List. ClassTags provide access only to the runtime class of a type. Analogous to scala.reflect.ClassManifest.

类型 ==>保存

  • scala.reflect.api.TypeTags#WeakTypeTag. A type descriptor for abstract types (see corresponding subp below).

sample:

scala> typeTag[List[String]]

res12: reflect.runtime.universe.TypeTag[List[String]] = TypeTag[scala.List[String]]

scala> classTag[List[String]]

res13: scala.reflect.ClassTag[List[String]] = scala.collection.immutable.List

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值