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

Feature request: adding description field to the @return! contract #1986

Closed
alexveden opened this issue Feb 18, 2025 · 6 comments
Closed

Feature request: adding description field to the @return! contract #1986

alexveden opened this issue Feb 18, 2025 · 6 comments

Comments

@alexveden
Copy link
Contributor

Should we consider adding optional text description to @return! contract with explanations when the error can be returned?

@lerno
Copy link
Collaborator

lerno commented Feb 20, 2025

Can you explain what you want with an example?

@alexveden
Copy link
Contributor Author

Something along those lines:

<* 
String checksum 

@return! Error.ARGUMENT "Empty string is not allowed"
@return! MathError.ZERO_DIVISION : "Magic number should be non-zero"
// Old behavior should be preserved
@return! Error.ARGUMENT
@return! Error.ARGUMENT, MathError.ZERO_DIVISION
*>
fn int! checksum(String s, int magic) {
  if (!s) return Error.ARGUMENT?;
  if (magic == 0) MathError.ZERO_DIVISION?;
 
  int sum = 0;
  foreach(c : s) sum += c;

  return sum / magic;
}

And another crazy idea came to my mind. What if we could extract the error message with reflection mechanism from function pointer/signature:

int! csum = checksum("foo", 0);
if (catch err = csum) {
   io::printfn("Error: %s -- %s", err, checksum.strerror(err)); 
   // Or via some builtin / macro
   //io::printfn("Error: %s -- %s", err, @strerror(checksum, err)); 
}
// Prints
Error: ARGUMENT -- Magic number should be non-zero

@alexveden
Copy link
Contributor Author

With the functionality above, this could be interlinked with #1985

@lerno
Copy link
Collaborator

lerno commented Feb 20, 2025

But isn't a description already possible today?

@alexveden
Copy link
Contributor Author

Indeed it's possible, but it's not in the docs :(

https://c3-lang.org/language-fundamentals/comments/#_top

Image

@lerno
Copy link
Collaborator

lerno commented Feb 20, 2025

I added it to the docs now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants