LanguagesArchitecture

Version 0.2 is now out of beta, and officially released!

As we all know, Vale is a programming language that aims to be fast, safe, and easy to use. It's a challenging goal, and version 0.2 lays a solid foundation for the next steps in the endeavor.

This version has been prioritizing ease of use, enabling much larger Vale projects. We can now comfortably create large programs such as this roguelike game. With this version, Vale has graduated from a prototype to early alpha status.

You can find the version 0.2 binaries here.

Try it out, and if you like what you see, consider sponsoring us on GitHub! This is a free project for the good of all, and with your help, we can continue working on it for longer. 0

Below are the new features in 0.2, enjoy!

Higher RAII

This release enables Higher RAII, a form of linear typing that helps the compiler enforce that we "never forget to call that function".

For a real-world use case, check out the recent article Higher RAII, the pattern that saved me a vital 5 hours in the 7DRL Challenge.

To use Higher RAII, just add a #!DeriveStructDrop above your struct or #!DeriveInterfaceDrop above your interface, to prevent it from automatically defining a drop function (the ! means "don't").

Instead of having an implicitly-called drop function, this example has a destroyShip function that takes a boolean parameter and returns an integer.

Since there is no drop function for the struct, Vale will never automatically destroy the object. The user must explicitly call the destroyShip function.

This pattern can be used to enforce that we don't forget to call a certain function (like destroyShip) at some point in the future.

vale
#!DeriveStructDrop
struct Spaceship {
name str;
fuel int;
}

exported func main() {
ship = Spaceship("Serenity", 2);
// ship is an owning reference

println(ship.name);

fuel = (ship).destroyShip(true);
println("Fuel was {fuel}.")

}


func destroyShip(
s Spaceship,
print bool)

int {
[name, fuel] = s; // Deallocates ship
if print {
println("Destroyed {name}!");
}

return fuel;

}
stdout
Destroyed Serenity!
Fuel was 2.

Higher RAII can be also be used for more powerful type-state programming 1 than any other imperative language. Try it out!

Side Notes
(interesting tangential thoughts)
0

We're particularly excited to prototype the region borrow checker, which could help Vale approach C++'s speed by getting rid of the vast majority of our memory safety overhead, and enable concurrency features like described in Seamless, Fearless, Structured Concurrency.

We've been looking forward to this version for a long time!

1

Type-state programming is a way to use the compiler to ensure that specific functions are only callable when the object is in a certain state. This is all tracked via the type system, with zero run-time overhead.

Concept Functions

Vale now supports concept functions, a way to specify that certain functions must exist for given generic parameters, without making them implement any traits.

For example, List's clone function can require that there exists a clone function for its elements:

vale
func clone<T>(self &List<T>) List<T>
where func clone(&T)T
{
result = List<T>();
foreach x in self {
result.add(clone(x));
}

return result;

}

This is often much easier than what we normally see in mainstream languages, which is to require an explicit implementation for the type.

Read more at Concept Functions!

Const Generics

We're happy to announce that Vale now has "const generics", where generic parameters can contain not only types (like the T in List<T>) but also integers, booleans, and other types.

Now, instead of repeating classes...

