Lurgy Lambda Leaves Lingering Lethargy

I’ve mentioned before that I have a distaste for lambda functions haven’t I? Allow me to now indulge in a little confirmation bias.

The scene:

I need every single ounce of speed in the heart of a really complex algorithm, and the complexity is now interfering with my grokking of a nasty but subtle bug, I’m embracing lambda functions to inject some extra semantics at little extra runtime cost.

The story:

For the entire morning, I’ve watched in frustration as the VB.NET code compiles and runs, but never return a ‘True’ value for a boolean lambda function. Yet, if I lift the boolean expression back out of the function and have the expression evaluate directly, it works fine.

Want to know what I did? I got the defining ‘Func’ signature wrong. I wrote this:

  Dim IsLastSeasonInRun As Func(Of Boolean, Integer, Integer) =
  Function(year As Integer, season As Integer)
    Return (year = params.YearsPerRun - 1) AndAlso (season = params.NoOfSeasons - 1)
  End Function

Instead of this (note the location of the ‘Boolean’ on the top line between both):

  Dim IsLastSeasonInRun As Func(Of Integer, Integer, Boolean) =
  Function(year As Integer, season As Integer)
    Return (year = params.YearsPerRun - 1) AndAlso (season = params.NoOfSeasons - 1)
  End Function

Am I miffed? Yes. Some at myself. Some at MSDN’s coverage of lambdas.

For myself, I learned that I could have avoided this problem by setting ‘Option Strict On’ in the source. I’d toggled it off, because I’m dealing with an OLE spreadsheet where every single number turns out to be a Double when it hits my code. I turned it off to get around the immense amount of explicit “noise” typecasting between doubles and integers the compiler was forcing me into (let me worry about crossing the Integer/Double boundary please).

Then again, examples from MSDN themselves don’t tackle what I want to do with Lambda functions explicitly.  You gotta go deep to find the fiddly bits not mentioned in the MSDN lanbda discussion that light the blowtorch. Specifically, it all hinges on that ‘Func‘ call:

The Func type is also new with Visual Basic 2008; it is essentially a delegate that has the return type specified as the last generic parameter and allows up to four arguments to be supplied as the leading generic parameters (there are actually several Func delegates, each of which accepts a specific number of parameters).

Last generic parameter… LAST… It burns… it burns bad… because this is a strongly typed language, and a compile-time check on the return type isn’t done on Lambda functions unless I flag ‘Option Strict On’ to catch my goof (invisible, because I was working on the assumption that it compiles, which means the signature is good).

So… remember .NET folks, if you’ve flagged ‘Option Strict’ off, lambda functions can:

  • compile when they shouldn’t
  • fail silently for you when they run

Nice. Like being affectionately licked to death by a minion of Cthulhu.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s