Skip to content

Declaring and using unmarshallers

jsalvata edited this page Nov 6, 2011 · 8 revisions

Current implementation

<?scala { expression } ?>

is translated to:

$xmlUnmarshaller = { lazy val $xmlUnmarshaller: XMLUnmarshaller= expression; expression }

The only reason for the syntax was that it is the easiest to implement.

The reason for the "lazy val" is to provide a modicum of type checking: unmarshallers must implement XMLUnmarshaller (just a marker trait). Is there a better way?

PIs or implicits?

Implicits was the other obvious solution, but:

  • It was more difficult (maybe impossible) to implement without restricting the compile-time type of the unmarshaller -- which would defeat the purpose of the whole project.

  • It is less obvious how to extend it to use different unmarshallers for different namespaces/prefix, which would be a great advantage for code combining, e.g. an HTML unmarshaller an an I18n unmarshaller.

Discussion welcome.

Syntax and semantics

Proposed syntax:

scalaPI ::= '<?scala' unmarshallerDeclaration '?>'

unmarshallerDeclaration ::= prefixUnmarshallerDecl
                          | localNameUnmarshallerDecl

prefixUnmarshallerDecl ::= xmlprefix ':' '=' expression

The default unmarshaller would be declared by using an empty-string as prefix: :=expression

localNameUnmarshallerDecl ::= localName = expression

These would apply only to all literals and patterns which don't start with a prefixed element name.

Prefixes or URIs?

Using namespace URIs would be the formally correct way in the XML world. But we're actually in the Scala world, and XML literals representing document fragments are much more frequent than full documents, which would make using URIs verbose and error-prone. For these reasons I believe prefixes are a better option. If the namespace bindings are changed in the literals, the programmer will need to change his processing instruction too.

Clone this wiki locally