Open retailcoder opened 5 years ago
VBA doesn't have an explicit cast operator: a new @DirectCast
annotation could be used to decorate assignments that would otherwise trip implicit object type conversions (linking #3580):
Dim a As Foo
Set a = New Foo
Dim b As Bar
'@DirectCast
Set b = a
My only issue with enforcing early binding is the problem it causes users with different versions and therefore requiring different references… I’d rather develop in early bound mode and then go late-bound when deploying. Maybe that is what you are suggesting and I am missing the point (which is usually the case…
@SmileyFtW the version-specific issues of early binding are real, but also wildly overstated. Scripting runtime, for one, has no reason to be late-bound: every single Windows machine built this century is running the same version. Ditto with any XML library, or regex, and several others. That said we can't hook the compiler (that I know of) and enforce it at compile-time; best we can do is trigger inspections, and offer quickfixes where appropriate. The vast majority of VBA code uses only standard libraries, and "late binding" has nothing to do with library references (another widely spread piece of misinformation).
This Range
call is late-bound:
Worksheets("Sheet1").Range("A1") = 42
It's late bound because Sheets
returns Object
. The fix is to cast that object to an early-bound interface:
Dim sh As Worksheet
Set sh = Worksheets("Sheet1")
And now a Range
call against sh
is compile-time validated. That is mostly what this idea is all about. But yes, late-bound library references would also be "forbidden"... and then, moving such library-specific calls into their own module (without @OptionStrict
specified) would only make the code more modular and robust overall.
This is why I love this RD community… I learn something virtually every time I read a post, and ALWAYS when I post a reply. Thanks to you and all who have moved me way beyond where I was a few months ago.
Justification
VBA is very forgiving in terms of implicit type conversions. Late-bound member calls move glaring compile-time errors from compile-time to run-time; variables don't even need a declared type! VB.NET addressed these loose ends with
Option Strict
, and I think we have everything we need to make VBA do the same.Description
Microsoft Docs describes
Option Strict
as follows:In a VBA context, "
Object
" means "Object
orVariant
".Let's introduce a new
@OptionStrict
annotation, aMissingOptionStrictInspection
"Rubberduck Opportunity" inspection that finds modules without it, and quickfix to add it in this module... and then another error-levelOptionStrictInspection
that point to all violations.Bonus: a checkbox setting to "use 'Option Strict' everywhere", and have the annotation added automatically to every new (& existing?) module.
Additional context These annotations should ideally all be valid (with or without the parentheses):
...but it's completely fine to start with just a single
@OptionStrict
annotation, and have it support a parameter later: if the annotation is there, it's on, if not, it's off.When
'@OptionStrict
is on, the following inspections are in effect:Variant
problem)Variant
orObject
.VariableTypeNotDeclaredInspection
for this... locates all declarations of variables/parameters/fields without anAs
clause. That way'@OptionStrict
would still make it issue a result even ifVariableTypeNotDeclaratedInspection
is disabled.Note that explicit
Variant
andObject
declarations are legal with'@OptionStrict
: what's not allowed is to make a member call against it without first casting it to an early-bound interface.The advantages are:
@OptionStrict
in a module in order to run in that module.The
OptionStrict
inspections should be public nested types in an all-encompassingOptionStrictInspection
, so that'@Ignore OptionStrict
works for allOptionStrict
results. Disabling theOptionStrictInspection
could fire anOptionStrictDisabledInspection
result that flags'@OptionStrict
annotations when theOptionStrictInspection
is disabled, warning about the option not being enacted. Or (perhaps simpler, and less... recursive "what it that one is disabled?"), we special-caseOptionStrictInspection
and disable changing/configuring that inspection's severity level (Error
).Wiki should also be updated with a table of Widening Conversions adapted to VBA data types.