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

Fails to Infer typedesc Argument for ensureType() Method #43774

Closed
chathushkaayash opened this issue Jan 25, 2025 · 3 comments
Closed

Fails to Infer typedesc Argument for ensureType() Method #43774

chathushkaayash opened this issue Jan 25, 2025 · 3 comments

Comments

@chathushkaayash
Copy link

Description

Description

When using the ensureType() method in Ballerina, the compiler cannot infer the typedesc argument for the type parameter t in the following sample.

Error err = check value.ensureType();

Expected Behavior

The compiler should infer the type automatically based on the context.

Actual Behavior

The compiler fails to infer the typedesc argument for ensureType() saying cannot infer the 'typedesc' argument for parameter 't' with '(Error|error)' as the contextually-expected type mapping to return type '(t|error)'(BCE3934)

Steps to Reproduce

  1. Create a new Ballerina file with the following content:
public type Error distinct error<ErrorDetails>;

public type ErrorDetails record {|  
    string errorMessage?;  
|};

function foo() returns int|Error {
    return error Error("Error");
}

public function main() {
    int|Error value = foo();
    Error err = check value.ensureType();
}
  1. Run the Ballerina build command:
bal build

Version

Ballerina version: 2201.10.2

Environment Details (with versions)

OS: Windows

@TharmiganK
Copy link
Contributor

TharmiganK commented Jan 28, 2025

ensureType can only infer the expected type which is part of any. I do not think this is possible. The error may be improved. Adding @MaryamZi as well

public isolated function ensureType(any|error v, typedesc<any> t = <>) returns t|error

May I know the actual use case for this?

@MaryamZi
Copy link
Member

MaryamZi commented Jan 28, 2025

Yeah, this wouldn't work even if you specified the argument explicitly, because only typedesc<any> is allowed.

error err = value.ensureType(Error); // error

With a checking-expr like

T val = check E;

the jBallerina compiler uses T|error as the contextually-expected type for E, which is the reason for this specific error message.

The way check has been used atm isn't correct anyway since even if this did worked, check would have returned an error (including Error). You can probably do an is check and cast instead.

@TharmiganK TharmiganK transferred this issue from ballerina-platform/ballerina-library Jan 28, 2025
@TharmiganK
Copy link
Contributor

Thanks @MaryamZi

@chathushkaayash As mentioned, you could use a if check and cast:

public function main() returns error? {
    int|Error value = foo();
    if value is Error {
        Error err = <Error> value;
    }
}

Or do an early return:

public function main() returns error? {
    int|Error value = foo();
    if value is int {
        return error("unexpected value found");
    }
    // The type of `value` will be narrowed
    // to `Error` after the early return
    Error err = value;
}

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

No branches or pull requests

3 participants