Post

Insane Idea #1

What if it were possible to do the following in VB?

10 PRINT "HELLO WORLD!"

Right now, to accomplish a similar result in VB…

1
2
3
4
5
6
7
Imports System

Module Program
  Sub Main(args As String())
     Console.WriteLine("HELLO WORLD!")
  End Sub
End Module

or more accurately…

1
2
3
4
5
6
7
8
Imports System

Module Program
  Sub Main(args As String())
10:
     Console.WriteLine("HELLO WORLD!")
  End Sub
End Module

With the concept of Top-Level Code, the theory would be that it would look something like:

1
2
3
4
Imports System

10:
  Console.WriteLine("HELLO WORLD!")

The reason why I include line numbers in the first example is to think about the following example:

10 PRINT "HELLO WORLD!"
20 X = X + 1
30 IF X > 10 THEN GOTO 50
40 GOTO 10
50 END

which would (theoretically, if Top-Level Code existed in VB) to:

1
2
3
4
5
6
7
8
9
10
11
12
Imports System

Dim x As Integer

10:
  Console.WriteLine("HELLO WORLD!")
  x=x+1
  If x > 10 Then GoTo 50
  GoTo 10

50:
  End

or, depending on if you had certain Project options set appropriately…

1
2
3
4
5
6
7
8
10:
  Console.WriteLine("HELLO WORLD!")
  x=x+1
  If x > 10 Then GoTo 50
  GoTo 10

50:
  End

For a really insane option, what about the following?

1
? "HELLO WORLD!"

What if, indeed…

AND… what if… and I do ask this in all seriousness… this were possible WITHOUT changing the VB compiler? It might just indeed be possible to support this with the tools that currently exist where (once available) all you would have to do is simply add a nuget reference.

Which then leads me to ask what would PRINT (or ?) do for different project types? In a Console application it would output text to the, well, Console. In a WinForms application, would it be equivalent to a Debug.Print or for “logging”? In an ASP.NET type application, would it be for sending content to the browser (HTML)?

I’ve also been thinking about whether or not this same mechanism could be extended to the point where VB Top-Level Code could be written in a C# project (instead of C# Top-Level Code)…

DISCLAIMER: This may be something that requires a team of people to participate in to ultimately accomplish.

This post is licensed under CC BY 4.0 by the author.