scala符号<:和: =>以及_*等
2017-06-11 23:03
309 查看
1.(1)函数传名调用(call-by-name)符号:
=>名字作为一个参数
(2)R
<: { def close():Unit }继承,R是子类,这里继承方法close():Unit
例子
objectmanage { def apply[R <: { def close():Unit },T](resource: => R)(f: R => T): T = { var res: Option[R] = None try { res = Some(resource) // Only reference "resource"once!! f(res.get) // Return the T instance } catch { case NonFatal(ex) => println(s"manage.apply(): Nonfatal exception! $ex") throw ex } finally { if (res != None) { println(s"Closingresource...") res.get.close } } } } objectTryCatchARM { /** Usage: scala rounding.TryCatch filename1filename2 ... */ def main(args: Array[String]) = { val sizes = args map (arg =>returnFileLength(arg)) println("Returned sizes: " +(sizes.mkString(", "))) } import scala.io.Source def returnFileLength(fileName: String): Int ={ println() // Add a blank line for legibility manage(Source.fromFile(fileName)) { source=> val size = source.getLines.size println(s"file $fileName has $sizelines") if (size > 200) throw newRuntimeException(s"Big file: $fileName!") size } } }
2.case +:(head, tail)或者
case head+:tail
1.(1)函数传名调用(call-by-name)符号:
=>名字作为一个参数
(2)R
<: { def close():Unit }继承,R是子类,这里继承方法close():Unit
例子
3 Types with two typeparameters(两个类型参数) can be written withinfix notation(中缀表示)
case classWith[A,B](a: A, b: B) //val fw1 ="Foo" With 1 // Doesn'twork // Butnotice the following type annotations: val with1:With[String,Int] = With("Foo", 1) val with2:String With Int = With("Bar",2) List(with1,with2, "test","today") foreach { w => w match { case s With i => println(s"$s with$i") case v => println(s"Unknown:$w") } }
4. _*其余的变量参数, 参数序列
valnonEmptyList = List(1, 2, 3, 4, 5) // <1> valemptyList = Nil valnonEmptyMap = Map("one"-> 1, "two" -> 2, "three" -> 3) // Processpairs defwindows[T](seq: Seq[T]): String = seq match { case Seq(head1, head2, _*) => //<2> s"($head1, $head2), " +windows(seq.tail) // <3> case Seq(head, _*) => s"($head, _), " +windows(seq.tail) // <4> case Nil => "Nil" } for (seq<- Seq(nonEmptyList, emptyList, nonEmptyMap.toSeq)) {
object Opextends Enumeration { //<1> type Op = Value val EQ = Value("=") val NE = Value("!=") val LTGT = Value("<>") val LT = Value("<") val LE = Value("<=") val GT = Value(">") val GE = Value(">=") } import Op._ //Represent a SQL "WHERE x op value" clause, where +op+ is a // comparisonoperator: =, !=, <>, <, <=, >, or >=. case classWhereOp[T](columnName: String, op: Op, value: T) // <2> //Represent a SQL "WHERE x IN (a, b, c, ...)" clause. case classWhereIn[T](columnName: String, val1: T, vals: T*) // <3> val wheres= Seq( // <4> WhereIn("state", "IL","CA", "VA"), WhereOp("state", EQ,"IL"), WhereOp("name", EQ, "BuckTrends"), WhereOp("age", GT, 29)) for (where<- wheres) { where match { case WhereIn(col, val1, vals @ _*)=> //<5> val valStr = (val1 +:vals).mkString(", ") println (s"WHERE $col IN($valStr)") case WhereOp(col, op, value) => println(s"WHERE $col $op $value") case _ => println (s"ERROR: Unknownexpression: $where") } }println(windows(seq))}
5. @ _*变量参数列表, vals: T*变长参数
object Op extends Enumeration { // <1> type Op = Value val EQ = Value("=") val NE = Value("!=") val LTGT = Value("<>") val LT = Value("<") val LE = Value("<=") val GT = Value(">") val GE = Value(">=") } import Op._ // Represent a SQL "WHERE x op value" clause, where +op+ is a // comparison operator: =, !=, <>, <, <=, >, or >=. case class WhereOp[T](columnName: String, op: Op, value: T) // <2> // Represent a SQL "WHERE x IN (a, b, c, ...)" clause. case class WhereIn[T](columnName: String, val1: T, vals: T*) // <3> val wheres = Seq( // <4> WhereIn("state", "IL", "CA", "VA"), WhereOp("state", EQ, "IL"), WhereOp("name", EQ, "Buck Trends"), WhereOp("age", GT, 29)) for (where <- wheres) { where match { case WhereIn(col, val1, vals @ _*) => // <5> val valStr = (val1 +: vals).mkString(", ") println (s"WHERE $col IN ($valStr)") case WhereOp(col, op, value) => println (s"WHERE $col $op $value") case _ => println (s"ERROR: Unknown expression: $where") } }注:本文例子来自programming scala
相关文章推荐
- Scala 的那些奇怪的符号 (一):“<:” 和 “>:” 作用及用法
- <2> UML类图符号以及各种关系
- 【HTML】 向网页<Title></Title>中插入图片以及跑马灯
- Scala学习整理[第三十章 Actor和并发]<Programming In Scala>
- <P>段落标签</p>设置字体和大小,颜色以及与<font>文本标签</font>
- 转载只为记录经典之<轻量级进程和普通进程以及线程之间的区别>
- 编辑xml文件的时候特殊符号的处理 < >
- #include<iostream>与#include<iostream.h>以及#inclue<string> 和 #include<string.h>的区别
- (Boolan)C++设计模式 <一> ——设计模式简介以及面向对象设计原则
- include指令和include动作以及<c:import>总结
- 无符号位移(>>>、<<<)和有符号位移(>>、<<)
- findViewById,ViewHolder,SparseArray<View> viewHolder原理以及效率对比
- .NET/C#中对对象集合进行查询的方法 以及相关的 Predicate<T> 及 Action<T> 的用法
- <a>标签中写有文字,如何隐藏文字以及360模式渲染
- Scala学习整理[第十一-十三章 类型/特质和其应用/包]<Programming In Scala>
- Scala学习整理[第十五章 样本类和模式匹配]<Programming In Scala>
- Java知识点<9> JVM以及内存回收机制
- iOS 第14课 TableView <一> 创建以及数据源方法
- scala返回List<Object>类型给java
- `<img>`放到`<div>`中底部出现空隙 以及解放方案.