When Optional isn’t so Optional!

One of the features of VB that has been the point of many a discussion is the use of Optional parameters. Optional parameters allow you to specify a parameter as being just that, you are stating that it is optional and providing a default value.  This quite often gets around defining numerous method overloads that take different combinations of parameters.  The issue with this is that Optional parameters are not supported by all languages despite being in the .NET Framework specs.  

Personally I like to use optional parameters but there are some limitations that I would love to see removed:

1) Remove the requirement that the default value be a constant value.  In my opinion optional parameters are really only a half-baked concept if you can’t use dynamically created default values.

2) Remove the (imho) stupid limitation around generic parameters:

image

This unfortunately comes down to what nothing, or null, means for a value type.  imho this should always be an effectively empty/zero-initialized value based on the structure of the value type (eg for a point it would be a point with values 0,0). With the constraint how it is currently I have to limit my methods to be class applicable only – even if the parameters is optional!!!!

Leave a comment