ballerina-lang
ballerina-lang copied to clipboard
[Bug]: Type included object's functions have to have the same parameter names
Description
See the below object type;
public type Finder isolated object {
public isolated function findCodeSystem(r4:uri? system = (), string? id = (), string? version = ()) returns r4:CodeSystem|r4:FHIRError;
}
when I type included this in another object and changed the name of a parameter like below;
isolated class InmemoryTerminology {
*Finder;
public isolated function findCodeSystem(r4:uri? systems, string? id, string? version) returns r4:CodeSystem|r4:FHIRError {
}
Ballerina complains saying;
mismatched function signatures: expected 'public function findCodeSystem(ballerinax/health.fhir.r4:4.3.4:uri? system, string? id, string? version) returns (ballerinax/health.fhir.r4:4.3.4:CodeSystem|ballerinax/health.fhir.r4:4.3.4:FHIRError)', found 'public function findCodeSystem(ballerinax/health.fhir.r4:4.3.4:uri? systems, string? id, string? version) returns (ballerinax/health.fhir.r4:4.3.4:CodeSystem|ballerinax/health.fhir.r4:4.3.4:FHIRError)'(BCE2094)
ballerinax/health.fhir.r4:4.3.4:uri? systems
ballerinax/health.fhir.r4:4.3.4:uri? systems
Steps to Reproduce
No response
Affected Version(s)
Ballerina 2201.8.4 (Swan Lake Update 8)
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
In Ballerina compiler function argument is represented a combination of argument type and the argument name (In java it is only type).
Following is a valid use case that used the argument name.
type Data record {|int a; string c; boolean b;|};
function foo(int a, string c, boolean b) {
}
public function main() {
Data data = {a: 1, c: "hello", b: true};
foo(...data);
}
In the above code we support ...data
as the function argument, So Ballerina will map the each record field with function argument by mapping the record field name with function argument name.
I'm not sure if the current restriction is required in the spec, I've created a spec issue for this.
In Ballerina compiler function argument is represented a combination of argument type and the argument name (In java it is only type).
Following is a valid use case that used the argument name.
type Data record {|int a; string c; boolean b;|}; function foo(int a, string c, boolean b) { } public function main() { Data data = {a: 1, c: "hello", b: true}; foo(...data); }
In the above code we support
...data
as the function argument, So Ballerina will map the each record field with function argument by mapping the record field name with function argument name.
Parameter names don't affect typing. The argument list is always constructed at the call site and is based on the function-reference
of the function-call-expr
, so this isn't an issue. E.g., modifying your example slightly
import ballerina/io;
type Data record {|int a; string c; boolean b;|};
function foo(int x, string y, boolean z) {
io:println("x: ", x);
io:println("y: ", y);
io:println("z: ", z);
}
public function main() {
Data data = {a: 1, c: "hello", b: true};
// Allowed since parameter names don't affect typing.
function (int a, string c, boolean b) fn = foo;
// Argument list is constructed as
// `(1, "hello", true)` in `fn(1, "hello", true)`
fn(...data);
// Disallowed since parameter types don't match by position,
// irrespective of names.
// error: expected 'function (string,int,boolean) returns ()',
// found 'function (int,string,boolean) returns ()'
// function (string y, int x, boolean z) _ = foo;
}
x: 1
y: hello
z: true
We can get rid of this restriction - https://github.com/ballerina-platform/ballerina-spec/issues/1304