There's been some
interest recently in the new
XML literal feature coming
to
Visual Basic 9. If you're not familiar with this feature, the idea is that you
can embed XML directly into VB code like this:
Sub Main()
Dim publicationdate = Date.Today
Dim isbn = 42
Dim price = 0.99D
Dim firstName = "Dustin"
Dim lastName = "Campbell"
Dim book = <book publicationdate=<%= publicationdate %> ISBN=<%= isbn %>>
<title>F#: For The Excessively Nerdy</title>
<price><%= price %></price>
<author>
<first-name><%= firstName %></first-name>
<last-name><%= lastName %></last-name>
</author>
</book>
End Sub
That compiles to something similar to this:
Public Sub Main()
Dim publicationdate As Date = Date.Today
Dim isbn As Integer = 42
Dim price As Decimal = 0.99
Dim firstName As String = "Dustin"
Dim lastName As String = "Campbell"
Dim book = New XElement("book", _
New XAttribute("publicationdate", publicationdate), _
New XAttribute("ISBN", isbn), _
New XElement("title", "F#: For The Excessively Nerdy"), _
New XElement("price", price), _
New XElement("author", _
New XElement("first-name", firstName), _
New XElement("last-name", lastName)))
End Sub
To me, this is a great example of what syntactic sugar should be all about:
making tasks easier for developers. The VB team has gone to great
pains to expose the new APIs in System.Xml.Linq in the most intuitive way possible. As a C# guy, I'm
shamefully filled with deep feelings of
VB
envy.
Since I spend most of my time working on a
wholly
remarkable refactoring tool, you might be wondering what sort of refactoring
support we have in store for these snazzy new XML literals. How about
Extract Method?
Here's the preview hint that is displayed for Extract Method when the XML
literal is selected:

And here's the successfully refactored code after applying Extract Method:
Private Function GetBook(ByVal publicationdate As Date, ByVal isbn As Integer, _
ByVal price As Decimal, ByVal firstName As String, _
ByVal lastName As String) As XElement
Return <book publicationdate=<%= publicationdate %> ISBN=<%= isbn %>>
<title>F#: For The Excessively Nerdy</title>
<price><%= price %></price>
<author>
<first-name><%= firstName %></first-name>
<last-name><%= lastName %></last-name>
</author>
</book>
End Function
Sub Main()
Dim publicationdate = Date.Today
Dim isbn = 42
Dim price = 0.99D
Dim firstName = "Dustin"
Dim lastName = "Campbell"
Dim book = GetBook(publicationdate, isbn, price, firstName, lastName)
End Sub
Notice the pieces that
Refactor!
must have to be in place to get this right:
- The refactoring must be smart enough to understand how the XML literal
is transformed into XElements, XAttributes and XNames under the hood.
- The refactoring must identify any dependant variables that are
referenced within the embedded expressions of the XML literal.
- The refactoring must infer the types of the dependant variables in order
to declare the parameters of the new method.
We still have some work to do before Visual Studio 2008 reaches the RTM
stage, but it looks like things are shaping up nicely.