4
被初始化
我已經在Java中,下面的方法:確保VAL在科特林
public void doSomething() {
final boolean promote = false;
final String bob;
if (promote) {
try(StringWriter sw = new StringWriter()) {
sw.write("this is a test");
bob = sw.toString();
} catch (IOException e) {
e.printStackTrace();
throw new IllegalStateException();
}
} else {
bob = "anaconda";
}
System.out.println(bob);
}
當我將它轉換爲科特林:
val promote = false
val bob: String
if (promote) {
try {
StringWriter().use { sw ->
sw.write("this is a test")
bob = sw.toString()
}
} catch (e: IOException) {
e.printStackTrace()
throw IllegalStateException()
}
} else {
bob = "anaconda"
}
println(bob)
,但我得到的最後一行編譯器錯誤: Variable 'bob' must be initialized.
我看不出Kotlin可能無法初始化bob
變量,因爲當Java編譯器確信變量已被初始化或異常已被拋出。
我是唯一選擇將bob
更改爲var
並初始化它嗎?
好吧,由於使用內聯,編譯器將知道如果傳遞的函數被調用,那會立即發生。 – Rocoty
@Rocoty注意'use'被內聯的事實取決於提供函數的庫(在這種情況下是stdlib)。想象一下這樣的實現: 'inline fun T.use(block:(T) - > R):R? {if(otherFunc()){block(this)}} else {null}'。取決於如果內聯了'otherFunc()',編譯器可以嘗試找出更多的東西,但它很快變得複雜。 –
miensol