The Vale Programming Language

Patterns are a convenient, concise syntax for receiving some incoming data and doing some common things with it. Keep reading to see how.

Patterns Make Locals

We use patterns to make new locals.

Everything to the left of an = is a pattern.

The pattern on the left receives incoming data from the expression on the right.

To the left of this = is a pattern that stores the incoming data into a local named a.

vale
exported func main() {
a = 3 + 4;
}

Destructuring

If we're receiving a struct, we can destructure the incoming struct and put its members into locals.

In this example, we're taking the parts of the Vec3 and assigning them into locals a, b, c.

vale
import stdlib.*;

struct Vec3 imm {
x int;
y int;
z int;
}
func makeVec() Vec3 { Vec3(3, 4, 5) }

exported func main() {
// Without destructure pattern:
tempVec = makeVec();
a = tempVec.x;
b = tempVec.y;
c = tempVec.z;

// Equivalent, using destructure:
Vec3[d, e, f] = makeVec();

// Equivalent; can leave off the type:
[g, h, i] = makeVec();

println("a: " + a);
println("d: " + d);
println("g: " + g);

}
stdout
a: 3
d: 3
g: 3

Planned Features

These are planned features for Vale. See the roadmap for plans!

Parameters

Every parameter could be a pattern.

Here, we're using destructuring for this function's parameters.

vale
// Using above Vec3

// Without destructuring:
func refuelA(
vec Vec3,
len int)
Vec3 {
Vec3(
vec.x * len,
vec.y * len,
vec.z * len)

}


// With destructuring:
func refuelB(
Vec3[x, y, z],
len int)
Vec3 {
Vec3(x * len, y * len, z * len)
}

Match Statement

We can use a pattern to check if the incoming data is a certain value.

Here, we're using a match statement to check what the user entered.

In patterns, _ matches any incoming data and discards it.

import stdlib.*;

exported func main() {
  match inputInt() {
    1 { println("One!"); }
    2 { println("Two!"); }
    _ { println("Other!"); }
  }
}

We can even check an incoming interface, to see what substruct it is.

We can even destructure it at the same time!

import stdlib.*;

interface ISpaceship { }

struct Firefly { name str; }
impl ISpaceship for Firefly;

struct Raza { name str; fuel int; }
impl ISpaceship for Raza;

exported func main() {
  serenity = Firefly("Serenity", 2);

  match serenity {
    Firefly(name) {
      println("Firefly name " + name);
    }
    Raza(name, fuel) {
      println("Raza name " + name);
    }
  }
}
stdout
Firefly name Serenity

Next: Regions