c3c icon indicating copy to clipboard operation
c3c copied to clipboard

Introduce single line function declaration

Open lerno opened this issue 3 years ago • 8 comments

Variant 1:

// Statement
fn int square(int x) = return x * x;
fn void foo(int y) = baz(y);

Proposal 2:

fn int square(int x) = x * x;
fn void foo(int y) = baz(y);

lerno avatar Apr 10 '22 07:04 lerno

Feel free to vote on this one using 👍 and 👎

lerno avatar Apr 10 '22 07:04 lerno

Proposal 3:

fn int square(int x) {x * x};
fn void foo(int y) {baz(y)};

data-man avatar Apr 10 '22 08:04 data-man

Proposal 4:

fn int[2] square(int x) (x * 2, x * 3);

data-man avatar Apr 10 '22 08:04 data-man

3 is ambiguous. 4 would need special parsing.

lerno avatar Apr 10 '22 12:04 lerno

An alternative uses =>

fn int square(int x) => return x * x;
fn void foo(int y) => baz(y);

fn int square(int x) => x * x;
fn void foo(int y) => baz(y);

lerno avatar Apr 11 '22 22:04 lerno

Proposal 6: :smile:

fn int square(int x) -> return x * x;

data-man avatar Apr 12 '22 02:04 data-man

I'm trying to reserve "->" in case it's needed later, plus the problem of that operator when converting C code.

lerno avatar Apr 12 '22 11:04 lerno

Two variants are now available for test:

fn int square(int x) = x * x;
fn int square(int x) => x * x;

See if it actually adds any value or we remove it.

lerno avatar Sep 14 '22 21:09 lerno

= is more likely to be adopted than =>

lerno avatar Oct 21 '22 09:10 lerno

Would single line functions also work for anonymous functions?

tommy-mitchell avatar Mar 14 '23 07:03 tommy-mitchell

@tommy-mitchell They already do.

lerno avatar Mar 14 '23 10:03 lerno

=> was chosen.

lerno avatar Mar 14 '23 10:03 lerno