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

[Bug]: Return type of anonymous function evaluated incorrectly #40409

Closed
IMS94 opened this issue May 12, 2023 · 2 comments
Closed

[Bug]: Return type of anonymous function evaluated incorrectly #40409

IMS94 opened this issue May 12, 2023 · 2 comments
Labels
needTriage The issue has to be inspected and labeled manually Reason/Invalid Issue is invalid. Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times. Type/Bug userCategory/Compilation

Comments

@IMS94
Copy link
Contributor

IMS94 commented May 12, 2023

Description

Consider:

import ballerina/http;

public function testFn() returns string[] {
    http:HeaderValue[] linkHeaders = [];
    http:HeaderValue[] filtered = linkHeaders.filter(function (http:HeaderValue h) returns boolean => {
                    if h.params.hasKey("rel") {
                        string rel = h.params["rel"];
                        return rel == "last";
                    } else {
                        return false;
                    }
                });
}

This gives the following compilation errors.

ERROR [test.bal:(5:103,6:32)] undeclared field 'params' in record 'record {| ballerina/http:2.7.0:HeaderValue h; |}'
ERROR [test.bal:(6:21,6:23)] invalid token 'if'
ERROR [test.bal:(6:25,6:25)] missing close brace token
ERROR [test.bal:(6:47,6:47)] missing close parenthesis token
ERROR [test.bal:(6:47,6:47)] missing semicolon token
ERROR [test.bal:(7:38,7:39)] undefined symbol 'h'
ERROR [test.bal:(8:32,8:45)] incompatible types: expected 'string[]', found 'boolean'
ERROR [test.bal:(9:23,9:27)] invalid token 'else'
ERROR [test.bal:(10:32,10:37)] incompatible types: expected 'string[]', found 'boolean'
ERROR [test.bal:(12:18,12:18)] missing open parenthesis token
ERROR [test.bal:(12:19,12:19)] missing identifier
ERROR [test.bal:(13:1,13:2)] invalid token '}'
error: compilation contains errors

Steps to Reproduce

No response

Affected Version(s)

2201.5.0

OS, DB, other environment details and versions

No response

Related area

-> Compilation

Related issue(s) (optional)

No response

Suggested label(s) (optional)

No response

Suggested assignee(s) (optional)

No response

@IMS94 IMS94 added the Type/Bug label May 12, 2023
@ballerina-bot ballerina-bot added needTriage The issue has to be inspected and labeled manually userCategory/Compilation labels May 12, 2023
@MaryamZi
Copy link
Member

Since the argument is an expression-bodied function, whatever after => should be a valid expression. This is like trying to say

    var fn = function (http:HeaderValue h) returns boolean {
        return {
            if h.params.hasKey("rel") {
                string rel = h.params["rel"];
                return rel == "last";
            } else {
                return false;
            }
        }
    };

So the compilation failure is expected.

A normal anonymous function will work.

    http:HeaderValue[] filtered = linkHeaders.filter(function (http:HeaderValue h) returns boolean { // removed =>
                    if h.params.hasKey("rel") {
                        string rel = h.params.get("rel");
                        return rel == "last";
                    } else {
                        return false;
                    }
                });

An expression-bodied function is possible with conditional expressions.

    http:HeaderValue[] filtered = linkHeaders.filter(function (http:HeaderValue h) returns boolean => 
                    h.params.hasKey("rel") ? h.params.get("rel") == "last" : false);

can be simplified to

    http:HeaderValue[] filtered = linkHeaders.filter(
        h => h.params.hasKey("rel") ? h.params.get("rel") == "last" : false);

@MaryamZi MaryamZi added Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times. Reason/Invalid Issue is invalid. labels May 12, 2023
@IMS94
Copy link
Contributor Author

IMS94 commented May 12, 2023

Closing this since this is the expected behavior

@IMS94 IMS94 closed this as completed May 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needTriage The issue has to be inspected and labeled manually Reason/Invalid Issue is invalid. Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times. Type/Bug userCategory/Compilation
Projects
None yet
Development

No branches or pull requests

3 participants