-
Notifications
You must be signed in to change notification settings - Fork 758
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
Comments
public isolated function ensureType(any|error v, typedesc<any> t = <>) returns t|error May I know the actual use case for this? |
Yeah, this wouldn't work even if you specified the argument explicitly, because only error err = value.ensureType(Error); // error With a
the jBallerina compiler uses The way |
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;
} |
Description
Description
When using the
ensureType()
method in Ballerina, the compiler cannot infer thetypedesc
argument for the type parametert
in the following sample.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
Version
Ballerina version: 2201.10.2
Environment Details (with versions)
OS: Windows
The text was updated successfully, but these errors were encountered: