4

这个Java代码的Scala等价物是什么,在哪里someMethodThatMightThrowException定义?

class MyClass {
    String a;
    String b;

    MyClass() {
        try {
            this.a = someMethodThatMightThrowException();
            this.b = someMethodThatMightThrowException();
        } finally {
            System.out.println("Done");
        }
    }
}
4

4 回答 4

5
class MyClass {
  private val (a, b) =
    try {
      (someMethodThatMightThrowException(),
       someMethodThatMightThrowException())
    } finally {
      println("Done")
    }
}

try是 Scala 中的表达式,因此您可以使用它的值。使用元组和模式匹配,您可以使用语句来获取多个值。

或者,您可以使用与 Java 中几乎相同的代码:

class MyClass {
  private var a: String = _
  private var b: String = _

  try {
    a = someMethodThatMightThrowException()
    b = someMethodThatMightThrowException()
  } finally {
    println("Done")
  }
}
于 2013-09-03T18:10:08.257 回答
1

伴随对象

case class MyClass(a: String, b: String)

object MyClass {
   def apply() = try { 
      new MyClass(
         a = someMethodThatMightThrowException(), 
         b = someMethodThatMightThrowException()
      ) 
   } finally {
      println("Done") 
   }
}

构造函数重载有点困难,因为我们不能包装这个(...):

def tryIt[T](something: => T) = try{
      something
   } finally {
      println("Done") 
   }

case class MyClass(a: String, b: String) {
   def this() = this(
      tryIt(someMethodThatMightThrowException),  
      tryIt(someMethodThatMightThrowException)
   ) 
}
于 2013-09-03T18:16:31.790 回答
1

更接近的东西怎么样:

scala> def foo = ???
foo: Nothing

scala> :pa
// Entering paste mode (ctrl-D to finish)

case class Foo(a: String = Foo.afoo, b: String = Foo.bfoo)
object Foo {
import util._
def afoo = Try (foo) recover { case _ => "a" } get
def bfoo = Try (foo) recover { case _ => "b" } get
}

// Exiting paste mode, now interpreting.

warning: there were 2 feature warning(s); re-run with -feature for details
defined class Foo
defined object Foo

scala> Foo()
res0: Foo = Foo(a,b)
于 2013-09-03T20:32:39.040 回答
1

如果发生异常,分配什么a或分配什么?bWrap aand bin aTry处理异常情况。您还可以对这些进行模式匹配以提取值。

scala> class MyClass(val a: Try[String], val b: Try[String])
defined class MyClass

scala> new MyClass(Try("foo"(0).toString), Try("foo"(3).toString))
res0: MyClass = MyClass@6bcc9c57

scala> res0.a
res1: scala.util.Try[String] = Success(f)

scala> res0.b
res2: scala.util.Try[String] = Failure(java.lang.StringIndexOutOfBoundsException: String index out of range: 3)

scala> res0.a.get
res3: String = f

scala> res0.b.get
java.lang.StringIndexOutOfBoundsException: String index out of range: 3
        at java.lang.String.charAt(String.java:658)
        ...

编辑评论。a对和使用默认参数b

null很糟糕,但这就是你所要求的。见选项

class MyClass(val a: Try[String] = null, val b: Try[String] = null)

scala> new MyClass(Success("a"))
res50: MyClass = MyClass@625aaaca

scala> res50.a
res51: scala.util.Try[String] = Success(a)

scala> res50.b
res52: scala.util.Try[String] = null

scala> new MyClass(b = Success("b"))
res53: MyClass = MyClass@68157e85

scala> res53.a
res54: scala.util.Try[String] = null

scala> res53.b
res55: scala.util.Try[String] = Success(b)
于 2013-09-03T18:17:31.140 回答