vale
struct Vec2<T> {
elements [#2]T; // 2-element array of T
}
struct Vec3<T> {
elements [#3]T; // 3-element array of T
}
struct Vec4<T> {
elements [#4]T; // 4-element array of T
}
exported func main() {
v = Vec3<int>([#](3, 4, 5));
...
}

...we can have one:

vale
struct Vec<N Int, T> {
elements [#N]T;
}
exported func main() {
v = Vec<3, int>([#](3, 4, 5));
...
}

We also now have the compile-time spread operator .. for structs, which enabled us to implement tuples in the standard library, instead of in the compiler.

We have some interesting things planned for const generics, see Const Generics and the Compile-Time Spread Operator for more!

Removing Let and Let Mut

The 0.2 release removes the let and let mut keywords, making our syntax cleaner and more readable.

Before:

func main() {
  let a = 3;
  let b = 3;
  let c = 3;
  let mut d = 3;
  d = 7;
  println(d);
}
stdout
7

After:

vale
func main() {
a = 3;
b = 3;
c = 3;
d = 3;
set d = 7;
println(d);

}
stdout
7

Read more at On Removing Let and Let Mut!

Faster Compile Times

This version of the compiler is 3x as fast as the previous version, after we did some optimizations:

  • Memoized all of our .hashCode() calls, and used interning to speed up .equals() calls.
  • Wrote a newly optimized generics solver, which operates on primitives instead of heap-allocated classes.
  • Migrated from Scala parser combinators to a hand-written recursive descent parser.

We also updated to LLVM 13 under the hood.

Modules

Vale has a new take on modules, which is as easy to use as Java's packages while providing the flexibility of Rust's crates.

Check out the Modules Guide to see them!

We make heavy use of our module system in our new standard library, too.

Downcasting

We can now downcast interfaces to a specified struct, using the as function.

In this example, we're downcasting a Ship interface reference to a FireflyShip struct reference.

Downcasting will always return a Result<T, E> where T is the struct type, and E is the interface type. In this example, it returns a Result<FireflyShip, Ship>.

It will either be an Ok containing a FireflyShip, or if the ship wasn't actually a FireflyShip it will contain an Err with the original Ship.

Here, we're calling .expect() because we know it is indeed a FireflyShip.

vale
interface Ship { }

struct FireflyShip {
name str;
}
impl Ship for FireflyShip;

exported func main() {
// Implicitly upcast to Ship
ship Ship = FireflyShip("Serenity");

// Can downcast with as:
fireflyShip =
ship.as<FireflyShip>().expect()
;

println(fireflyShip.name);

}
stdout
Serenity

Foreach and Break

Vale now supports foreach loops, and the break statement as well.

See the Collections Guide for more!

vale
exported func main() {
l = List<int>().add(1).add(3).add(7);
foreach [i, x] in l.entries() {
println(i + ": " + x);
if i == 1 {
break;
}

}

}
stdout
0: 1
1: 3

FFI

When we use the exported keyword on a function, Vale will generate headers so that C can call that function. We can also call C functions by declaring an extern function.

See Externs and Exports in the guide for more!

Here, main is calling out into an external C function that reads an int from the keyboard.

The extern func readInt() int; is telling Vale that the C code will declare a function named readInt.

vale
exported func main() {
i = readInt();
println("User entered: " + i);

}

extern func readInt() int;
c
#include <stdint.h>
#include <stdio.h>
#include "mvtest/readInt.h"
extern ValeInt mvtest_readInt() {
  int64_t x = 0;
  scanf("%ld", &x);
  return x;
}
stdin
42
stdout
User entered: 42

Vale's approach to FFI is unusual. Vale regards its own objects as in a separate "region" of memory than C's objects, and doesn't directly expose Vale objects to C. 2

This separation enables some pretty interesting upcoming features. "Fearless FFI" will allow us to be certain we're not accidentally corrupting our Vale objects from C. "Perfect Replayability" will then rely on that memory-safety and determinism to allow us to easily reproduce any bug from a previous run. 3

What's next for 0.3

Now that we have a usable and solid language to build on, version 0.3 will be focused on prototyping our flagship feature, the region borrow checker.

Also keep an eye out for our next series of articles, which paint a more holistic picture of how Vale will combine generational references, "life cells", and the region borrow checker to provide maximum speed, memory safety, while still remaining easy to use.

Here's to a great year, and a great version!

Thanks for reading, we hope you enjoyed this article! And if you're impressed with our track record and believe in the direction we're heading, please consider sponsoring us on github:

With your support, we can work on this full-time, and bring speed and safety to more programmers than ever before!

2

This may be relaxed in the future, using unsafe blocks.

3

Stay tuned for the next two articles, on Fearless FFI and Perfect Replayability. If you want early access, feel free to swing by the discord server and ask!

We're looking for sponsors!

With your help, we can launch a language with speed, safety, flexibility, and ease of use.

We’re a very small team of passionate individuals, working on this on our own and not backed by any corporation.

If you want to support our work, please consider sponsoring us on GitHub!

Those who sponsor us also get extra benefits, including:

  • Early access to all of our articles!
  • A sneak peek at some of our more ambitious designs, such as memory-safe allocators based on algebraic effects, an async/await/goroutine hybrid that works without data coloring or function coloring, and more.
  • Your name on the vale.dev home page!

With enough sponsorship, we can:

  • Start a a 501(c)(3) non-profit organization to hold ownership of Vale. 4
  • Buy the necessary computers to support more architectures.
  • Work on this full-time.
  • Make Vale into a production-ready language, and push it into the mainstream!

We have a strong track record, and during this quest we've discovered and implemented a lot of completely new techniques:

  • The Linear-Aliasing Model that lets us use linear types where we need speed, and generational references where we need the flexibility of shared mutability.
  • Region Borrowing, which makes it easier to write efficient code by composing shared mutability with the ability to temporarily freeze data.
  • Higher RAII, where the language adds logic safety by enforcing that we eventually perform a specific future operation.
  • Perfect Replayability makes debugging race conditions obsolete by recording all inputs and replaying execution exactly.

These have been successfully prototyped. With your sponsorship we can polish them, integrate them, and bring these techniques into the mainstream. 5

Our next steps are focused on making Vale more user-friendly by:

  1. Finalizing the compiler's error messages and improving compile speeds.
  2. Polishing interop with other languages.
  3. Growing the standard library and ecosystem!

We aim to combine and add to the benefits of our favorite languages:

We need your help to make this happen!

If you're impressed by our track record and believe in the direction we're heading, please consider sponsoring us:

If you have any questions, always feel free to reach out via email, twitter, discord, or the subreddit. Cheers!

4

Tentatively named the Vale Software Foundation.

5

Generational references, the linear-aliasing model, and higher RAII are all complete, and region borrowing, fearless FFI, and perfect replayability have been successfully prototyped. Be sure to check out the experimental version of the compiler!