鉴于此代码:
class Rational(n: Int, d: Int) {
require(d != 0)
private val g = gcd(n.abs, d.abs)
val numerator = n / g
val denominator = d / g
def this(n: Int) = this(n, 1)
override def toString = numerator + "/" + denominator
def +(r: Rational) = new Rational(numerator * r.denominator + r.numerator * denominator, denominator * r.denominator)
def *(r: Rational) = new Rational(numerator * r.numerator, denominator * r.denominator)
def +(i: Int) = new Rational(i) + this
private def gcd(a: …Run Code Online (Sandbox Code Playgroud) 返回收藏品对我们来说哪种类型最好?
我应该使用IList<T>,IEnumerable<T>,IQueryable<T>,别的东西吗?哪个最好,为什么?
我正在尝试决定我应该使用哪种方式,无论是在接口还是我正在编写的几个类的实现中.
编辑让我更进一步,我使用LINQ to SQL通过WCF服务返回数据.感觉这可能会改变最佳使用类型?
我正在学习Scala,我注意到有关使用return语句的一些事情.
因此,显然在Scala中,如果您没有return语句,则默认返回最后一个值.哪个好.但是如果你使用return语句而没有指定返回类型,Scala说"error: method testMethod has return statement; needs result type"
这样可行
def testMethod(arg: Int) = {
arg*2
}
Run Code Online (Sandbox Code Playgroud)
但是这给出了错误
def testMethod(arg: Int) = {
return arg*2
}
Run Code Online (Sandbox Code Playgroud)
这让我刮伤了下巴然后走了
嗯......必须有这样的理由.
为什么在使用return语句时需要显式类型声明,而在让Scala返回最后一个值时不需要?我假设它们是完全相同的,并且返回语句只适用于你想在嵌套函数/条件等中返回一个值(换句话说,"return"语句会自动插入到你的最后一个值中由编译器..如果不存在方法中的任何其他地方)
但显然我错了.当然,实施中必然存在其他一些差异?
我错过了什么吗?
这是我的代码,我实际上不需要任何返回值和类型,并想知道如何处理此错误?
"重载方法需要结果类型"的错误就在这一行 foo (start, end, 14)
object HelloWorld {
def foo(start: String, end: String) = {
foo (start, end, 14)
}
def foo(start: String, end: String, id: Int) = {
println("Hello, world!")
}
def main(args: Array[String]): Unit = {
foo("hello", "scala")
}
}
Run Code Online (Sandbox Code Playgroud)
更正后的代码版本,
object HelloWorld {
def foo(start: String, end: String): Unit = {
foo (start, end, 14)
}
def foo(start: String, end: String, id: Int): Unit = {
println("Hello, world!")
}
def main(args: Array[String]): Unit = {
foo("hello", …Run Code Online (Sandbox Code Playgroud)