除了继承方面,以下类模板之间是否存在差异:
1| trait TraitA extends TraitB
2| trait TraitA { self: TraitB => }
Run Code Online (Sandbox Code Playgroud)
我想在两者之间分配责任TraitA,TraitB但如果没有后者,前者就无法运作.
你会如何表达这个意图?对我而言,解决方案[2]将是更自然的方法.但是,我不想把实施者的负担混合在一起需要混合的东西.
我可以private final在Scala中使用修饰符吗?
鉴于以下代码:
1| class A { def callFoo = foo; private final def foo = "bar of A" }
2| class B extends A { private final def foo = "bar of B"}
3| println((new A()).callFoo)
4| println((new B()).callFoo)
Run Code Online (Sandbox Code Playgroud)
3号线和4号线打印:
1| bar of A
2| bar of A
Run Code Online (Sandbox Code Playgroud)
可以理解为什么第2行不打印,bar of B因为实际上有两个foo定义而B中的后者不会覆盖A中的前者.否则Scala将需要override- 而不是final修饰符.
那么Scala为什么不简单地禁止修饰符的组合private final呢?
为什么以下三个字符没有对称的toLower,toUpper结果
/**
* Written in the Scala programming language, typed into the Scala REPL.
* Results commented accordingly.
*/
/* Unicode Character 'LATIN CAPITAL LETTER SHARP S' (U+1E9E) */
'\u1e9e'.toHexString == "1e9e" // true
'\u1e9e'.toLower.toHexString == "df" // "df" == "df"
'\u1e9e'.toHexString == '\u1e9e'.toLower.toUpper.toHexString // "1e9e" != "df"
/* Unicode Character 'KELVIN SIGN' (U+212A) */
'\u212a'.toHexString == "212a" // "212a" == "212a"
'\u212a'.toLower.toHexString == "6b" // "6b" == "6b"
'\u212a'.toHexString == '\u212a'.toLower.toUpper.toHexString // "212a" != "4b" …Run Code Online (Sandbox Code Playgroud) 给定类型别名type Cal = java.util.Calendar如何static getInstance访问该方法?我在Scala REPL中尝试了以下内容:
scala> type Cal = java.util.Calendar
defined type alias Cal
scala> Cal.getInstance
<console>:8: error: not found: value Cal
Cal.getInstance
^
scala> val Cal = java.util.Calendar
<console>:7: error: object Calendar is not a value
val Cal = java.util.Calendar
^
Run Code Online (Sandbox Code Playgroud)
是import java.util.{Calendar => Cal}其次import Cal._真的是我最好的选择?
处理目录堆栈的方式是否比使用pushd和更可行popd?
我找到的的的CLI pushd,并popd呈现给用户一种令人难以置信的内置命令.但与此同时,我希望经常使用dirs内置命令提供的目录堆栈.
所以我花了很长时间才弄清楚用例然后记住以下内容:
pushd - 从顶部切换两个目录.pushd +1 - 从顶级目录切换到第二个.pushd -0 - 从顶层目录切换到底部.popd +0 - 从顶部目录弹出顶部更改为第二个.pushd -0; popd +1 - 从顶层目录切换到底部并弹出前顶部.每次只是为了与目录堆栈玩杂耍,仍然需要输入它们,这对我很重要.我甚至不能用助记名来为这五个用例添加别名.
任何见解?
给出以下代码:
class A {
class B
type C <: B
trait D
}
class E extends A {
type C = B
}
class F extends E {
override type C = B with D
}
Run Code Online (Sandbox Code Playgroud)
为什么Eclipse Indigo IDE中的Scala IDE的表示编译器会在E类中覆盖覆盖类型C的错误消息,它等于F.this.B; C型有不兼容的类型?
在所有类"B"仅用特征"D""修改"之后,因此两种类型定义具有相同的基本类型,即"B".因此兼容的类型定义.
以下代码有效.我认为类型赋值的规则与变量赋值类似,例如:
class Foo
trait Bar
val a: Foo = new Foo
val fooWithBar: Foo = new Foo with Bar
Run Code Online (Sandbox Code Playgroud)
我的理解错了吗?
鉴于register下面的通用方法,我想将:=运算符定义为符号别名.
def register[Prop <: Property[_]](prop: Prop): Prop
@inline
final def :=[Prop <: Property[_]] = register[Prop] _
Run Code Online (Sandbox Code Playgroud)
本来我想写这样的东西:
val := = register _
Run Code Online (Sandbox Code Playgroud)
但这给了我功能签名Nothing => Nothing.我的下一次尝试是使用类型对其进行参数化,Prop但这显然只有在我将其设为a时才有效def,它可以采用类型参数并将其传递给它们.
理想情况下,我想省略@inline注释,但我不确定Scala编译器使用什么目标代码.
最重要的是我的目标是不让:=方法复制register方法签名的所有部分,除了名称,然后简单地让前者委托给后者.
在Scala REPL中输入以下小顺序程序及其并行化版本:
/* Activate time measurement in "App" class. Prints [total <X> ms] on exit. */
util.Properties.setProp("scala.time", "true")
/* Define sequential program version. */
object X extends App { for (x <- (1 to 10)) {Thread.sleep(1000);println(x)}}
/* Define parallel program version. Note '.par' selector on Range here. */
object Y extends App { for (y <- (1 to 10).par) {Thread.sleep(1000);println(y)}}
Run Code Online (Sandbox Code Playgroud)
执行X with X.main(Array.empty)给出:
1
2
3
4
5
6
7
8
9
10
[total 10002ms]
Run Code Online (Sandbox Code Playgroud)
而Y与Y.main(Array.empty)给出:
1 …Run Code Online (Sandbox Code Playgroud) 给出的方法下面的代码foo要比运营商明智的给定参数bar与lowerBound和upperBound相同的抽象类型全部为Bar.
trait Foo {
type Bar <: Ordered[Bar]
val lowerBound: Bar
val upperBound: Bar
def foo(bar: Bar) = bar >= lowerBound && bar <= upperBound
}
Run Code Online (Sandbox Code Playgroud)
这样Foo就可以定义特征.问题从下面的具体类开始FooImpl.
class FooImpl extends Foo {
type Bar = Int
val lowerBound = 0
val upperBound = 5
}
Run Code Online (Sandbox Code Playgroud)
据我所知,scala.Int这scala.runtime.RichInt并没有有效地实现scala.math.Ordered[Int].将类型定义Bar为RichInt不起作用,因为它不符合scala.math.Ordered[RichInt].我第三次尝试将类型定义为声明Bar为Ordered[Ord]where 并将其定义为也不起作用.Ord …
我一直在为如何在 Bash 中声明变量或函数的决定而斗争。
鉴于以下假设:
在全局变量的情况下,我应该使用:
foo=bar - 函数的内部和外部?declare -g foo=bar - 函数的内部和外部?local -g foo=bar - 在函数内部?在局部变量的情况下,我应该使用:
local foo=bardeclare foo=bar在只读变量的情况下,我应该使用:
declare -r foo=barlocal -r foo=barreadonly foo- 在 [1.] 或 [2.] 之后没有-r下一行的标志。在函数的情况下,我应该使用:
foo() { echo bar; }foo { echo bar; }function foo() { echo bar; }function foo { echo bar; }