Scala: why duplicate declaration is allowed inside pattern matching? -


it seems able bind pattern identifier x , declare 1 more x identifier in same scope. why code work?

"123" match {   case x@"123" =>     // expected compilation error here, works. var works     val x = "456"     x // "456" } 

explaining link sls appreciated.

i don't think there's dedicated explanation of situation in sls, looking @ pattern matching expressions part of spec, 1 can find following wording:

a pattern matching expression

e match { case p_1 => b_1 … case p_n => b_n } 

the scope of pattern variables in p_i comprises pattern's guard , corresponding block b_i.

which can understood block b_i has own scope, nested scope of pattern variables.

now variable x bound in pattern variable scope p_i, , redefined in b_i scope. gives situation of nested scopes, , normal rules of name shadowing in nested scope apply.


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -