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

Use of type cast to ignore errors #489

Closed
jclark opened this issue Apr 13, 2020 · 5 comments
Closed

Use of type cast to ignore errors #489

jclark opened this issue Apr 13, 2020 · 5 comments
Assignees
Labels
Area/Lang Relates to the Ballerina language specification design/usability Design does not work well for some tasks

Comments

@jclark
Copy link
Collaborator

jclark commented Apr 13, 2020

Given:

function foo1() returns string|int;
function foo2() returns string|int|error;

Both of these will compile:

int x = <int>foo1();
int x = <int>foo2();

This feels like silently ignoring the error. Another example is

json j;
string x = <string>j.x.y.name;

With lax static typing, type of j.x.y is json|error, and the cast to <string> results in the error being implicitly converted to a panic.

This relates to #350, #487 and #488.

@jclark jclark added Area/Lang Relates to the Ballerina language specification design/usability Design does not work well for some tasks labels Apr 13, 2020
@jclark jclark added this to the 2020R2 milestone Apr 13, 2020
@jclark jclark self-assigned this Apr 13, 2020
@sanjiva
Copy link
Contributor

sanjiva commented Apr 14, 2020

I think our mindset was that the use of cast here is not to ignore the error but rather to assert confidence in the value being of that type, even though it may be of another.

I don't think we want to lose that.

@ThisaruGuruge
Copy link
Member

We kinda used the following practice when it comes to type checking

string|error result = getSomeStringOrError();
if (s is error) {
    // handle error
}
string s = <string>result;

This is to reduce the long if else ladders and improve the code readability. This example is a minimal one, but there are some complex cases. This may effect it.
@daneshk @shafreenAnfar

@jclark
Copy link
Collaborator Author

jclark commented Sep 14, 2020

@ThisaruGuruge That one is #73

@jclark jclark added the status/discuss Needs discussion outside github comments label Nov 17, 2020
@jclark
Copy link
Collaborator Author

jclark commented Nov 20, 2020

I think the rule should be that you "cannot cast away error", meaning that if there are errors in the type before the cast, there must be errors in the type after the cast. The before/after errors don't have to be the same: it's fine to cast to a more precise error. Note that the language already provides checkpanic which has the same effect as casting to a type with all errors removed.

@jclark
Copy link
Collaborator Author

jclark commented Nov 20, 2020

We decided we are ok with the proposal in #489 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area/Lang Relates to the Ballerina language specification design/usability Design does not work well for some tasks
Projects
None yet
Development

No branches or pull requests

3 participants