我试图在Razor中使用三元运算符,类似于这个问题,但我想输出的内容包含空格.这段代码
@(selectedGoal == null ? "" : "value=" + selectedGoal.Name)
Run Code Online (Sandbox Code Playgroud)
应该产生
value="Goal 3"
Run Code Online (Sandbox Code Playgroud)
由于selectedGoal.Name的值为"目标3".相反,我得到了
value="Goal" 3
Run Code Online (Sandbox Code Playgroud)
这不好.我已经尝试了一堆不同的转义引号,@符号和没有@符号的组合,我只是无法让它工作,即
@(selectedGoal == null ? "" : "value=" + "selectedGoal.Name")
@(selectedGoal == null ? "" : "value=@selectedGoal.Name")
Run Code Online (Sandbox Code Playgroud)
然后我就得到类似的东西
value="selectedGoal.Name"
Run Code Online (Sandbox Code Playgroud)
谁知道应该怎么做?
这段代码如何抛出空指针异常?
for (Foo f : Vector<Foo> v)
{
f.doStuff(); // this line throws a NullPointerException
}
Run Code Online (Sandbox Code Playgroud)
即使Vector为空,也不应该只执行内部块?
我正在研究DSL的一些正式的基于语法的东西.我希望能够说出一些代码'start produces "a" andThen 'b andThen "c",其中符号和字符串代表语法的不同组成部分.我看到这样的代码有问题:
class ImplicitTest {
trait GrammarPart
case class Nonterminal(name: Symbol) extends GrammarPart
case class Terminal(value: String) extends GrammarPart
case class Wrapper(production: Seq[GrammarPart]) {
def andThen(next: Wrapper) =
Wrapper(production ++ next.production)
}
implicit def symbolToWrapper(symbol: scala.Symbol) =
Wrapper(Seq(Nonterminal(symbol)))
implicit def stringToWrapper(s: String) =
Wrapper(Seq(Terminal(s)))
}
object StringGrammar extends ImplicitTest {
"x" andThen "y" // this causes a compiler error: "value andThen is not a member of String"
}
object SymbolGrammar extends ImplicitTest { …Run Code Online (Sandbox Code Playgroud) 这是一个简单的类型层次结构.
type Parent() = class end
type Child() = inherit Parent()
Run Code Online (Sandbox Code Playgroud)
我想将类型的函数('x -> Child)视为('x -> Parent):
let f (x: 'x): Child = new Child()
let g: ('x -> Parent) = f // error
Run Code Online (Sandbox Code Playgroud)
但是最后一次分配失败了The type 'Parent' does not match the type 'Child'.有没有办法让这项工作?