views:

71

answers:

1

I have this case class define:

class Protocol(protocol:String) 

object Protocol {
    def apply(protocol:String) :Protocol = {
      protocol.toUpperCase match {
        case "HTTP" => Http()
        case "HTTPS" => Https()
        case "Ftp" => Ftp()
        case "Mail" =>Mail()
        case other => new Protocol(other)
    }
}
}

case class Http() extends Protocol("HTTP") {}

Which I then use in this case class:

case class Url(protocol: Protocol,
  username: Option[String],
  password: Option[String],
  domainName: DomainName,
  port: Option[Int], 
  path: Option[List[String]], 
  parameters: Option[List[Parameter]]) {

And then try to use here:

"An url class" should {
    "represent http://localhost" in {
        val url = Url(Http, None, None, localhost, None, None, None)
            url.toString must beEqualTo("http://localhost")
    }

For which I get the following baffling compiler error:

[error] C:\Users\Jim.Barrows\Desktop\workspaces\utils\src\test\scala\UrlSpec.scala:16: type mismatch;
[error]  found   : bizondemand.utils.models.internet.Http.type (with underlying type object bizondemand.utils.models.internet.Http)
[error]  required: bizondemand.utils.models.internet.Protocol
[error]                         val url = Url(Http, None, None, localhost, None, None, None)

What am I doing wrong?

+4  A: 

The error is here:

Url(Http, None, None, localhost, None, None, None)
    ^^^^

Since you defined Http as a class, not an object, you need to do Http() to create an instance. Or even better: define Http as a case object in the first place.

It is generally preferred to use case object rather than case classes without arguments.

sepp2k