sanjib
sanjib

Reputation: 593

How to make the following code type safe?

say, I have a generic command trait with an execute method that takes an Input and returns an Output. Something like

trait Input;
trait Output;

trait Command[I <: Input, O <: Output] {
  def execute(input: I): O;
}

Then, I am going to create various Commands, something like

class SampleInput extends Input
class SampleOutput extends Output

class SampleCommand extends Command[SampleInput, SampleOutput] {
  def execute(input:SampleInput):SampleOutput = new SampleOutput()
}

The problem with this is I could create a Command with a SampleAInput and SampleBOutput and the compiler will accept that happily. How do I enforce that so the compiler fails with type mismatch error ?

Somehow, I need to group Input and Output under a type and pass that type to create a command. How do I do that?

Upvotes: 6

Views: 651

Answers (3)

Landei
Landei

Reputation: 54574

I'm a little bit late, but how about this:

object inout {

  trait ~>[I, O]

  trait FooInput
  trait FooOutput

  trait BarOutput

  //this specifies a valid input-output combination
  implicit object fooInOut extends ~>[FooInput,FooOutput]

  class Command[I, O](implicit val valid: I ~> O) {
    def execute(input: I): O;
  }

  class FooCommand extends Command[FooInput, FooOutput]

  //won't compile:
  //class FubarCommand extends Command[FooInput, BarOutput]
}

Upvotes: 0

IttayD
IttayD

Reputation: 29123

trait InputOutput {
  type Input
  type Output
}

trait Command[IO <: InputOutput] {
  def execute(input: IO#Input): IO#Output
}

Here's some usage:

scala> trait SampleIO extends InputOutput {type Input = String; type Output = String}
defined trait SampleIO

scala> class SampleCommand extends Command[SampleIO] {def execute(input: String) = input}
defined class SampleCommand

scala> class SampleCommand extends Command[SampleIO] {def execute(input: String) = 1}
<console>:13: error: type mismatch;
 found   : Int(1)
 required: SampleIO#Output
       class SampleCommand extends Command[SampleIO] {def execute(input: String) = 1}
                                                                                 ^

Upvotes: 18

stevej
stevej

Reputation: 69

Since your constraint is that the type of Input and Output be the same, I would try the following:

trait Input[T]
trait Output[T]

trait Command[T] {
  def execute[I <: Input[T], O <: Output[T]](i: I): O  
}

Let's try this with two different types.

class SampleInput extends Input[String]
class SampleOutput extends Output[Int]


scala> class SampleCommand extends Command[String] {                 
     |   def execute(input: SampleInput): SampleOutput = new SampleOutput
     | }
:10: error: class SampleCommand needs to be abstract, since method execute in trait Command of type [I <: Input[String],O <: Output[String]](i: I)O is not defined
       class SampleCommand extends Command[String] {
             ^

Upvotes: 6

Related Questions