小编fer*_*ito的帖子

Nil和List作为Scala中的case表达式

此代码编译:

def wtf(arg: Any) = {  
  arg match {  
    case Nil => "Nil was passed to arg"  
    case List() => "List() was passed to arg"  
    case _ =>"otherwise"  
  }  
}
Run Code Online (Sandbox Code Playgroud)

但是这个没有:

def wtf(arg: Any) = {  
  arg match {  
    case List() => "List() was passed to arg"  
    case Nil => "Nil was passed to arg"  
    case _ =>"otherwise"  
  }  
}  
Run Code Online (Sandbox Code Playgroud)

情况Nil => ...被标记为无法访问的代码.为什么,在第一种情况下,行案例List()=> ...没有标记相同的错误?

scala

11
推荐指数
3
解决办法
7028
查看次数

new Array()vs Object.create(Array.prototype)

天真的困惑:

var arr1 = new Array();
var arr2 = Object.create(Array.prototype);
//Inserting elements in "both arrays"
arr1[0] =0;
arr1[9] =9;
arr2[0] =0;
arr2[9] =9;
arr1.push(10);
arr2.push(10);
console.log(arr1.length); // prints 11
console.log(arr2.length); // prints 1
Run Code Online (Sandbox Code Playgroud)

两个对象都继承了Array.prototype,但它们与[]运算符的行为不同.为什么?

javascript

8
推荐指数
1
解决办法
2569
查看次数

标签 统计

javascript ×1

scala ×1