Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kasper's syntax suggestions #592

Open
probablykasper opened this issue Apr 30, 2023 · 1 comment
Open

Kasper's syntax suggestions #592

probablykasper opened this issue Apr 30, 2023 · 1 comment

Comments

@probablykasper
Copy link

probablykasper commented Apr 30, 2023

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

  • 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 in package.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

  1. exported -> pub
  2. import -> use
@comods
Copy link

comods commented Jul 13, 2023

Is this more readable and not conflicting with other syntax?

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants