xml - Scala – companion object & apply: non understandable error -
i can't create class representing xml parsed document, using companion object.
here code of class:
package models import javax.xml.bind.element import scala.xml.elem import javax.xml.validation.schemafactory import javax.xml.transform.stream.streamsource trait myxml { case class elémentxml(code_xml: scala.xml.elem) { def validate: boolean = { try ({ val schemalang = "http://www.w3.org/2001/xmlschema" val factory = schemafactory.newinstance(schemalang) val schema = factory.newschema(new streamsource("sites_types_libelles.xsd")) val validator = schema.newvalidator() validator.validate(new streamsource(code_xml.tostring)) true }) catch { case t:throwable => false } } } object elémentxml { def apply(fichier: string) { try{ val xml_chargé = xml.xml.loadfile(fichier) some(new elémentxml(xml_chargé)) }catch{ case e:throwable => none } } } }
and here code app using class:
val xml1:elémentxml = elémentxml("app/models/exemple_bon.xml") xml1 must not beequalto(none)
the error is:
type mismatch; found : string("app/models/exemple_bon.xml") required: scala.xml.elem
i don't understand error(and how can remove this).
thanks!
your apply method procedure. amend apply(fichier: string): elémentxml = ...
.
the overload synthetic case apply resolved expected type.
this why procedure syntax deprecated:
apm@mara:~/tmp$ scala -xfuture -deprecation welcome scala version 2.11.0-20140129-135431-0e578e6931 (openjdk 64-bit server vm, java 1.7.0_25). type in expressions have them evaluated. type :help more information. scala> def f() { } <console>:1: warning: procedure syntax deprecated. convert procedure `f` method adding `: unit =`. def f() { } ^ <console>:7: warning: procedure syntax deprecated. convert procedure `f` method adding `: unit =`. def f() { } ^ f: ()unit
one curious effect of last line works value discard:
scala> :pa // entering paste mode (ctrl-d finish) case class c(c: int) object c { def apply(s: string): unit = c(s.toint) } // exiting paste mode, interpreting. defined class c defined object c scala> c(4) res2: c = c(4) scala> c("4") scala> val x: c = c(4) x: c = c(4) scala> val x: c = c("4") <console>:11: error: type mismatch; found : string("4") required: int val x: c = c("4") ^ scala> val x: unit = c("4") x: unit = () scala> val x: unit = c(4) // works silently x: unit = ()
Comments
Post a Comment