ballerina-platform / ballerina-spec

Ballerina Language and Platform Specifications
Other
167 stars 53 forks source link

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

Open LakshanWeerasinghe opened 4 months ago

LakshanWeerasinghe commented 4 months ago

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:

jclark commented 4 months ago

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.