我刚刚在Scala actors包中看到过这个case类:
case class ! [a](ch: Channel[a], msg: a)
Run Code Online (Sandbox Code Playgroud)
在JavaDoc中,它以下列形式描述了用法:
receive {
case Chan1 ! msg1 => ...
case Chan2 ! msg2 => ...
}
Run Code Online (Sandbox Code Playgroud)
为什么不是这样的:
receive {
case !(Chan1, msg1) => ...
case !(Chan2, msg2) => ...
}
Run Code Online (Sandbox Code Playgroud)
是砰的操作员!与以冒号结尾的方法类似的特殊情况:
我有一个Java类,它记录的东西有这样的方法:
void info(Object message, Object... params);
Run Code Online (Sandbox Code Playgroud)
在Scala中,我创建了一个围绕这样调用的包装器,如下所示:
def info(msg: => String, params: Any*) {
log.info(msg, params);
}
Run Code Online (Sandbox Code Playgroud)
我打电话的时候:
val host = "127.0.0.1"
val port = "1234"
info("Start on {0}:{1}", host, port)
Run Code Online (Sandbox Code Playgroud)
我明白了:
"Started on WrappedArray(127.0.0.1, 1234):{1}"
Run Code Online (Sandbox Code Playgroud)
现在,有没有人现在如何将params转换为可以正确使用的Object []?
我试着这样做:
def info(msg: => String, params: Any*)
log.info(msg, params.toList.toArray);
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用:
"Started on [Ljava.lang.Object;@14a18d:{1}"
Run Code Online (Sandbox Code Playgroud)
你做的时候会发生类似的事情:
params.asInstanceOf[WrappedArray[Object]].array
Run Code Online (Sandbox Code Playgroud) 我有一个Iterator[Option[T]]和我想的Iterator[T]那些OptionS其中T isDefined.必须有一个比这更好的方法:
it filter { _ isDefined} map { _ get }
Run Code Online (Sandbox Code Playgroud)
我原本以为在一个结构中它是可能的......有人有什么想法吗?
在这些定义foo的每种形式中发生了什么?:
scala> def foo = {1}
foo: Int
scala> foo
res2: Int = 1
Run Code Online (Sandbox Code Playgroud)
但:
scala> def foo {1}
foo: Unit
scala> foo
scala>
Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个特性(在Scala 2.8中),它可以混合到一个case类中,允许在运行时检查它的字段,以用于特定的调试目的.我想按照它们在源文件中声明的顺序返回它们,我想省略case类中的任何其他字段.例如:
trait CaseClassReflector extends Product {
def getFields: List[(String, Any)] = {
var fieldValueToName: Map[Any, String] = Map()
for (field <- getClass.getDeclaredFields) {
field.setAccessible(true)
fieldValueToName += (field.get(this) -> field.getName)
}
productIterator.toList map { value => fieldValueToName(value) -> value }
}
}
case class Colour(red: Int, green: Int, blue: Int) extends CaseClassReflector {
val other: Int = 42
}
scala> val c = Colour(234, 123, 23)
c: Colour = Colour(234,123,23)
scala> val fields = c.getFields
fields: List[(String, Any)] = List((red,234), …Run Code Online (Sandbox Code Playgroud) trait NotNull {}
Run Code Online (Sandbox Code Playgroud)
我一直试图看看这个特性如何保证某些东西不是空的我无法弄清楚:
def main(args: Array[String]) {
val i = List(1, 2)
foo(i) //(*)
}
def foo(a: Any) = println(a.hashCode)
def foo(@NotNull a: Any) = println(a.hashCode) //compile error: trait NotNull is abstract
def foo(a: Any with NotNull) = println(a.hashCode) //compile error: type mismatch at (*)
Run Code Online (Sandbox Code Playgroud)
和:
val i = new Object with NotNull //compile-error illegal inheritance
Run Code Online (Sandbox Code Playgroud)
显然有一些特殊的编译器处理正在进行,因为它编译:
trait MyTrait {}
def main(args: Array[String]) {
val i: MyTrait = null
println(i)
}
Run Code Online (Sandbox Code Playgroud)
然而,这不是:
def main(args: Array[String]) {
val i: NotNull …Run Code Online (Sandbox Code Playgroud) 我如何State用来模仿行为List.zipWithIndex?到目前为止我提出的(不起作用)是:
def numberSA[A](list : List[A]) : State[Int, List[(A, Int)]] = list match {
case x :: xs => (init[Int] <* modify((_:Int) + 1)) map { s : Int => (x -> s) :: (numberSA(xs) ! s) }
case Nil => state( (i : Int) => i -> nil[(A, Int)] )
}
Run Code Online (Sandbox Code Playgroud)
这基于状态示例非常松散.正如我所说,它不起作用:
scala> res4
res5: List[java.lang.String] = List(one, two, three)
scala> numberSA(res4) ! 1
res6: List[(String, Int)] = List((one,1), (two,1), (three,1))
Run Code Online (Sandbox Code Playgroud)
我可以通过更改case语句的一行来使它工作:
case x …Run Code Online (Sandbox Code Playgroud) 我可以=在scala for-understanding中使用一个(如SLS第6.19节所述)如下:
假设我有一些功能String => Option[Int]:
scala> def intOpt(s: String) = try { Some(s.toInt) } catch { case _ => None }
intOpt: (s: String)Option[Int]
Run Code Online (Sandbox Code Playgroud)
然后我可以这样使用它
scala> for {
| str <- Option("1")
| i <- intOpt(str)
| val j = i + 10 //Note use of = in generator
| }
| yield j
res18: Option[Int] = Some(11)
Run Code Online (Sandbox Code Playgroud)
据我所知,这基本上相当于:
scala> Option("1") flatMap { str => intOpt(str) } map { …Run Code Online (Sandbox Code Playgroud) 我问的问题与这个问题略有不同.假设我有一个代码段:
def foo(i : Int) : List[String] = {
val s = i.toString + "!" //using val
s :: Nil
}
Run Code Online (Sandbox Code Playgroud)
这在功能上等同于以下内容:
def foo(i : Int) : List[String] = {
def s = i.toString + "!" //using def
s :: Nil
}
Run Code Online (Sandbox Code Playgroud)
为什么我会选择一个而不是另一个?显然我会认为第二个在以下方面有一点点缺点:
def被提升到类中的方法)s两次(即不必要的重做计算)我能想到的唯一优势是:
s意味着它只在被使用时被调用(但我可以使用它lazy val)人们的想法在这里是什么?制作所有内心val的东西对我来说是否有重大的不利影响def?
该sum方法的签名TraversableOnce如下:
def sum[B >: A](implicit num: Numeric[B]): B = foldLeft(num.zero)(num.plus)
Run Code Online (Sandbox Code Playgroud)
我可以这样使用它:
scala> (1 to 10).sum
res0: Int = 55
Run Code Online (Sandbox Code Playgroud)
在这种情况下,编译器会注入Numeric[B]自身,因此在范围内必须有这种类型的明确隐式值.如果我Predef.implicitly自己注射它,会发生这种情况:
scala> (1 to 10).sum(implicitly)
<console>:6: error: ambiguous implicit values:
both method conforms in object Predef of type [A]<:<[A,A]
and method stringCanBuildFrom in object Predef of type => scala.collection.generic.CanBuildFrom[String,Char,String]
match expected type T
(1 to 10).sum(implicitly)
^
Run Code Online (Sandbox Code Playgroud)
为什么这个含糊不清?
我可以使模糊性消失
scala> (1 to 10).sum(implicitly[Numeric[Int]])
res2: Int = 55
Run Code Online (Sandbox Code Playgroud)
要么
scala> (1 to 10).sum[Int](implicitly)
res3: …Run Code Online (Sandbox Code Playgroud) scala ×10
scala-2.8 ×3
case-class ×2
either ×1
for-loop ×1
implicit ×1
methods ×1
monads ×1
nullable ×1
reflection ×1
scalaz ×1
state-monad ×1