Core3: Dynamic pseudo-type (scoped late binding)

[This post is part of a series, "wish-list for future versions of VB"]


IDEA: Scoped late binding through a dynamic pseudo-type. Add a new pseudo-type called either Dynamic or Variant. It would be exactly the same type as Object, but it would suppress errors/warnings about late-binding in any expression that it’s used. Also, when we import COM functions that currently return Object, we’d import them instead as returning Dynamic. We’d also use the same DynamicAttribute that C# uses when reading or writing metadata.

SCENARIO: You want to have late-binding in your code, but want to leave Option Strict On. Currently there’s no way to do this. The dynamic pseudo-type would allow it, as shown below. In the following we’ve written “As Dynamic” explicitly to show what type we’re getting back (and it suppresses errors/warnings on “cell.ColumnWidth”), but if we omitted it then "cell" would still be inferred as Dynamic.

    Option Strict On

    Dim excel = New Microsoft.Office.Interop.Excel.Application

    excel.Visible = True : excel.Workbooks.Add() : excel.Worksheets.Add()

    Dim cell As Dynamic = excel.Cells(1, 1)

    cell.ColumnWidth = 15


Note that this wouldn’t quite be the same as the C# “dynamic” pseudo-type. The C# rules say “If dynamic is involved anywhere then do a late-bound call”. But the existing VB rules say “If binding failed, and it failed due to narrowings from Object, then do a late-bound call”. We would stick with the VB rules for our dynamic pseudo-type. For instance,

        Dim d As Dynamic = "hello"

        Console.WriteLine(d) ' early-bound call in VB


        dynamic d = "hello";

        Console.WriteLine(d); // late-bound call in C#

ALTERNATIVE IDEA: Instead of using a pseudo-type to suppress errors/warnings about late-binding, we could instead use different operators:

        cell~.ColumnWidth = 15

        ' The "~." operator is like "." but it suppresses
' warnings/errors about late-binding


An entirely different way to achieve “scoped late-binding” is to add #Pragma directives to VB which could suppress arbitrary errors/warnings in any chunk of code. I’ll take about this later (“Req33: Pragmas, e.g. to suppress warnings”).



Provisional evaluation from VB team: VB has always had its own form late-binding via Object. While it’s a shame that you can’t scope your late-binding smaller than file granularity, this doesn’t seem a big enough problem to justify a second form of late-binding.