我在包中有一个抽象类foo(在这个特殊情况下,一个特性),可以由各种子类实现,我想创建一个正交子类,用于一个更具体的包bar,添加特定于包的信息.似乎最好的方法是通过组合(包装)而不是继承,因为否则我必须声明每个foo-package子类的特定于包的版本.但这会导致需要转发的受保护成员出现问题:
package foo {
trait Foo {
protected def bar: Int
}
}
package bar {
import foo.Foo
class Baz
class WrapFoo(wrapped: Foo) extends Baz with Foo {
protected def bar = wrapped.bar
}
}
Run Code Online (Sandbox Code Playgroud)
这会导致错误:
~/test/scala 14:54 152272% scalac testprotected.scala
testprotected.scala:11: error: method bar in trait Foo cannot be accessed in foo.Foo
Access to protected method bar not permitted because
prefix type foo.Foo does not conform to
class WrapFoo in package bar where …Run Code Online (Sandbox Code Playgroud) 我刚刚开始使用Scala.我一直在使用Python进行研究编程,而且我正在转换一个相当大的(~4000行)Python程序.
一些评论:
我的问题是:
是否计划使用默认参数为函数参数添加类型推断?写这样的东西有点烦人:
def add_words(words:Traversable[String], ignoreCase:Boolean=true,
stopwords:Set[String]=Set[String]()) {
...
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,根本不需要对ignoreCase和stopwords进行类型注释,它们只是添加了不必要的详细程度.
感谢参与Scala开发的人员提出的任何意见.
JavaTokenParsersScala 中提供了方便的正则表达式来匹配整数和浮点数以及双引号字符串。但这就是它的全部作用。我如何做将这些字符串转换回底层转换对象的明显事情?toDouble对于数字来说,使用or等很容易做到这一点toInt。但是如何对字符串进行等效操作呢?例如,如果我输入字符串
"Unicode \u20ac is a Euro sign, which I would write \\u20ac in a string. \243 is a pound sign.\n\r And \f is a \"form feed\", with embedded quotes.\n\r"
Run Code Online (Sandbox Code Playgroud)
然后我运行这个JavaTokenParsers,我会及时得到一个字符串,该字符串正确解析嵌入的引号,但有一个双引号字符作为其第一个和最后一个字符,以及许多反斜杠序列。如何获得处理了转义序列的等效 Java 字符串?我不敢相信没有库函数可以做到这一点,但找不到。
请注意以下奇怪的行为(Scala 2.9.1.RC2):
scala> val spam = x => log(x)
spam: Double => Double = <function1>
scala> val spam = x => log(x)*log(x)
<console>:10: error: missing parameter type
val spam = x => log(x)*log(x)
^
scala> log(2)*log(2)
res30: Double = 0.4804530139182014
Run Code Online (Sandbox Code Playgroud)
为什么Scala可以推断出第一个的类型而不是第二个?
另一种陌生感:
scala> def eggs(foo:Int=-1) = foo
<console>:1: error: identifier expected but integer literal found.
def eggs(foo:Int=-1) = foo
^
scala> def eggs(foo:Int= -1) = foo
eggs: (foo: Int)Int
Run Code Online (Sandbox Code Playgroud)
这里发生了什么?当=和 - 之间没有空格时,为什么会窒息?
我正在使用ggplot_dual_axis()Stack Overflow 答案中的函数:如何将转换后的比例放在 ggplot2 的右侧?为了显示两个 Y 轴,一个在右侧,一个在左侧。该函数是一团乱七八糟的东西,基本上将两个图重叠在一起,一个 Y 轴在左侧,另一个 Y 轴在右侧。然而,它似乎并没有从正确的情节中获取所有元素,特别是图例。这很重要的原因是它首先显示左图,导致网格线被写在图例上。我对代码的理解不足以ggplot_dual_axis()修复它。有懂的人可以帮助我吗?
这是我的代码:
library(ggplot2)
library(reshape2)
library(scales) # for format_format
# See /sf/ask/1329230101/
ggplot_dual_axis <- function(lhs, rhs, axis.title.y.rhs = "rotate") {
# 1. Fix the right y-axis label justification
rhs <- rhs + theme(axis.text.y = element_text(hjust = 0))
# 2. Rotate the right y-axis label by 270 degrees by default
if (missing(axis.title.y.rhs) |
axis.title.y.rhs %in% c("rotate", "rotated")) {
rhs <- rhs + theme(axis.title.y = element_text(angle = …Run Code Online (Sandbox Code Playgroud) scala ×4
parsing ×2
axis-labels ×1
composition ×1
decode ×1
escaping ×1
ggplot2 ×1
idiomatic ×1
inheritance ×1
legend ×1
parameters ×1
plot ×1
protected ×1
r ×1
type-erasure ×1