ValeLang / Vale

Compiler for the Vale programming language - http://vale.dev/
https://vale.dev/
Apache License 2.0
1.74k stars 53 forks source link

Kasper's syntax suggestions #592

Open probablykasper opened 1 year ago

probablykasper commented 1 year ago

These are my personal suggestions syntax suggestions

Enum variant access & type guards

This shows a few things:

  1. Type guards, like in TypeScript. After handling if the result is Err, we can safely access the Ok value.
  2. Enum variants are accessible like properties.

I think this is very simple and easy to understand.

let result = fetch(); // returns a Result enum
if result.Err {
  return result.Err
}
return result.Ok;

Potential equivalent shorthand:

let Ok(value) = fetch() else error {
  return result.Err
}
return value;

Methods, and how they work with UFCS

Define methods:

struct Spaceship {
  name: str;

  fn rename(spaceship &Spaceship, name str) {
    set spaceship.name = name;
  }
}

If you only import Spaceship, rename is not in scope. That means you cannot use rename() as an independent function, but you can use the rename method.

import space.Spaceship;
spaceship1 mut = space.Spaceship { name: "Apollo" };
spaceship1.rename("Saturn V");
rename(&spaceship1 "Saturn V"); // error (not in scope)

You can use UFCS for anything, it just needs to be in scope

import space.Spaceship.rename as change_name
change_name(spaceship1, "Soyuz");
spaceship1.change_name("Soyuz");
rename(spaceship1, "Soyuz"); // error (not in scope)

Wildcard imports bring every direct child into scope:

import space.*;
spaceship = Spaceship { name: "Apollo" };
spaceship.rename("Starship");
rename("Starship"); // error (not in scope)

Variable declaration

mut is specified alongside the type, and it's consistent between both variable and struct field declarations

// variables
x = value;
x mut = value;
set x = value;
// struct fields
x type;
x mut type;
set str.x = value;

List declaration

Declarations for the 3 different types of lists. Type arguments: <item_type, list_capacity>. [1, 2, 3] is a special shorthand for List, but there are no other special shorthands you need to think about.

list = [1, 2, 3];
list = List<int, 3>[1, 2, 3];

array = Array[1, 2, 3];
array = Array<int, 3>[1, 2, 3];

runtime_sized_array = DynamicArray[1, 2, 3];
runtime_sized_array = DynamicArray<int, 4>[1, 2, 3];

Discourage wildcard imports

Private-by-default struct fields

struct Spaceship {
  pub fuel u64;
}

Multi-statement loop conditions

while {
  ship = getShip(5);
  ship.canFly()
} do {
  ship.fly();
}

Shorter keywords

  1. exported -> pub
  2. import -> use
RefinedSoftwareLLC commented 11 months ago

Is this more readable and not conflicting with other syntax?

let Ok(value) = fetch() else {
  return Err(value)
}
return value;