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.
Hello, world
Section titled “Hello, world”Module Program Sub Main() Console.WriteLine("Hello, world!") End SubEnd Moduledef main: Void puts "Hello, world!"endVariables
Section titled “Variables”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.
Dim name = "Ada"Dim count As Integer = 0Const MaxRetries As Integer = 3name = "Ada"count: Int = 0MAX_RETRIES = 3Amethyst is case-sensitive. Name, name and NAME are three different things, and
one of them is a constant.
Sub and Function
Section titled “Sub and Function”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.
Function Square(x As Integer) As Integer Return x * xEnd Function
Sub Log(message As String) Console.WriteLine(message)End Subdef square(x: Int): Int x * xend
def log(message: String) puts messageendOptional and named arguments
Section titled “Optional and named arguments”Optional parameters become keyword parameters, marked with ::, and they must be
passed by name. := loses its =.
Function Create(name As String, Optional role As String = "user") As UserEnd Function
Create("Ada", role:="admin")def create(name: String, role:: String = "user"): Userend
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.
Classes
Section titled “Classes”Property becomes property, ReadOnly Property becomes getter, Sub New becomes
initialize, and Shared members are defined on self.
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 FunctionEnd Class
Dim user As New User("Ada", 36)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) endend
user = User.new("Ada", 36)Nothing becomes nil
Section titled “Nothing becomes nil”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
&..
Dim user = FindUser(id)If user IsNot Nothing Then Console.WriteLine(user.Name)End IfDim city = user?.Address?.Cityuser = find_user(id) # User?if user puts user.name # narrowed to Userendcity = user&.address&.city # String?Operators
Section titled “Operators”| Visual Basic | Amethyst |
|---|---|
= (comparison) |
== |
<> |
!= |
AndAlso / OrElse |
&& / || |
Not |
! |
& (string concatenation) |
+, or interpolation: "Hello, #{name}" |
Mod |
% |
^ |
** |
Select Case
Section titled “Select Case”Select Case becomes case/when, and it’s an expression, so it returns a value.
Select Case score Case 90 To 100 grade = "A" Case 80 To 89 grade = "B" Case Else grade = "F"End Selectgrade = case score when 90..100 then "A" when 80..89 then "B" else "F" endErrors
Section titled “Errors”Try/Catch/Finally becomes begin/rescue/ensure, with the same .NET
exceptions.
Try Parse(path)Catch ex As FileNotFoundException Log(ex.Message)Finally Cleanup()End Trybegin parse(path)rescue FileNotFoundException => ex log ex.messageensure cleanupendThere’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.
Everything else
Section titled “Everything else”| 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 |