Vale
Vale copied to clipboard
Kasper's syntax suggestions
These are my personal suggestions syntax suggestions
Enum variant access & type guards
This shows a few things:
- Type guards, like in TypeScript. After handling if the result is
Err
, we can safely access theOk
value. - 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
- Explicit imports are much easier to read/navigate outside of a code editor, when there's no
Go to definition
(like on GitHub) - Officially discourage wildcard imports, but don't show warnings for them
- Show warnings when there are wildcard imports if the code is publishable to a package registry (aka
private: true
isn't specified inpackage.json
-equivalent)
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
-
exported
->pub
-
import
->use
Is this more readable and not conflicting with other syntax?
let Ok(value) = fetch() else {
return Err(value)
}
return value;