First of all, the warning has nothing to do with the fact that you are using an unsafe method. If you restart REPL with -feature
, you will see the following:
scala> arr tail <console>:9: warning: postfix operator tail should be enabled by making the implicit value language.postfixOps visible...
And a pointer to the documentation for scala.language.postfixOps
, which includes the following note:
Postfix statements interact poorly with semicolon pins. Most programmers avoid them for this reason.
This is good advice. The ability to write arr tail
instead of arr.tail
not worth the fuss.
Now about the security issue. The standard library does not provide tailOption
for collections, but you can get the same effect using headOption
and map
:
scala> val arr = Array(1, 2) arr: Array[Int] = Array(1, 2) scala> arr.headOption.map(_ => arr.tail) res0: Option[Array[Int]] = Some([ I@359be9fb )
If this is too verbose or opaque or inefficient for you, you can easily create an implicit class that will add a more nice tailOption
to Array
(or Seq
, or IndexedSeq
, etc.).
source share