Skip to content

Amethyst for Visual Basic developers

Good news: you already type End. Amethyst just asks for less of it. End Sub, End Function, End If and End Class all become a lowercase end.

You also know the platform. Amethyst runs on .NET, uses the same class library, and compiles to the same kind of assemblies, so VB and Amethyst projects can reference each other. In 2020, Microsoft said it doesn’t plan to evolve Visual Basic as a language. Amethyst is still evolving, mostly because nobody has told us to stop.

Visual Basic
Module Program
Sub Main()
Console.WriteLine("Hello, world!")
End Sub
End Module
Amethyst
def main: Void
puts "Hello, world!"
end

There’s no Dim. Assignment declares a variable, with its type inferred, as it would be with Option Infer On. Names in ALL_CAPS are constants.

Visual Basic
Dim name = "Ada"
Dim count As Integer = 0
Const MaxRetries As Integer = 3
Amethyst
name = "Ada"
count: Int = 0
MAX_RETRIES = 3

Amethyst is case-sensitive. Name, name and NAME are three different things, and one of them is a constant.

Both become def. The difference is whether you write a return type: a public method without one returns nil and compiles to void, like a Sub. The last expression is the return value, so Return is optional.

Visual Basic
Function Square(x As Integer) As Integer
Return x * x
End Function
Sub Log(message As String)
Console.WriteLine(message)
End Sub
Amethyst
def square(x: Int): Int
x * x
end
def log(message: String)
puts message
end

Optional parameters become keyword parameters, marked with ::, and they must be passed by name. := loses its =.

Visual Basic
Function Create(name As String,
Optional role As String = "user") As User
End Function
Create("Ada", role:="admin")
Amethyst
def create(name: String, role:: String = "user"): User
end
create("Ada", role: "admin")

Amethyst methods can’t declare ByRef parameters. Return a tuple instead. When you call a .NET method that has one, pass ref variable.

Property becomes property, ReadOnly Property becomes getter, Sub New becomes initialize, and Shared members are defined on self.

Visual Basic
Public Class User
Public Property Name As String
Public ReadOnly Property Age As Integer
Public Sub New(name As String, age As Integer)
Me.Name = name
Me.Age = age
End Sub
Public Shared Function Guest() As User
Return New User("Guest", 0)
End Function
End Class
Dim user As New User("Ada", 36)
Amethyst
class User
property name: String
getter age: Int
def initialize(name: String, age: Int)
@name = name
@age = age
end
def self.guest: User
User.new("Guest", 0)
end
end
user = User.new("Ada", 36)

Types are non-nullable by default. String? can hold nil, String can’t, and the compiler won’t let you use a nullable value until you’ve checked it. ?. is spelled &..

Visual Basic
Dim user = FindUser(id)
If user IsNot Nothing Then
Console.WriteLine(user.Name)
End If
Dim city = user?.Address?.City
Amethyst
user = find_user(id) # User?
if user
puts user.name # narrowed to User
end
city = user&.address&.city # String?
Visual Basic Amethyst
= (comparison) ==
<> !=
AndAlso / OrElse && / ||
Not !
& (string concatenation) +, or interpolation: "Hello, #{name}"
Mod %
^ **

Select Case becomes case/when, and it’s an expression, so it returns a value.

Visual Basic
Select Case score
Case 90 To 100
grade = "A"
Case 80 To 89
grade = "B"
Case Else
grade = "F"
End Select
Amethyst
grade = case score
when 90..100 then "A"
when 80..89 then "B"
else "F"
end

Try/Catch/Finally becomes begin/rescue/ensure, with the same .NET exceptions.

Visual Basic
Try
Parse(path)
Catch ex As FileNotFoundException
Log(ex.Message)
Finally
Cleanup()
End Try
Amethyst
begin
parse(path)
rescue FileNotFoundException => ex
log ex.message
ensure
cleanup
end

There’s no On Error Resume Next. The closest thing is inline rescue, which at least makes you say what happens instead: value = risky_method rescue fallback.

Visual Basic Amethyst
Imports System.Text use System::Text
Namespace MyApp.Models namespace MyApp::Models
Inherits User class Admin < User
Implements IShape implements Shape, where Shape is a protocol
Module Top-level methods, or a mixin with extend self
Me / MyBase self / super
List(Of String) Array[String], which is a List(Of String)
<Serializable> @[Serializable]
Function(x) x * 2 { |x| x * 2 } or ->(x: Int) { x * 2 }
Async Function … As Task(Of String) async def …: String
Using reader As New StreamReader(path) StreamReader.new(path).use do |reader| … end, on anything Disposable
For Each name In names for name in names, or names.each { |name| … }
' comment and REM # comment. REM is not coming back
_ line continuation Not needed inside brackets, where newlines are ignored
Option Strict On Always on, with no switch