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

Rethinking failable types #478

Open
b1ek opened this issue Sep 15, 2024 · 8 comments
Open

Rethinking failable types #478

b1ek opened this issue Sep 15, 2024 · 8 comments
Labels
compiler help wanted Extra attention is needed syntax

Comments

@b1ek
Copy link
Member

b1ek commented Sep 15, 2024

preface

i feel like the way failables are implemented right now is completely wrong and we have to start over.

lets say that we have $curl something.com$. it returns a Text, right? and its not the data that is failable, but the eval block itself.

so right now we have failable types that are only usable in function declarations:

fun process(raw: Text): Text {
    ...
}
fun get_data(): Text? {
    let raw = $curl data.com$? // <--- this is the failable part
    return process(raw) // <--- data is Text here, not Text?
}

but imo types have nothing to do with failability. even when called, the function has to have a failed block right away and its not possible to unwrap that type later as you would do with a Result in rust.

like if i call it like this

let a = get_data()

the compiler will say "The failed expression must be followed by a block or statement". an expression. not a type

what i want to do with it

remove the failable type, and add a failable function modifier instead:

failable fun get_data() { ... }

the same way as the pub modifier works

@Ph0enixKM
Copy link
Member

Ph0enixKM commented Sep 15, 2024

I was also thinking about it. Also what about actually failable types?

We could implement Rust's Result<Int,T> type. Where T is the data type and Int will always be an exit code. Then T? could be the syntax representing this new type. Additionally, we do not introduce yet another breaking change.

Text? can be some text value or hide an exit code that we can use to handle later.

I got stuck with the Bash implementation for it as Sh does not support arrays and Bash nested arrays.

@b1ek
Copy link
Member Author

b1ek commented Sep 15, 2024

i think we could implement Result<Int, T> when, if we make custom types. and implement it as part of stdlib, not as part of the compiler, with an .unwrap() and .expect(Text) methods like rust has

@b1ek
Copy link
Member Author

b1ek commented Sep 22, 2024

so far i see we have the following options:

  1. leave it as is, and add a ( ... ) brackets for types so that a union type could be also marked as failable
  2. add a function modifier:
    1. pub failable fun abc(): Type {}
    2. pub fun abc?(): Type {}
    3. pub fun abc()?: Type {}
  3. no modifiers, a function is failable if it has failable code in it
  4. compiler flags

@b1ek
Copy link
Member Author

b1ek commented Sep 22, 2024

come to think of it, do we even need the modifiers? it could be set automatically if a function has a $...$? or fail in it. that would be sort of hard to tell, though

@b1ek
Copy link
Member Author

b1ek commented Sep 22, 2024

or use the compiler flags:

#[failable_fun]
fun abc(): Type { ... }

@mks-h

This comment was marked as off-topic.

@b1ek

This comment was marked as off-topic.

@mks-h

This comment was marked as off-topic.

@Ph0enixKM Ph0enixKM added this to the Amber 0.5.0-alpha milestone Oct 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler help wanted Extra attention is needed syntax
Projects
None yet
Development

No branches or pull requests

3 participants