Skip to content

single expression function syntax #3369

Closed as not planned
Closed as not planned
@scarf005

Description

@scarf005

Summary

allow kotlin-style single expression function syntax in form of fn name() = expr

fn hello() -> &'static str = "Hello"

fn add(a: i32, b: i32) -> i32 = a + b

fn rank(n: i32) -> i32 = match n {
    1 => 1,
    2 | 3 | 4 => 2,
    13..=20 => 3,
    _ => 4,
}

example implementation

Advantages

one less brackets improve readability, espcially for functions with single match block.

Disadvantages

being able to do same things in two different way may be confusing.

fn add(a: i32, b: i32) -> i32 = a + b
fn add(a: i32, b: i32) -> i32 { a + b }

Previous Discussion

there was previous discussion on this topic, but the it was mostly about implicit return type. however, that is beyond this RFC's scope.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions