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

Clarification on the member type of constructed arrays using query expressions #1309

Open
LakshanWeerasinghe opened this issue Jul 4, 2024 · 1 comment
Assignees
Labels
Area/Lang Relates to the Ballerina language specification spec/undefined Spec does not define some behavour Type/Improvement Enhancement to language design

Comments

@LakshanWeerasinghe
Copy link
Contributor

Description:
The spec clearly explains how to derive the basic type (constructed collection type) from a query expression. However, it does not address how to determine the member type when constructing an array.

This issue arises when a user attempts to add an element to an array and encounters an error indicating an inherent type violation.

import ballerina/io;

public function main() {

    (int|string)[] all = [];
    io:println(typeof all); // (int|string)[]

    int[] ints = [1, 2, 3, 4];

    all = from var item in ints select item;
    io:println(typeof all); // int[]

    all.push("1"); // inherent type violation
}

Suggested Labels:

Code sample that shows issue:

Related Issues:

@LakshanWeerasinghe LakshanWeerasinghe added the spec/undefined Spec does not define some behavour label Jul 4, 2024
@jclark jclark self-assigned this Jul 9, 2024
@jclark
Copy link
Collaborator

jclark commented Jul 9, 2024

I agree the spec should define this.

I think the natural thing to do is to make the member type be the broad type of the expression in the select, so that it is consistent with the type you would get from [...E], where E is what is the select expression.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area/Lang Relates to the Ballerina language specification spec/undefined Spec does not define some behavour Type/Improvement Enhancement to language design
Projects
None yet
Development

No branches or pull requests

3 participants