Scala: Help me understand comprehensions and case classes in this code
Question: I am trying to wrap my head around Scala, and I am finding it quite
challenging so far. I found this library (<https://github.com/snowplow/scala-
maxmind-geoip>) I used in the past with Python for finding out stuff like the
country base on an IP address.

So the example is pretty simple

    
    
    import com.snowplowanalytics.maxmind.geoip.IpGeo
    
    val ipGeo = IpGeo(dbFile = "/opt/maxmind/GeoLiteCity.dat", memCache = false, lruCache = 20000)
    
    for (loc <- ipGeo.getLocation("213.52.50.8")) {
      println(loc.countryCode)   // => "NO"
      println(loc.countryName)   // => "Norway" 
    }
    

And the documentation reads

> The getLocation(ip) method returns an IpLocation case class

So, if it is a case class, why this does not work?

    
    
    val loc = ipGeo.getLocation("213.52.50.8")
    println(loc.countryCode)
    

After all I can do

    
    
    case class Team(team: String, country: String)
    val u = Team("Barcelon", "Spain")
    scala> u.country
    res5: String = Spain
    

Thank you for your time!


Answer: I guess the documentation is just outdated there. If you look in [the
code](https://github.com/snowplow/scala-maxmind-
geoip/blob/master/src/main/scala/com/snowplowanalytics/maxmind/geoip/IpGeo.scala)
it does not return a `IpLocation` but an `Option[IpLocation]`.

`Option` is a type from the scala standard library that has two constructors:
`None` and `Some(value)`. So it is used where values are optional.

`for` in scala is just syntactic sugar. `for (x <- xs) { println(x) }` is
translated into `xs.foreach(x => println(x))`. So you basically call the
`foreach` on option, which executes your printlines if a value was returned by
`getLocation`.

