自从开始看scala的Parser相关的源码以来,我越来越觉得scala中很多处理方法就像是用黑魔法在与编译器打交道。不变成JVM上的c++誓不罢休?
看Programming in Scala 源码 33.6
abstract class Parser[+T] ... { p =>
...
def ~ [U](q: => Parser[U]) = new Parser[T~U] {
def apply(in: Input) = p(in) match {
case Success(x, in1) =>
q(in1) match {
case Success(y, in2) => Success(new ~(x, y), in2)
case failure => failure
}
case failure => failure
}
}
结果查2.9.1里的代码
def ~ [U](q: => Parser[U]): Parser[~[T, U]] = { lazy val p = q // lazy argument
(for(a <- this; b <- p) yield new ~(a,b)).named("~")
}
named方法的定义
def named(n: String): this.type = {name=n; this}
对这个地方一直不明白,查到了这篇文章
后才了解到它的作用。
class A {def method1: A = this }
class B extends A (def method2: B = this}
val b = new B
如果调用b.method2.method1是可以的,但是如果想调用b.method1.method2就不行了。因为method1返回的是A类型的。
当然你可以在B中覆盖method1,以返回正确类型。但是scala中解决这个问题的办法就是this.type
class A { def method1: this.type = this }
class B extends A { def method2: this.type = this }
val b = new B
如果调用b.method1则编译器会知道method1返回的是B类型的。
自言自语:
scala学习曲线过高,代码可读性差(也许我读得还不够多),大量的语法规则和复杂的类型申明让我头晕。是不是该继续尝试更为纯粹的clojure呢?
分享到:
相关推荐
Author Jason Swartz demonstrates why Scala’s concise and expressive syntax make it an ideal language for Ruby or Python developers who want to improve their craft, while its type safety and ...
接下来,我们将具体介绍几种在Scala中常见的设计模式,并探讨它们是如何在Scala中实现的。 ### 三、Singleton(单例模式) 单例模式是一种常用的模式,用于确保一个类只有一个实例,并提供一个全局访问点。在Java...
- **Varargs**: This feature allows a function to accept a variable number of arguments of the same type. For example: ```scala def sum(xs: Int*) = { var r = 0 for (x ) r += x r } ``` - **...
`defretain(p:(A,B)=>Boolean):Map.this.type` 保留满足条件的元素。 - **示例**:`val m = Map("a" -> 1, "b" -> 2, "c" -> 3); m.retain{ case (k, v) => v > 1 }` 结果为 `Map("b" -> 2, "c" -> 3)`。 #### 34....
Learn Scala's sophisticated type system that combines Functional Programming and object-oriented concepts Work on a wide array of applications, from simple batch jobs to stream processing and machine ...
Scala for Data Science, the first module in this course, is a tutorial guide that provides tutorials on some of the most common Scala libraries for data science, allowing you to quickly get up to ...
- **方法与构造器**:Scala中的方法定义使用`= _`语法,构造器可以通过`def this(...)`来定义。 - **访问修饰符**:`private`, `protected`, `public`控制成员的可见性。 3. **函数式编程** - **函数是一等公民*...
generic-supporting type system. For these and any other developers who want to learn how to develop in the Scala programming language, this book provides an organized and examples-based guide that ...
specific languages * Understanding the Scala type system * Applying advanced 'power tools' such as annotations, implicits, and delimited continuations Scala is rapidly reaching a tipping point that ...
// You can use helpers to send any type of data with an available Writable[T] case GET -> Root / "synchronous" => Ok("This is good to go right now.") } 标签:...
: B , B ]() // this checks type equality type S = x - > : y - > : z - > : ( x @@ z @@ (y @@ z) ) type K = x - > : y - > : x type result = ( S @@ K @@ K @@ a ) # - > * Equals [result, a] 在可以找到更...
The videos show construction of code from the ground up and this type of "live coding" is invaluable for learning to program, as it allows students into the mind of a more experienced programmer, ...
This is a toy Lisp interpreter, written in Scala.Type :q to leave lisc> ( defn fact [x] ( if ( <= x 1 ) 1 ( * x ( fact ( - x 1 )))))==> ()lisc> ( fact 5 )==> 120 输入:q退出 REPL。特殊表格目前有四个...
Solve real-life programming ... By using both the statically typed, type-inferred Scala and the dynamically typed, modern Lisp Clojure, you'll gain a broad understanding of functional programming.
val dataFile = this.getClass.getClassLoader.getResource("jts-example.csv").getPath val df = spark.read .schema(schema) .option("sep", "-") .option("timestampFormat", "yyyy/MM/dd HH:mm:ss ZZ") ....
In particular, this book covers Scala’s implicit and type systems in detail before discussing how these can be used to drastically simplify development. The book promotes the “blended style” of ...
Templateval template : Template = Template ( " This is a {{subject.name}} {{subject.type}} " )// template: Any => String = tinyplate.Template$$$Lambda$1317/0x000000080184b840@40c28b0dval result = ...
Scala quickly, and explains what makes this language ideal for today’s scalable, distributed, component-based applications that support concurrency and distribution. You’ll also learn how Scala ...