到目前为止的搜索结果让我相信,如果没有非主要构造函数,这是不可能的
class Foo { // NOT OK: 2 extra lines--doesn't leverage Scala's conciseness
private var _x = 0
def this(x: Int) { this(); _x = x }
def x = _x
}
val f = new Foo(x = 123) // OK: named parameter is 'x'
Run Code Online (Sandbox Code Playgroud)
或牺牲主构造函数中的参数名称(使用命名参数进行调用丑陋)
class Foo(private var _x: Int) { // OK: concise
def x = _x
}
val f = new Foo(_x = 123) // NOT OK: named parameter should be 'x' not '_x'
Run Code Online (Sandbox Code Playgroud)
理想情况下,人们可以这样做:
class Foo(private …Run Code Online (Sandbox Code Playgroud) 我想要的课程只能混合指定的特征:
class Peter extends Human with Lawful with Evil
class Mag extends Elf with Chaotic with Neutral
Run Code Online (Sandbox Code Playgroud)
在Scala中是这样做的吗?
UPD:
trait Law
trait Lawful extends Law
trait LNeutral extends Law
trait Chaotic extends Law
trait Moral
trait Good extends Moral
trait Neutral extends Moral
trait Evil extends Moral
class Hero .........
class Homer extends Hero with Chaotic with Good
Run Code Online (Sandbox Code Playgroud)
我想定义一个Hero在限制客户端程序员混合特定性状的方式类(Lawful/ LNeutral/ Chaotic和Good/ Neutral/ Evil),如果他扩展了Hero类.我想找到一些限制/约束客户端代码的其他可能性.
我已经学会了+我自己学习编程.我经常看到这些话.如果有人在编程环境中解释它们,我将不胜感激:
aspnet_isapi.dll我有我的java枚举,例如:FOO("foo"),BAR("bar")...我有一个getValue()方法来返回值"foo"和"bar"枚举,这必须是Java.
另一方面,我必须在Scala中匹配:
result match {
case "foo" =>
Run Code Online (Sandbox Code Playgroud)
我想做:
result match {
case Enum.FOO.getValue() =>
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
method getValue is not a case class constructor, nor does it have an
unapply/unapplySeq method
Run Code Online (Sandbox Code Playgroud)
我不太清楚这里发生了什么,因为我的getValue()方法返回了一个String为什么我不能用它来进行模式匹配?谢谢
事实证明,在GHC 7.10中,这编译很好:
mysum xs = foldr (+) 0 xs
Run Code Online (Sandbox Code Playgroud)
但是这个:
mysum = foldr (+) 0
Run Code Online (Sandbox Code Playgroud)
导致以下错误:
No instance for (Foldable t0) arising from a use of ‘foldr’
The type variable ‘t0’ is ambiguous
Relevant bindings include
mysum :: t0 Integer -> Integer (bound at src/Main.hs:37:1)
Note: there are several potential instances:
instance Foldable (Either a) -- Defined in ‘Data.Foldable’
instance Foldable Data.Functor.Identity.Identity
-- Defined in ‘Data.Functor.Identity’
instance Foldable Data.Proxy.Proxy -- Defined in ‘Data.Foldable’
...plus five others
In the expression: foldr …Run Code Online (Sandbox Code Playgroud) 由于钠已被作者弃用,我正试图将我的代码移植到reactive-banana.然而,两者之间似乎存在一些不协调,以至于我很难过度使用.
例如,在钠中很容易检索行为的当前值:
retrieve :: Behaviour a -> IO a
retrieve b = sync $ sample b
Run Code Online (Sandbox Code Playgroud)
我不知道如何在反应性香蕉中这样做
(我想要这个的原因是因为我试图将行为导出为dbus属性.可以从其他dbus客户端查询属性)
编辑:替换"民意调查"一词,因为它具有误导性
我试图virtualenv[wrapper]通过Cygwin在我的Windows机器上工作.安装成功,并easy_install基于以下方向:http://www.doughellmann.com/docs/virtualenvwrapper/.
当我使用时出现问题mkvirtualenv [name_of_vir_env].我得到以下输出:
$ mkvirtualenv testenv
New python executable in testenv\Scripts\python.exe
Installing setuptools.................done.
bash: D:\.virtualenvs/testenv/bin/postactivate: No such file or directory
chmod: getting attributes of `D:\\.virtualenvs/testenv/bin/postactivate': No such file or directory
bash: D:\.virtualenvs/testenv/bin/predeactivate: No such file or directory
chmod: getting attributes of `D:\\.virtualenvs/testenv/bin/predeactivate': No such file or directory
bash: D:\.virtualenvs/testenv/bin/postdeactivate: No such file or directory
chmod: getting attributes of `D:\\.virtualenvs/testenv/bin/postdeactivate': No such file or directory
ERROR: Environment 'D:\.virtualenvs/testenv' does not contain …Run Code Online (Sandbox Code Playgroud) 可以通过以下方式启动文本文件来在Scala中编写shell脚本:
#!/bin/sh
exec scala "$0" "$@"
!#
Run Code Online (Sandbox Code Playgroud)
为了简化脚本创建,我想编写一个名为scalash(可能是BASH脚本)的可执行文件,允许将Scala脚本标题缩短为一行:
#!/bin/scalash
Run Code Online (Sandbox Code Playgroud)
可能吗 ?如果我可以传递可选参数scalash,例如添加类路径依赖项,则需要额外的点数.
让我思考的是为什么以下
<something> match { ... }
Run Code Online (Sandbox Code Playgroud)
无法改写为
<something>.match({ ... }) # error: identifier expected but 'match' found.
Run Code Online (Sandbox Code Playgroud)
我想这是因为它不可能match作为常规方法实现,但我不确定.或许这是出于性能原因.
此外,现在宏可用,match可以使用宏来实现吗?(不是说它应该完成,而只是假设)
编辑:这似乎与scala的实验虚拟模式匹配器有什么关系?; 感谢@ om-nom-nom指出.
我想回顾一下同事的补丁.我们无法使用审核工具.所以我想评论他制作的补丁文件.是否可以在(svn)补丁文件中写入内联注释?
我在svn红皮书中找不到任何信息.我甚至无法找到补丁文件语法来弄清楚自己.