-
Notifications
You must be signed in to change notification settings - Fork 0
Declaring and using unmarshallers
<?scala { expression } ?>
is translated to:
$xmlUnmarshaller = { lazy val $xmlUnmarshaller: XMLUnmarshaller= expression; expression }
The only reason for the syntax was that it was 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?
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.
Proposed syntax:
scalaPI ::= '<?scala' unmarshallerDeclaration '?>'
unmarshallerDeclaration ::= prefixUnmarshallerDecl
| localNameUnmarshallerDecl
prefixUnmarshallerDecl ::= xmlprefix ':_=' expression
A default unmarshaller for namespaces without an explicit declaration could be declared by using an underscore as prefix: _:_=expression
.
localNameUnmarshallerDecl ::= localName = expression
These would apply only to literals and patterns which start with a non-element or an unprefixed element name. The default unmarshaller for names which don't have an explicit one can be declared using an underscore: _=expression
.
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. 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.
Note: Chris Twiner advocates for URIs and suggests to look at Scales Xml's use of QName and AttributeQName -- describes use of prefixes as an historical mistake not to be repeated. Consider this & also think of a way to define namespace prefixes with scopes larger than a single XML literal (e.g. in the scala PI itself), to avoid excessive verbosity.