English 中文(简体)
Scala的超修补功能
原标题:Overriding curried functions in Scala
  • 时间:2012-04-30 21:16:37
  •  标签:
  • scala

我的印象是:

// short syntax
def foo(bar: Bar)(baz: Baz): Quux

食糖

// long syntax
def foo(bar: Bar): (Baz) => Quux

但是,在继承问题上,我似乎不能把两者混在一起。 整个树木必须在短yn或长yn中界定;从来都不是。

例如:

case class Context
case class Work

trait ContextualWorker {
  def workWithContext(ctxt: Context)(work: Work): Traversable[Work]
}

class ShortConcreteWorker extends ContextualWorker {
  override def workWithContext(ctxt: Context)(work: Work) = Nil
}

class LongConcreteWorker extends ContextualWorker {
  // error on next line: method workWithContext overrides nothing    <-------------
 override def workWithContext(ctxt: Context): (Work) => Traversable[Work] = {
    val setupCode = 1
    { work => Nil }
  }
}

如果我改变海峡,使用长子,那么短期合同工人就没有汇编成册。

www.un.org/spanish/ecosoc 为什么这些是不可交换/不可继承的? 你们如何绕过它?

现在最灵活的做法似乎是界定长子的树木,或许可以归入短期合同规定的执行类别。 劳动者:

case class Context
case class Work

trait ContextualWorker {
  def workWithContext(ctxt: Context): (Work) => Traversable[Work]
}

class ShortConcreteWorker extends ContextualWorker {
  override def workWithContext(ctxt: Context) = workWithContextImpl(ctxt)_ 
  private def workWithContextImpl(ctxt: Context)(work: Work) = Nil
}

class LongConcreteWorker extends ContextualWorker {
  override def workWithContext(ctxt: Context): (Work) => Traversable[Work] = {
    val setupCode = 1
    { work => Nil }
  }
}
最佳回答

所述两种方法完全不同。 REPL确认:

scala> def foo1(a: Int)(b: Int): Int = a + b
foo1: (a: Int)(b: Int)Int

scala> def foo2(a: Int): (Int => Int) = (b: Int) => a + b
foo2: (a: Int)Int => Int

第一项职能需要两个论点,分别以单独的理由清单提出,并填写<代码>。 Int 。 第二项职能是one论据和从Int<>>>> 。 虽然这两个问题在概念上是相似的,但事实上是不同的构造,而Scala则将这两个问题作为对待。

这并不限于具有多重论点清单的职能。 这里的工作方式相同:

scala> def foo3(a: Int): Int = a + 1
foo3: (a: Int)Int

scala> def foo4: (Int => Int) = (a: Int) => a + 1
foo4: Int => Int

Note that there are different ramifications for usage as well. With foo2, because it only accepts one argument, we can call it with just one argument. However, foo1 requires two arguments, an so we cannot simply call it with one. You can however use the _ syntax to convert it into a callable function.

foo2(2)    // Int => Int = <function1>
foo1(2)    // error: missing arguments for method foo1
foo1(2) _  // Int => Int = <function1>

回答你的问题: 不能互换的原因是这些原因不一样。 如果是这样的话,我们将能够以同样的方式来看待他们。 如果你在延长时能够改变签名,那么Schala会知道什么叫 s子允许? “周围植被”的方法只是使签名保持一致。

问题回答

暂无回答




相关问题
How to flatten a List of different types in Scala?

I have 4 elements:List[List[Object]] (Objects are different in each element) that I want to zip so that I can have a List[List[obj1],List[obj2],List[obj3],List[obj4]] I tried to zip them and I ...

To use or not to use Scala for new Java projects? [closed]

I m impressed with Twitter and investigating to use Scala for a new large scale web project with Hibernate and Wicket. What do you think about Scala, and should I use it instead of Java? EDIT: And, ...

Why does Scala create a ~/tmp directory when I run a script?

When I execute a Scala script from the command line, a directory named "tmp" is created in my home directory. It is always empty, so I simply deleted it without any apparent problem. Of course, when I ...

Include jar file in Scala interpreter

Is it possible to include a jar file run running the Scala interpreter? My code is working when I compile from scalac: scalac script.scala -classpath *.jar But I would like to be able to include a ...

Scala and tail recursion

There are various answers on Stack Overflow which explain the conditions under which tail recursion is possible in Scala. I understand the limitations and how and where I can take advantage of tail ...

热门标签