scalacheck - Type mismatch error in a scala for comprehension -
i writing generators following adts. idea generate blocks random data. having compiler error type mismatch: expected: seq[field], actual:gen[field]
in blockgen
method. doing wrong?
edit
the error fields
on last line of method i.e. yield block(id, fields)
.
def blockgen(b: block): gen[block] = { id <- b.blockid fields <- b.fields.map(f => fieldgen(f)) } yield block(id, fields)
adt
trait data {} trait field extends data { val name: string val value: string } case class stringfield(name: string, value: string) extends field case class numberfield(name: string, value: string) extends field case class block(blockid: field, fields: seq[field]) extends data
generators
def blockgen(b: block): gen[block] = { id <- b.blockid fields <- b.fields.map(f => fieldgen(f)) } yield block(id, fields) def fieldgen(fieldtype: field): gen[field] = { { f <- fieldtype match { case _: numberfield => numgen case _: stringfield => strgen } } yield f } val strgen: gen[stringfield] = { name <- gen.identifier value <- gen.alphastr } yield stringfield(name, value) val numgen: gen[numberfield] = { name <- gen.identifier value <- gen.numstr } yield numberfield(name, value)
this caused by: fieldgen(f)
return type gen[field]
, block
fields
type seq[field]
, compiler throw type mismatch
.
solution:
- change
def fieldgen(fieldtype: field): gen[field]
def fieldgen(fieldtype: field): seq[field]
, or changeblock
fields typegen[feild]
create implicit conversion
gen
seq
, like:implicit def gentoseq[t](s: gen[t]): seq[t] = ??? // implementation conversion.
Comments
Post a Comment