Too Dynamic

Normally, XML is untyped, by itself it certainly is, although there are a plethora of schema languages in the wild, all too willing to project types onto an otherwise untyped structure. XML Schema and what have you. And so when C# 4.0 introduced the dynamic type, some people thought this may actually be the ticket out of syntax hell for dealing with XML in C#. Not so in my oppinion. An example

book.isbn

Now book will of type dynamic and will contain an XElement underneath. Method dispatch on this dynamic object will identify the method name being called and basically use that name to do _xelement.Element(methodName). There are problems with this approach.

First of all, XML name syntax is not a syntactic subset of the C# method identifier syntax, and so one cannot cleanly map XML names into C# method names. And when introducing namespaces all thoughts of compatibility goes out the door.

Let’s just take plain XSLT as an example

xslt:value-of

The colon is the first show-stopper. The next is the dash, which is the minus operator.

Some XML dialects also use a dot. XAML is one such language.

StackPanel.Resources

Oops, now we have the quintessential operator in C#, the member invocation operator, now being used by the XML and we have to express this in naming convention somehow.

The abstraction breaks down.

There is a deCast video on Channel 9 about this approach to deal with XML and while it’s clever, it just breaks down in my oppinion. I’d be very happy to be corrected in this matter because dynamic dispatch is really cool for this.

The video even goes as far as introducing magic names such as Parent and Nodes that are interpreted differently than element names. Parent will fetch the parent element, for example. This will collide if you have an XML name called Parent. I suppose in that example just don’t use Parent. The extensibility of XML allows new names to be introduced and used without breaking existing code – it’s one of its strengths – and so one might unexpectedly run into an XML name called Parent in the wild, in a format where one thought this was safe. So suddenly language evolution has broken ones code. Not good!

Nice idea though.

References

XML 1.0 name syntax

NameStartChar:
“:” | [A-Z] | “_” | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]

NameChar:
NameStartChar | “-” | “.” | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040]

Name:
NameStartChar (NameChar)*

C# 1.0 identifer syntax

identifier:
available-identifier
@ identifier-or-keyword

available-identifier:
An identifier-or-keyword that is not a keyword

identifier-or-keyword:
identifier-start-character identifier-part-charactersopt

identifier-start-character:
letter-character
_ (the underscore character U+005F)

identifier-part-characters:
identifier-part-character
identifier-part-characters identifier-part-character

identifier-part-character:
letter-character
decimal-digit-character
connecting-character
combining-character
formatting-character

letter-character:
A Unicode character of classes Lu, Ll, Lt, Lm, Lo, or Nl
A unicode-escape-sequence representing a character of classes Lu, Ll, Lt, Lm, Lo, or Nl

combining-character:
A Unicode character of classes Mn or Mc
A unicode-escape-sequence representing a character of classes Mn or Mc

decimal-digit-character:
A Unicode character of the class Nd
A unicode-escape-sequence representing a character of the class Nd

connecting-character:
A Unicode character of the class Pc
A unicode-escape-sequence representing a character of the class Pc

formatting-character:
A Unicode character of the class Cf
A unicode-escape-sequence representing a character of the class Cf

Advertisements

About xosfaere

Software Developer
This entry was posted in Datamodel, Declarative, Infoset, Software, Technical and tagged , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s