Scala中的案例类是使用模式匹配增强的标准类,等于......(或者我错了?).此外,他们的实例不需要"新"关键字.在我看来,它们比常规类更容易定义(或者我又错了吗?).
有很多网页告诉他们应该在哪里使用(主要是关于模式匹配).但他们应该避免在哪里?为什么我们不到处使用它们?
下面的代码试图模仿DSL的多态嵌入:它不是给出行为Inner,而是在useInner其封闭类的方法中编码.我添加了enclosing方法,以便用户只需保留对Inner实例的引用,但始终可以获取其封闭的实例.通过这样做,Inner来自特定Outer实例的所有实例仅绑定到一个行为(但这里需要它).
abstract class Outer {
sealed class Inner {
def enclosing = Outer.this
}
def useInner(x:Inner) : Boolean
}
def toBoolean(x:Outer#Inner) : Boolean = x.enclosing.useInner(x)
Run Code Online (Sandbox Code Playgroud)
它没有编译和scala 2.8抱怨:
type mismatch; found: sandbox.Outer#Inner
required: _81.Inner where val _81:sandbox.Outer
Run Code Online (Sandbox Code Playgroud)
从Scala编程:嵌套类和Scala:Inner Classes之旅,在我看来,问题是useInner期望作为参数Inner来自特定Outer实例的实例.
什么是真正的解释以及如何解决这个问题?
我试图检测是否存在隐式转换,并依赖于它来执行一些代码.例如 :
if (x can-be-converted-to SomeType)
return something(conversion(x))
else
return someotherthing(x)
Run Code Online (Sandbox Code Playgroud)
例如,x是一个Int,应该转换为RichInt.这可能在Scala中吗?如果有,怎么样?
谢谢
我试图使用Redis的-卢阿内库COPAS.它需要一些修补.一个问题是redis-lua将一些迭代器定义为协同程序,但这些迭代器执行可以执行的网络操作yield.
因此,coroutine.yield用于两个非常不同的东西:迭代器和copas.由于网络调用嵌套在迭代器中,coroutine.wrap因此迭代器拦截网络产量,而不是被copas拦截.
以下示例显示了问题:
local function iterator ()
for i = 1, 2 do
if i == 2 then coroutine.yield () end -- network yield
coroutine.yield () -- iterator yield
end
end
local citerator = coroutine.wrap (iterator)
local function loop () -- use of the iterator within a copas thread
while citerator () do end
end
local cloop = coroutine.create (loop)
while coroutine.resume (cloop) do …Run Code Online (Sandbox Code Playgroud) 我目前正在为我们的项目更新 maven 原型,并希望减少用户在使用此原型时必须提供的信息量。例如,groupId 将始终相同。
那么,是否可以在 Maven 原型中为 groupId 定义默认值?