No Slide Title

Download Report

Transcript No Slide Title

research
product
TeSLa
Why The $@#%!
Visual Basic
Imports SySTem.STriNg
Case insensitive
Module Demo
Public Sub Main()
Dim S = Join(", ", { "Hello", "World"})
End Sub
End Module
Class imports
Static class
Why The $@#%!
Visual Basic
Class Dog
Public Event Bark()
End Class
Declarative
Event Handling
Dim WithEvents D As New Dog()
Sub OnBark() Handles D.Bark
Console.WriteLine("Call 911")
End Sub
"AOP"
Why The $@#%!
Visual Basic
Optional parameters
Function F(X As Integer, _
Optional Flag As Boolean = False) _
As String
…
End Function
F (Flag := True, X := 4711)
Named arguments
Why The $@#%!
Visual Basic
Class Dog
Public Event Bark()
Line continuations
Public Default Property Toys(Name As String) _
As Object
Get
…
Indexed
End Get
Properties
Set (Value As Object)
…
End Set
End Property
End Class
Why The $@#%!
Visual Basic
With Statement
Dim D As New Dog()
With D
.Toys("Bone") = New Bone()
Late
.Toys!Cat = CatchNeighboursCat()
binding
Console.WriteLine(.Toys("Ball").Color)
End With
Late binding
With Scope
(Power is in the ".")
Late Binding
K = J+3
For I=0 To 10
Console.WriteLine(I)
Next
Dim X As String = 4711
Function F(X)
Return X+3
End Function
Option
Implicit
Implicit Explicit
Conversions
Types Optional
Relaxed delegates
Dim WithEvents B As New Button()
Sub OnClick(sender As Object, e As EventArgs) _
Handles B.Click
End Sub
Sub RelaxedOnClick(sender As Object, e As Object) _
Handles B.Click
End Sub
Can call it  can
handle it
Relaxed delegates
Delegate Function F(A As S) As T
Function G(B As P) As Q
Conversion
stub
New F(AddressOf G)

