September Morn Update

This commit is contained in:
Ingy döt Net 2019-09-12 10:33:56 -07:00
parent 4e2d22a71d
commit aac6731f2c
6856 changed files with 141342 additions and 21127 deletions

View file

@ -0,0 +1,39 @@
' Delegate declaration is similar to C#.
Delegate Function Func2(a As Integer, b As Integer) As Integer
Module Program
Function Add(a As Integer, b As Integer) As Integer
Return a + b
End Function
Function Mul(a As Integer, b As Integer) As Integer
Return a * b
End Function
Function Div(a As Integer, b As Integer) As Integer
Return a \ b
End Function
' Call is a keyword and must be escaped using brackets.
Function [Call](f As Func2, a As Integer, b As Integer) As Integer
Return f(a, b)
End Function
Sub Main()
Dim a = 6
Dim b = 2
' Delegates in VB.NET could be created without a New expression from the start. Both syntaxes are shown here.
Dim add As Func2 = New Func2(AddressOf Program.Add)
' The "As New" idiom applies to delegate creation.
Dim div As New Func2(AddressOf Program.Div)
' Directly coercing the AddressOf expression:
Dim mul As Func2 = AddressOf Program.Mul
Console.WriteLine("f=Add, f({0}, {1}) = {2}", a, b, [Call](add, a, b))
Console.WriteLine("f=Mul, f({0}, {1}) = {2}", a, b, [Call](mul, a, b))
Console.WriteLine("f=Div, f({0}, {1}) = {2}", a, b, [Call](div, a, b))
End Sub
End Module

View file

@ -0,0 +1,29 @@
Module Program
' Uses the System generic delegate; see C# entry for details.
Function [Call](f As Func(Of Integer, Integer, Integer), a As Integer, b As Integer) As Integer
Return f(a, b)
End Function
Sub Main()
Dim a = 6
Dim b = 2
Console.WriteLine("f=Add, f({0}, {1}) = {2}", a, b, [Call](Function(x As Integer, y As Integer) x + y, a, b))
' With inferred parameter types:
Console.WriteLine("f=Mul, f({0}, {1}) = {2}", a, b, [Call](Function(x, y) x * y, a, b))
' The block syntax must be used in order to specify a return type. As there is no target type in this case, the parameter types must be explicitly specified. anon has an anonymous, compiler-generated type.
Dim anon = Function(x As Integer, y As Integer) As Integer
Return x \ y
End Function
' Parameters are contravariant and the return type is covariant. Note that this conversion is not valid CLR variance (which disallows boxing conversions) and so is compiled as an additional anonymous delegate that explicitly boxes the return value.
Dim example As Func(Of Integer, Integer, Object) = anon
' Dropped-return-type conversion.
Dim example2 As Action(Of Integer, Integer) = anon
Console.WriteLine("f=Div, f({0}, {1}) = {2}", a, b, [Call](anon, a, b))
End Sub
End Module