Back on April Fool’s day Anthony Green published a funny article “How ‘Roslyn# Finally Unshackled Visual Basic From The Tyranny of the Pretty-Lister”, showing VB.Net with lower case keywords, and how it made it look cooler. And more recently Mads Torgersen gave an “Early View of C# 7” demonstrating pattern matching, a feature that didn’t make it into C# 6, and syntax for tuples.
Taking some inspiration from this I thought I’d add some new extensions to my own language project, Fun Basic (try it free on the Windows Store). But Fun Basic already supports lower case keywords and tuples with pattern matching (which I implemented about 2 years ago), so instead over the last week I’ve added some different language features including cleaner aesthetics for BASIC and JSON literal support.
Hipster BASIC
People often complain about the verbosity of VB.Net, I see two parts to this:
- End statements – End If, End While, End Select, etc.
- Casing – Dim is the same length as var, but var just looks smaller
Fun Basic now lets you write `end` and it will infer the type of end for you:
while i < 10
i = i - 1
end
With the lower case keywords & simple end statement you could easily mistake this syntax for Ruby code.
JSON literals
VB.Net has XML literal support, which was a cool feature at the time, but these days XML is more associated with big enterprise, and all the hipsters are using JSON. With that in mind I’ve added JSON literal support to Fun BASIC:
name = "Phil"
age = 27
phil = {"name":name, "age":age}
This allows you to build up strings to send to web based services.
The syntax is also quite close to record syntax in ML, OCaml, F#, TypeScript etc.
Pattern matching
JSON literals are cool, but most of the time you’re using it the other way around, and consuming JSON from an API. For this I’ve added pattern matching over JSON literals.
For example say we want to get the temperature and humidity in London, we can use the Open Weather API, which spits back:
{"coord":{"lon":-0.13,"lat":51.51},
"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],
"base":"cmc stations",
"main":{"temp":282.515,"pressure":1026.96,"humidity":97,"temp_min":282.515,"temp_max":282.515,
"sea_level":1037.24,"grnd_level":1026.96},
"wind":{"speed":5.75,"deg":228.012},"rain":{"3h":0.6475},"clouds":{"all":92},"dt":1447504501,
"sys":{"message":0.003,"country":"GB","sunrise":1447485423,"sunset":1447517536},
"id":2643743,
"name":"London",
"cod":200}
With the new Fun Basic pattern matching syntax we can easily extract the values of temp and humidity:
london = "http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=2de143494c0b295cca9337e1e96b00e0"
weather = Web.Get(london)
{"main":{"temp":temp, "humidity":humidity}} = weather
We can also use the pattern matching in a select case statement:
function ageGroup(person)
select case person
case { "age": Is >= 18 }
return "Adult"
case else
return "Child"
end
end
sean = {"name":"sean", "age":9}
TextWindow.WriteLine(ageGroup(sean))
Summary
Both features were easy to implement (JSON literals took me the morning), and feel quite natural in the setting of BASIC, you can try them out now in Fun Basic, who knows one day we might see them in mainstream enterprise languages