New F(Function(A)
CType(G(CType(A,P)),T)
Why The $@#%!
Visual Basic
Extension Methods
<Runtime.CompilerServices.Extension()> _
Function FindName(Source As Object, Name As String) As Object
On Error Resume Next
If Source.Name = Name Then
Return Source
Unstructured
End If
Exception
Dim Children As Object = Source.Children
If Children IsNot Nothing Then
Handling
For Each Child As Object In Children
FindName = FindName(Child, Name)
If FindName IsNot Nothing Then
Late binding
Return FindName
End If
Next
End If
"AOP"
Return Nothing
End Function
Late Binding!
Type Rule For Early-Bound
Method Invocation
G |- e As S ~~> e’
G |- a As T ~~> a’
S•m(T) As R ~~> f
---------------------------------G |- e.m(a) As R ~~> f(e’,a’)
Late Binding!
Type Rule For Late-Bound
Method Invocation
G |- e As Object ~~> e’
G |- a As T ~~> a’
T <: Object ~~> g
----------------------------G |- e.m(a) <: Object
~~> latecall("m", e’, g(a’))
Late Binding!
Operational Semantics
For Late-Bound Call
s.GetType()  S,
t.GetType()  T
VB has multimethods!
S•m(T) <: R ~~> f
R <: Object ~~> h
---------------------------latecall(m,s,t)  h(f(s,t))
Late Binding
Class A
Sub F(X As String)
Imports
WriteLine("F(String)")
System.Console
End Sub
Sub F(X As Object)
WriteLine("F(Object)")
End Sub
Type
Sub Main()
Inference
Dim A = New A()
Dim S As Object = "Hello"
A.F(S)
REM Early  F(Object)
CObj(A).F(S)
REM Late  F(String)
End Sub
End Class
Meta-circular interpreter
ℰ[ A+B ]
=
Add(ℰ[ A ], ℰ[ B ])
Self
interpretation
is litmus test
for dynamic
language
ℰ[ A.m(B) ]
=
Call(ℰ[ m ], ℰ[ A ], ℰ[ B ])
Replace Constants
By Variables
G |- e <: Object ~~> e’
G |- a <: T ~~> a’
T <: Object ~~> f
G |- m <: String ~~> m'
-----------------------------------------------------G |- e.(m)(a) <: Object
~~> latecall(m', e’, f(a’))
Later
binding
Later Binding
Class A
Sub F(X As String)
WriteLine("F(String)")
End Sub
Sub F(X As Object)
WriteLine("F(Object)")
End Sub
Sub Main()
Dim A = New A()
Dim S As Object = "Hello"
A.(Console.ReadLine())(S)
End Sub
End Class
VB as the ultimate
scripting language
Static Typing
Where Possible
Dynamic
Typing Where
Necessary
Ruby
e = 8 + 9 \
+ 10
d = 4 + 5 +
6 + 7
Line continuation
Smart "inference"
def options(a=99, b=a+1)
[a,b]
Default arguments
end
redirect_to :action => 'show', :id => 4711
Named arguments via hashes
LINQ Project
== monad comprehensions in C# & VB
VB 9
C# 3.0
…
Standard
Query
Operators
DLinq
(relational)
XLinq
(xml)
LINQ Framework
The Origins of LINQ
Swartz, SETL 1970; Burstall and Darlington, NPL
1977; Turner, KRC 1981; …. Haskell, Python, Scala, …
Write programs using mathematical ZF set comprehensions
syntax.
Wadler 1989
List comprehensions are related to the relational calculus.
Wadler 1992
List comprehensions are a special case of monad
comprehensions.
HaskellDb
Query Monad (1997)
SELECT X.FirstName, X.LastName
FROM Authors AS X
WHERE X.City = 'OakLand'
Query
monad
oaklands =
do{ x <- table authors
; restrict (x!city .==. constant "Oakland")
; project ( au_fname = x!au_fname
, au_lname = x!au_lname
)
}
intentional representation
for expressions
Query Comprensions
In C# 3.0
var contacts =
from c in customers
where c.State == "WA"
select new { c.Name, c.Phone };
Type inference
Syntactic sugar
over standard
query operations
var contacts =
customers
.Where(c => c.State == "WA")
.Select(c => new{c.Name, c.Phone});
-expression
Query Comprensions
In Visual Basic 9
Dim Squares = From N In Range(0,99) Select N*N
Dim Pairs = From N In Range(0,9)
From C In "ABCDEFGHIJ"
Select N, C
Dim Evens = From N In (0,99)
Where N Mod 2 = 0
Select N
Dim ByDigit = From N In (0,99)
Group By N Mod 10
Select Key, It
In Visual Basic 9
Dim Contacts =
From C In Customers
Where C.State = "WA"
Select C.Name, C.Phone
Dim Contacts = _
Customers.
Where(Function(C) C.State = "WA").
Select(New With {
.Name = C.Name, .Phone = C.Phone
})
Query Comprensions
In Visual Basic 9
Dim MyCDs As IEnumerable(Of _
{ Title As String
, Artist As String
}) = _
From CD In MyMusic
Where CD.Genre IsNot Classic
Group By Genre = CD.Genre
Where Count(CD) > 10
Select Group(CD.Title, CD.Artist)
Aggregate
comprehensions
Query Comprehensions
Xs.SelectMany((X) _
F(X).Select((Y)_
New {X,Y}))
X
From Y In F(X)
X,Y
Joined
Compiled To
Standard
Query
Operators
Query Comprehensions
X,Y
XYs.OrderBy((XY) _
F(XY.X, XY.Y))
Order By F(X,Y)
X,Y
Sorted
Query Comprehensions
X,Y
Where P(X,Y)
X,Y
Filtered
XYs.Where((XY) _
P(XY.X, XY.Y))
Query Comprehensions
X,Y
projected
Select A = F(X,Y), B = G(X,Y)
A,B
XYs.Select((XY) _
New {A = F(XY.X, XY.Y), _
B = F(XY.X, XY.Y) })
Query Comprehensions
XYs.GroupBy((XY) _
New {A = G(XY.X, XY.Y), _
B = H(XY.X, XY.Y) })
X,Y
Group By A = G(X,Y), B = H(X,Y)
A,B,
X,Y
grouped
Extension Methods
static class Integer {
static void Times
(this int x, Action<int> f){
for(var i = 0; i < x; i++) f(i);
}
3.Times((x) => {
Console.WriteLine(x);
}
Extension Methods
IEnumerable<Customer>
var contacts =
customers
.Where(c => c.State == "WA")
.Select(c => new{c.Name, c.Phone});
static class System.Query
{
public static IEnumerable<T>
Where<T>(this IEnumerable<T> src,
Func<T, bool>> p);
…
Extension method for IEnumerable<T>
}
In Visual Basic 9
Dim Contacts = _
Customers.Where(P).Select(R)
Module MyExtensions
<Runtime.CompilerServices.Extension()> _
Function Where(Of T)( _
[Me] As IEnumerable(Of T), _
P As Func(Of T, Boolean)) _
As IEnumerable(Of T)
…
End Module
Just CA
Expression Trees
Table<Customer>
var contacts =
customers
.Where(c => c.State == "WA")
.Select(c => new{c.Name, c.Phone});
class Table<T>: IEnumerable<T>
{
public Table<T>
Where(Expression
<Func<T, bool>> p);
…
}
Intensional representation of
delegate
Expression Trees
Convert to expression tree by type
Dim F As Expr = Function(X)X+3
Dim G = F.Compile()
Dim Z = G(5)
Compile to delegate
Execute
Anonymous types,
Object initializers
var contacts =
Anonymous
from c in customers
types
where c.State == "WA"
select new { c.Name, c.Phone };
Object Initializers
var Joe = new Person{
Name = “Joe”, Age = 42,
Address = { Street = “1th St”,
City = “Seattle” }
}
Initialize RO member
In Visual Basic 9
Dim Contacts As IEnumerable(Of _
{ Name As String, Phone As Integer } =
From c In customers _
Where C.State == "WA“ _
Anonymous
Select c.Name, c.Phone
types
Dim Joe As Person = New With { _
.Name = “Joe”, .Age = 42, _
Address With { _
.Street = “1th St”, .City = “Seattle” }
}
Syntax new
Nullable
Dim S1 As String? = Nothing
Dim S2 As String? = “Hello”
Dim S3 = S1+S2
Null propagating +
In VB Nothing is default
XML DOM
Dim PO As New XmlDocument
Dim purchaseOrder As XmlElement = _
PO.CreateElement("purchaseOrder")
PO.AppendChild(purchaseOrder)
Dim orderDate As XmlAttribute =
PO.CreateAttribute("orderDate")
orderDate.Value = "1999-10-20"
purchaseOrder.Attributes.Append(orderDate)
Dim shipTo As XmlElement =
PO.CreateElement("shipTo")
purchaseOrder.AppendChild(shipTo)
Dim country As XmlAttribute =
PO.CreateAttribute("country")
country.Value = "US"
shipTo.Attributes.Append(country)
What
does
this
program
do?
XLinq API
Functional (expressionbased) construction
Dim Item = _
New XElement("item", _
New XAttribute("partNum", "926-AA"), _
New XElement("productName", “…"), _
New XElement("quantity", 1), _
New XElement("price", 39.98), _
New XElement("shipDate", "1999-05-21"))))
Simple
Context-free (no
document scope)
Haskell Server Pages
XHTML Literals (1998)
table :: TABLE
table = <TABLE border="1">
<% mkRows cells %>
</TABLE>
Translated to
universal DOM
representation
cells :: [[(Int,Int)]]
cells = [[ (x,y) | x <- [1..16] ] | y <- [1..16] ]
mkRows :: [[(Int,Int)]] -> [TR]
mkRows =
map $ \cs -> <TR><% mkColums cs %></TR>
ASP-style
embedding
mkColumns :: [(Int,Int)] -> [TD]
mkColums =
map $ \c -> <TD bgcolor=(color c)><% c %></TD>
Dim PO = <purchaseOrder orderDate=(System.DateTime.Today)>
<shipTo country="US">
<name>Alice Smith</name>
<street>123 Maple Street</street>
<city>Mill Valley</city>
<state>CA</state>
Translated to
<zip>90952</zip>
XLinq constructor
</shipTo>
<%= BillTo %>
calls
<items>
<%=
Select <item partNum=<%= O.PartID %>>
<productName>
<%= O.Product %>
ASP-style
</productName>
embedding
<quantity>
<%= O.Quantity %>
</quantity>
<price>
<%= O.Price %>
</price>
</item>
From O In Orders
Includes full
Where O.Name = "Robert Smith"
namespace
%>
</items>
support
</purchaseOrder>
Late binding over XML
Child axis
BillTo.<street>
 BillTo.Elements(“street”)
Attribute axis
BillTo.@country
 BillTo.Attributes(“country”)
Descendants axis
PO...<item> 
PO.Descendants(“item”)
Tesla
• Tools & IDE
• Type system & Language
extensions
• Runtime & Library
support
• Transactions everywhere
Conclusion
VB IsNot C#
VB = static typing where
possible, dynamic typing
where necessary.