I have started learning about Play, and in the tutorials that I saw, the model usually has two components: a case class and an object.
I have created a model with an object and a case class. My question is how do I reference a field(declared in the case class) from the object:
package models
import java.net.URL
import play.api.Logger
import play.api.db.DB
import play.api.libs.json.Json
case class Page(url: String)
object Page {
implicit val personFormat = Json.format[Page]
def readPageContent(): String = {
var content: String = new URL(this.url).getContent().toString
return content
}
}
For instance, here in the object, I am trying to reference the field url using this.url, but I get cannot resolve symbol url.
How can I reference the field?
this.urlfrom the object, because the object does not have a field namedurl. If you're trying to access theurlfrom an instance of the class, then how is the object supposed to know from which instance of the class you want to accessurl? This is the equivalent of the Java question where you cannot access instance members from a static method. It looks like you have a misunderstanding about Scala classes and objects.