说我有以下两个case class
es:
case class Address(street: String, city: String, state: String, zipCode: Int)
case class Person(firstName: String, lastName: String, address: Address)
Run Code Online (Sandbox Code Playgroud)
和以下Person
类的实例:
val raj = Person("Raj", "Shekhar", Address("M Gandhi Marg",
"Mumbai",
"Maharashtra",
411342))
Run Code Online (Sandbox Code Playgroud)
现在,如果我想更新zipCode
,raj
那么我将不得不做:
val updatedRaj = raj.copy(address = raj.address.copy(zipCode = raj.address.zipCode + 1))
Run Code Online (Sandbox Code Playgroud)
随着嵌套水平的提高,这将变得更加丑陋.是否有更清洁的方式(像Clojure的东西update-in
)来更新这样的嵌套结构?
我正在使用9.0.3这是最新版本的想法.当我在Idea中编写一些scala源代码时,编译和运行需要几秒钟.这不应该那么慢,这是正常的吗?
Scala具有支持模式匹配中的析取的语言功能('Pattern Alternatives'):
x match {
case _: String | _: Int =>
case _ =>
}
Run Code Online (Sandbox Code Playgroud)
但是,如果仔细检查满足PatternA 和 PatternB(连接),我经常需要触发一个动作.
我创建了一个模式组合器'&&',它增加了这个功能.三条小线条让我想起为什么我爱斯卡拉!
// Splitter to apply two pattern matches on the same scrutiny.
object && {
def unapply[A](a: A) = Some((a, a))
}
// Extractor object matching first character.
object StartsWith {
def unapply(s: String) = s.headOption
}
// Extractor object matching last character.
object EndsWith {
def unapply(s: String) = s.reverse.headOption
}
// Extractor object matching length.
object Length {
def …
Run Code Online (Sandbox Code Playgroud) @uncheckedVariance
可以用来弥合Scala的声明站点方差注释和Java的不变泛型之间的差距.
scala> import java.util.Comparator
import java.util.Comparator
scala> trait Foo[T] extends Comparator[T]
defined trait Foo
scala> trait Foo[-T] extends Comparator[T]
<console>:5: error: contravariant type T occurs in invariant position in type [-T]java.lang.Object with java.util.Comparator[T] of trait Foo
trait Foo[-T] extends Comparator[T]
^
scala> import annotation.unchecked._
import annotation.unchecked._
scala> trait Foo[-T] extends Comparator[T @uncheckedVariance]
defined trait Foo
Run Code Online (Sandbox Code Playgroud)
这表示java.util.Comparator自然是反变体,即类型参数T
出现在参数中,而不是返回类型.
这就引出了一个问题:为什么它还在Scala集合库中使用,它不是从Java接口扩展的?
trait GenericTraversableTemplate[+A, +CC[X] <: Traversable[X]] extends HasNewBuilder[A, CC[A] @uncheckedVariance]
Run Code Online (Sandbox Code Playgroud)
此注释的有效用途是什么?
当你创建一个类的情况下,编译器创建了几个案例类东西的相应配套对象:一个apply
工厂方法相匹配的主构造,equals
,hashCode
,和copy
.
奇怪的是,这个生成的对象扩展了FunctionN.
scala> case class A(a: Int)
defined class A
scala> A: (Int => A)
res0: (Int) => A = <function1>
Run Code Online (Sandbox Code Playgroud)
只有在以下情况下才会这样:
有没有人使用它,或者知道它为什么被添加?它使用静态转发器方法稍微增加了生成的字节码的大小,并显示在#toString()
伴随对象的方法中:
scala> case class A()
defined class A
scala> A.toString
res12: java.lang.String = <function0>
Run Code Online (Sandbox Code Playgroud)
UPDATE
使用单个apply
方法手动创建的对象不会自动视为FunctionN
:
object HasApply {
def apply(a: Int) = 1
}
val i = HasApply(1)
// fails
// HasApply: (Int => …
Run Code Online (Sandbox Code Playgroud) 这是我可以在java中做的事情,获取重复参数的结果并将其传递给另一个方法:
public void foo(String ... args){bar(args);}
public void bar(String ... args){System.out.println("count="+args.length);}
Run Code Online (Sandbox Code Playgroud)
在scala中它看起来像这样:
def foo(args:String*) = bar(args)
def bar(args:String*) = println("count="+args.length)
Run Code Online (Sandbox Code Playgroud)
但这不会编译,bar签名需要一系列单独的字符串,而传入的args是一些非字符串结构.
现在我只是绕过阵列.使用星号参数会非常好.有办法吗?
这有效:
class ButtonCountObserver {
private var cnt = 0 // private field
def count = cnt // reader method
def count_=(newCount: Int) = cnt = newCount // writer method
// ...
}
val b = new ButtonCountObserver
b.count = 0
Run Code Online (Sandbox Code Playgroud)
但事实并非如此
class ButtonCountObserver {
private var cnt = 0 // private field
def count_=(newCount: Int) = cnt = newCount // writer method
// ...
}
val b = new ButtonCountObserver
b.count = 0
Run Code Online (Sandbox Code Playgroud)
我明白了: error: value count is not a …
在Scala 2.7中,我可以写:
package com.acme.bar
class Bar
Run Code Online (Sandbox Code Playgroud)
.
package com.acme.foo
class Foo {
new bar.Bar
}
Run Code Online (Sandbox Code Playgroud)
这不能在Scala 2.8中编译 - 但是这样做:
package com.acme
package bar
class Bar
Run Code Online (Sandbox Code Playgroud)
.
package com.acme
package foo
class Foo {
new bar.Bar
}
Run Code Online (Sandbox Code Playgroud)
我使用scala Map#get
函数,并为每个准确的查询返回Some[String]
有一个简单的方法来删除Some
?
例:
def searchDefs{
print("What Word would you like defined? ")
val selection = readLine
println(selection + ":\n\t" + definitionMap.get(selection))
}
Run Code Online (Sandbox Code Playgroud)
当我使用此方法并使用以下输入时:
What Word would you like defined? Ontology
Run Code Online (Sandbox Code Playgroud)
返回的值是:
Ontology:
Some(A set of representational primitives with which to model a domain of knowledge or discourse.)
Run Code Online (Sandbox Code Playgroud)
我想删除周围的Some().
有小费吗?
因为我打算只重载构造函数以供公共使用来创建类实例,所以我想将主构造函数设为私有.这可能在Scala中吗?
scala ×10
case-class ×2
scala-2.8 ×2
annotations ×1
constructor ×1
map ×1
methods ×1
package ×1
parameters ×1
unchecked ×1
variance ×1
zipper ×1