小编Fin*_*son的帖子

什么()在Scala中意味着什么?

我找到了一个Scala代码片段,它声明了一个方法<init>并放在()调用之下.

我对第5行有疑问.()这里的意思是什么?

(() => {
  final class $anon extends MutableProjection {
    def <init>() = {
      super.<init>();
      ()
    };
    ...
  };
  new $anon()
})
Run Code Online (Sandbox Code Playgroud)

这是一个完整示例的代码.

scala

4
推荐指数
2
解决办法
1670
查看次数

Scala中缀类型的真实例子

我发现了一个有趣的语法东西.它被称为Infix type.

例:

class M[T, U]
new (Int M String)
Run Code Online (Sandbox Code Playgroud)

现在我正在从一些流行的框架或库中寻找这种类型的例子.我在哪里可以找到它们?有什么建议?

scala

4
推荐指数
2
解决办法
578
查看次数

如何在case类中指定多个构造函数?

我正在尝试创建一个包含多个构造函数的case类:

object App {
  def main(args: Array[String]) {
    val a = Something("abc", 100500, _ % 2 == 0)
    val b = Something(true, 10, 20)   
    println(s"$a - $b")
  }
}

case class Something(s: String, n: Int, p: Int => Boolean) {
  /*
  Additional constructor -- Wrong way! -- it is imposible to invoke it outside the class
  def this(b: Boolean, x: Int, y: Int) {
    this("", 0, (i: Int) => i % x + y == 0)
  }
  */
}
Run Code Online (Sandbox Code Playgroud)

到目前为止我的代码不起作用: …

scala

4
推荐指数
1
解决办法
6468
查看次数

在Java中声明类时,顺序是否重要?

为什么这段代码甚至无法编译?

public class T {
    public static void main(String[] args) {
        class A extends B {}
        class B {}
        B a = new A();
    }
}
Run Code Online (Sandbox Code Playgroud)

错误:

Error:(10, 25) java: cannot find symbol
  symbol:   class B
  location: class com.test.T
Error:(12, 15) java: incompatible types
  required: B
  found:    A
Run Code Online (Sandbox Code Playgroud)

在声明这些类时,顺序真的重要吗?

java

3
推荐指数
1
解决办法
321
查看次数

Scala类声明中私有修饰符位置有什么区别?

假设我有两个类:

private[example] class PoorInt    extends Int
class RichInt    private[example] extends Int
Run Code Online (Sandbox Code Playgroud)

问题是这些类声明中私有修饰符位置的区别是什么?

scala

3
推荐指数
1
解决办法
124
查看次数

角:如何使整个表行在ng-repeat中可点击?

我有桌子

<tbody>
    <tr ng-repeat="star in stars">
        <td>
            <a ng-href="/#/stars/{{star.id}}">{{star.id}}</a>
        </td>
        <td>{{star.name}}</td>
    </tr>
</tbody>
Run Code Online (Sandbox Code Playgroud)

它渲染实体。到目前为止,第一列是可单击的。我想使整个表格行(<tr>)可点击。我该怎么做?

javascript angularjs

2
推荐指数
1
解决办法
1万
查看次数

Scala中reduceLeft和reduceRight之间的区别是什么?

Scala中reduceLeft和reduceRight之间的区别是什么?

  val list = List(1, 0, 0, 1, 1, 1)

  val sum1 = list reduceLeft  {_ + _}   
  val sum2 = list reduceRight {_ + _}

  println { sum2 == sum2 }
Run Code Online (Sandbox Code Playgroud)

在我的代码段sum1= sum2= 4,所以这里的顺序无关紧要.

collections scala

1
推荐指数
1
解决办法
2328
查看次数

这是Monad实施吗?

在我看来,我写了一个简单的Scala代码,这是一个Monad实现.

这是一个基本特征:

trait M[A] {

  def unit(x: A): M[A]

  def bind[B](f: A => M[B]): M[B]

}
Run Code Online (Sandbox Code Playgroud)

执行:

case class Monad(e: String) extends M[String] {    

  def unit(x: String): M[String] = {
    Monad(x)
  }

  def bind[B](f: (String) => M[B]): M[B] = {
    f(e)
  }    

}
Run Code Online (Sandbox Code Playgroud)

任何人都可以确认它是否是真正的Monad实现?

monads scala

1
推荐指数
1
解决办法
93
查看次数

纯粹的表达在Scala中意味着什么?

我有两个问题.第一个:是code一个纯粹的表达?

lazy val code: Unit = {

    // block of code

    var s = "abc"
    for (i <- 0 until 10) println(i)
    s += s concat "def"
    println(s)

}
Run Code Online (Sandbox Code Playgroud)

第二个问题:纯粹表达意味着什么?这是一些不返回任何内容的代码吗?

scala

1
推荐指数
1
解决办法
301
查看次数

Scala:类型参数不符合类类型参数边界

我的代码

object TestApp extends App {   
  class A[T <: Ordered[T]] {
    def check(f: T, s: T) = f > s
  }    
  object A {
    def apply[T] = new A[T]
  }   
  A[String].check("ab", "aa")    
}
Run Code Online (Sandbox Code Playgroud)

抛出异常:

Error:(13, 9) type arguments [T] do not conform to class A's type parameter bounds [T <: Ordered[T]]
    def apply[T] = new A[T]
        ^
Run Code Online (Sandbox Code Playgroud)

但我不明白为什么.对我来说,似乎没问题.我有一个带有Ordered的类型变量的类.在伴侣对象中,我正在尝试创建A的实例.但是我不确定我的伴随对象是否可以访问类A的类型变量.

我该如何解决?

scala

1
推荐指数
1
解决办法
2260
查看次数

标签 统计

scala ×8

angularjs ×1

collections ×1

java ×1

javascript ×1

monads ×1