ballerina-platform / ballerina-lang

The Ballerina Programming Language
https://ballerina.io/
Apache License 2.0
3.54k stars 733 forks source link

[2201.9.0] Add the union of the types in the `Create variable with type` code action #42607

Closed nipunayf closed 2 weeks ago

nipunayf commented 3 weeks ago

Purpose

In scenarios like the one described in the sample, there may arise situations where dependent types require a union of types rather than individual ones. To address this, the PR introduces an additional code action with a union type alongside the existing ones to cover this specific use case.

Fixes #42591

Approach

Return the union of all the possible types along with the ones that are already provided in the respective code action.

Samples

https://github.com/ballerina-platform/ballerina-lang/assets/59343084/a8f874a2-b670-49bb-994f-b3b93fd37348

Check List

LakshanWeerasinghe commented 2 weeks ago

When an HTTP client resource is invoked, this fix will suggest a code action with the type http:Response|anydata|http:ClientError. However, for HTTP client invocations, we cannot use a union type of http:Response|anydata because it will result in a runtime error.

public function main() returns error? {
    http:Client cl = check new("http://localhost:8080");

    http:Response|anydata|http:ClientError path1 = cl->/path1;
    io:println(typeof path1); // ClientError

    http:Response|anydata|http:ClientError path2 = cl->/path2;
    io:println(typeof path2); // ClientError

    http:Response|http:ClientError path1_a = cl->/path1;
    io:println(typeof path1_a); // http:Response

    anydata|http:ClientError path2_a = cl->/path2;
    io:println(typeof path2_a); // anydata
}
nipunayf commented 2 weeks ago

When an HTTP client resource is invoked, this fix will suggest a code action with the type http:Response|anydata|http:ClientError. However, for HTTP client invocations, we cannot use a union type of http:Response|anydata because it will result in a runtime error.

public function main() returns error? {
    http:Client cl = check new("http://localhost:8080");

    http:Response|anydata|http:ClientError path1 = cl->/path1;
    io:println(typeof path1); // ClientError

    http:Response|anydata|http:ClientError path2 = cl->/path2;
    io:println(typeof path2); // ClientError

    http:Response|http:ClientError path1_a = cl->/path1;
    io:println(typeof path1_a); // http:Response

    anydata|http:ClientError path2_a = cl->/path2;
    io:println(typeof path2_a); // anydata
}

This was one of the issues that arose during the design change. Since it is specific to the library module, we cannot handle it as a special case from the language server side. After an offline discussion, we have decided to allow it for now. Unlike in the generated client, the primary user flow in this scenario is to rewrite the type or write the type from scratch.

SasinduDilshara commented 2 weeks ago

@nipunayf if we need this change with Update 9. This PR should be base with 2201.9.0-stage branch