Hello all, i'm trying to figure out a language syntax and I keep going in circles, maybe some opinions from other devs may help.
For a given programming language what is the most efficient & cleanest way of describing a variable, type, etc?
Here is a list of ones I have considered. (psudocode)
C style (structs and return type prefix), pros: simple, really succinct, cons: annoying lexing because return type is ambiguous.
struct Vec3 {
f32 v[3];
}
struct Player {
char *username;
Vec3 pos;
Color appearance;
}
Player[] login(Player p, char *password) {
...
}
Player me("me", Vec3(0.0,0.0,0.0), Color(255, 0, 0));
login(me, password);
Rust style (function prefix, arrow return type), pros: separation of data and implementation makes composition easier. Cons: not very intuitive for learners.
struct Player {
username: str,
pos: Vec3,
appearance: Color,
}
impl Player {
fn login(self, pass: str) -> Vec<Player> {
...
}
}
let me: Player = Player {username="me", ...
me.login(password);
Typescript style (similar to rust but without arrows, also OOP). pros: simple, easy to understand. cons: OOP can cause problems
class Player {
username: string;
pos: Vec3;
appearance: Color;
constructor(username: string, pos: Vec3, appearance: Color) {
this.username = username;
...
}
login(password: string):Player[] {
...
}
}
let me: Player = Player(...
me.login(password);
Fortran style (types below definition). Pros: clear whats going on? (maybe too used to fortran weirdness). Cons: extremely verbose.
type player
string :: username
real(kind=32), dimension(3) :: pos
integer(kind=8), dimension(3) :: appearance
contains
procedure :: login
end type player
player function init_player(username, position, appearance) result(this)
string :: username
real(kind=32), dimension(3) :: position
integer(kind=8), dimension(3) :: appearance
this%username = username
this%position = position
this%appearance = appearance
end function
function login(this, password) result(players)
class(player) :: this
string :: password
player, dimension(:), allocateable :: players
...
end function
...
player :: me
me = Player("me" ...
me%login(password)
Go style, types after like rust/typescript but no :
, interfaces instead of OOP, prefix types on the left of functions. Slices also backwards. Pros: simple, cons: syntax is backwards and weird, implementing a tupe would be annoying.
type Player {
username string
position Vec3
appearance Color
}
func (p Player) login(password string) []Player {
...
}
me := Player("me" ...
me.login(...
Let me know what you think, thanks all!
Personally, I like functions that operate on plain objects. I tend to avoid creating classes unless the language forces the issue. Elixir makes me happy in this regard. Gerbil scheme does some of the same. BQN takes things to an extreme that I rather enjoy as well, but we're firmly in uncommon territory at that point XD
Neat! Ill have to read up on this, I haven't used a APL-like language before. The closest thing to APL or BQN ive used is Uiua if you have heard of it.