Where does this .get(x) behavior come from?

Viewed 79
Some("abcdefg").get(3)  //res0: Char = d

The argument to get() is sent to the apply() method of the String, but the source code for Option (and Some) doesn't have a get() method that takes parameters, and String has no get method at all.

So what get() is being invoked? Is it a mole from Java land?

2 Answers

It is StringOps.apply from implicit conversion

augmentString(Some("abcdefg").get)(3)

where augmentString is

@inline implicit def augmentString(x: String): StringOps = new StringOps(x)

Actually, in this case .get(x) is not a separate get() method but an abbreviation for .get.apply(x). So it's the standard get method on the Some type. No implicits required.

Some(Seq(99,32,12,7,101)).get(3)  //res0: Int = 7

Many thanks to @Mario Galic for pointing me in the right direction.

Related