Skip to content

Possibility of inferring all functions as maybe keyword #60

@0xForerunner

Description

@0xForerunner

One common complaint I see about the keyword-generics-innitiative is that the syntax can be quite verbose. The core idea I have is to essentially treat all unmarked keyword functions as maybe keyword. A solution I've been exploring in my head looks something like this:

/// Not awaiting or otherwise using the future for this function
/// would result in a warning
async fn foo_async() {}

/// Can optionally be called in any context and can optionally be awaited
/// Notice that all regular functions would fall into this category
/// Hopefully no need for additional syntax here?
fn foo_maybe_async() {
   // Perhaps #[cfg(async)] makes more sense than `if ?async`
   if ?async {
       // Do some async stuff
   } else {
       // Do some sync stuff
   }
}

Awaiting a function which doesn't contain an `?async' block/section would result in a warning. Conversely, not awaiting a function which does contain an async block/section while inside an async context would also emit a warning. I imagine this style of implementation should be fully backwards compatible.

Perhaps there's something I'm missing here, but declaring a function signature like

?async fn maybe_async_foo(){}

or

#[maybe(async)]
fn maybe_async_bar(){}

seems superfluous.

Currently, the compiler can tell if a const fn is valid. So it would seem to me that it should also be able to validate if calling unmarked (maybe const) functions from within a const function is valid. I'll give an example:

This example would compile because the compiler could check that bar_maybe_const has a valid const implementation

/// Notice we are marking explicitly as const
const fn foo_const() -> usize {
    bar_maybe_const()
}

/// Notice we are leaving unmarked (maybe const)
fn bar_maybe_const() -> usize {
    if ?const {
        return 1
    } else {
        do_something_at_runtime()
    }
}

This example would error because the compiler could check that bar_maybe_const has no valid const implementation

/// Notice we are marking explicitly as const
const fn foo_const() -> usize {
    bar_maybe_const()
}

/// Notice we are leaving unmarked (maybe const)
fn bar_maybe_const() -> usize {
    // we don't provide a valid const implementation
    do_something_at_runtime() <-- Error: fn do_something_at_runtime is not const
}

This is just something that's been spinning in my head for a while and there's a good chance I'm not seeing the whole picture here. But I'd love to get your thoughts/opinions on this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions