Generics with VB.NET and JScript.NET
Reading one of Sam Gentile's old weblog entries regarding late binding brought to mind a conversation I had recently with a VB.NET developer.
The developer was concerned that C# was soon to have "generics" - classes and methods that work uniformly on values of different types. He wasn't too sure why he should be concerned, but it was going to be something that VB.NET didn't have.
My response was that both JScript.NET and VB.NET already support "generics". It's possible to achieve polymorphic behavior through late binding. Granted, this does not have the compile time or performance advantage of static type-checking as with the C# implementation, but offers other benefits which are especially suited to a scripting type environment like that provided by Alintex Script .NET
An example of a script which provides polymorphic behavior through JScript.NET in Alintex Script is the following:-
print ( Sqr(9) );
function Sqr(x) { return x*x; }
One can pass parameters of different types to the Sqr function. Pass a type that is inapplicable to the * operator and JScript.NET doesn't throw an exception - it returns "NaN" - not a number. JScript.NET is especially concise - those two lines are a complete script.
What about VB.NET?
One can create a similar "Sqr" function, but to make it interesting I thought I might show how one can achieve apparent C# generic behavior via VB.NET with Alintex Script.
To do so, I would create two script files, named (for example) maths.vb and generics.csx. The two script files would contain the following code:-
'maths.vb
public Module Maths
function Sqr(x)
return x * x
end function
End Module// generic.csx
#region Script
print( Maths.Sqr(99) );
#endregion
One would run the script by typing the following:-
> axscript generic.csx maths.vb
The answer 81 would be displayed. The value 9 could then be substituted for other types.
A benefit of this behavior is that I can write an extremely concise script in VB.NET like the following, which displays the list of IP addresses on my current machine...
imports System.Net
#region "Script"
addressList = Dns.GetHostByName(Dns.GetHostName()).AddressList
for each address in addressList
print(address)
next address
#end region