2017-09-21 5 views
0

RuntimeExceptionの子をとることができるスカラーメソッドを記述したいと思います。私はそれを持っていますが、それはコンパイルされません。コードの何が間違っていますか?RuntimeExceptionのサブクラスを受け入れることができるスカラーメソッドを書く

def testme(e: RuntimeException): String = { 
    case e:BadRequestException=> "bad request" 
    case e: IllegalArgumentException=>"illegal argument" 
} 

私は、例えば、あなたはあなたが一致しているかを指定する必要があり

missing parameter type for expanded function 
[error] The argument types of an anonymous function must be fully known. (SLS 8.5) 
[error] Expected type was: String 
[error] def testme(e: RuntimeException): String = { 
[error]           ^
[error] one error found 
[error] (playWeb/compile:compileIncremental) Compilation failed 
[error] Total time: 5 s, completed Sep 21, 2017 2:45:09 PM 

答えて

2

以下のエラーが出ます

def testme(e: RuntimeException): String = e match { 
    case e:BadRequestException=> "bad request" 
    case e: IllegalArgumentException=>"illegal argument" 
} 
+0

おかげでなく、Scalaのパターンマッチングは、このチェックで正確なサブクラスのインスタンスに一致するように起こっているか、それがない場合があります:e matchを追加しますか? – curiousengineer

+0

'e:BadRequestException'を実行すると、' e'が 'BadRequestException'のインスタンスまたはそのサブクラスのインスタンスである場合に一致します。ケースは順番に照合されるので、より具体的なケースを先に入力します。 –

+0

ありがとうございます。はい、私は得る。基本的に最も厳しい(またはほとんどの子クラス)は、まずあなたが意味するものです。私はRuntimeExceptionを渡すことによってそれがまだマッチすることができ、それができると思うなら混乱していました。それ以外の場合は、どういう意味ですか? – curiousengineer

関連する問題