Scala Pattern Matching Compiler Warning -


i caused bug in code tougher find have liked, , i'd ideally avoid in future. expected scala compiler have warned me error (unless i'm missing something).

i've reduced trivial case:

vector.maybegetvector match { case v:vector => true case _ => false } case class vector(x:int, y:int) object vector { def maybegetvector : option[vector] = some(new vector(1,2)) } 

the reason used wildcard instead of none fall through want match on subtype of returned option.

i expecting compiler warning, it's easy reason first case statement contains unreachable code. option[vector] can't subtype of vector.

the strange part if add following case statement:

 case i:int => false 

it raises error , tells me option[vector] required.

is there way protect against programmer error in way, outside of naming conventions. things can ever match against option some/none/null. feel i'm missing obvious.

if define vector class final modifier, you'll "pattern type incompatible expected type" error you're expecting.

to me says scala thinks option[vector] might somehow instance of subtype of vector. (this seems impossible me, think that's reasoning @ work.) making vector final precludes seemingly remote possibility.

so, if first case statement should unreachable, it's not because, said, vector can't subtype of option[vector]; rather because option[vector] can't instance of subtype of vector. maybe that's meant :)


Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -