Metacircular thoughts

October 20, 2007

Generics in VB 2005

Filed under: The Dark Side — metacircular @ 5:19 pm

So, generics in .NET 2.0 let you make functions and classes generic over types.

They also let you maintain type safety by constraining the type, e.g. making it implements a certain interface.

For example, you could define a general function to find the minimum element in a list. In VB, the data structure of interest is System.Collections.Generic.List(Of T). Now, we have to be able to compare on these objects, and the .NET interface for this is IComparable. So, we can define the function as follows:


Imports System.Collections.Generic

Public Function FindMin(Of T As IComparable(Of T))(ByVal data As List(Of T)) As T
   Dim minval As T = data(0)

    For Each value As T In data
        If value.CompareTo(minval) < 0 Then
            minval = value
        End If
    Next

   Return minval
End Function

The As IComparable(Of T) bit constraints T to implement IComparable. Visual Studio complains with highlighted source code until we add this constraint about the call to CompareTo; it statically determines the type safety of what we've produced with respect to constraints and interfaces and tells us promptly, which is nice.

Then you can test this out as follows:


Dim numdata As New List(Of Integer)
numdata.Add(1) : numdata.Add(3) : numdata.Add(-5) : numdata.Add(4)
Console.WriteLine(FindMin(numdata))

Dim stringdata As New List(Of String)
stringdata.Add("first") : stringdata.Add("second") : stringdata.Add("last")
Console.WriteLine(FindMin(stringdata))

This outputs -5 and "first", respectively, as you'd expect.

Pretty straightforward, really.

Whoda thought a BASIC dialect could be a respectable albeit underwhelming programming language?

October 16, 2007

Notes on learning the .NET framework

Filed under: Language pissing matches, Life, The Dark Side — metacircular @ 11:23 pm

What’s good, peoples? It’s been a while since I rapped at ya.

Because of my job, I’ve been learning VB.NET and the .NET framework.

I have yet to find anything really retarded. These are some notes mainly for my own purposes.

  • And and Or still don’t shortcircuit. You have to use AndAlso and OrElse. Lame!
  • Boxing and unboxing (converting between a primitive type and an object) can definitely affect performance if called many times. This isn’t surprising, but it bears remembering and repeating. Consider the following code snippet:
    
    Option Strict On
    
    Module BoxingTest
        Public Sub NoBoxing(ByVal i As Integer)
            If i = 5 Then
                Console.WriteLine("omg!")
            End If
        End Sub
    
        Public Sub DoBoxing(ByVal i As Object)
            If CType(i, Integer) = 5 Then
                Console.WriteLine("omg!")
            End If
        End Sub
    
        Sub Main()
            Dim n As Integer = 300000000
            Console.WriteLine(Now)
            For i As Integer = 1 To n
                NoBoxing(i)
            Next
            Console.WriteLine(Now)
    
            Console.WriteLine(Now)
            For i As Integer = 1 To n
                DoBoxing(i)
            Next
            Console.WriteLine(Now)
        End Sub
    End Module
    

    On my machine, the boxing function takes about 10x longer than the one that's directly with value types. Something to keep in mind if you're working on a performance-sensitive application with tight inner loops.

  • Generics and the other collections are nice. Generics seem to be really fast, faster than normal collections?
  • The specialty collections, such as BitArray are efficient and nice (I can twiddle 10 million bits in under a second on a 2.8 Ghz machine).
  • Visual Basic 2005 is C# in clown shoes.

Blog at WordPress.com.