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

Inherent type of a slice of a list #551

Closed
MaryamZi opened this issue Jun 18, 2020 · 2 comments
Closed

Inherent type of a slice of a list #551

MaryamZi opened this issue Jun 18, 2020 · 2 comments

Comments

@MaryamZi
Copy link
Member

Description:
The lang.array:slice method is defined as follows.

# Returns a subarray starting from `startIndex` (inclusive) to `endIndex` (exclusive).
#
# + arr - the array
# + startIndex - index of first member to include in the slice
# + endIndex - index of first member not to include in the slice
# + return - array slice within specified range
public function slice(Type[] arr, int startIndex, int endIndex = arr.length()) returns Type[] = external;

The concern is regarding the actual inherent type of the slice created; is it the type representing the exact slice at the point of creation?

public function main() {
    [int, string, boolean] x = [1, "ballerina", true];

    int i = 1;
    int j = x.length();

    (int|string|boolean)[] y = x.slice(i, j);
}

So the static type here would have to be an array of (int|string|boolean), but what should be the inherent type of the slice, which in this case would be ["ballerina", true]?

Should it be

  1. [string, boolean]
  2. (string|boolean)[]
  3. (string|boolean)[2]
  4. (int|string|boolean)[]

Similarly, for an array, should a slice result in a fixed length list?

import ballerina/io;

public function main() {
    int[] x = [1, 2, 3];

    int i = 1;
    int j = x.length();

    int[] y = x.slice(i, j);
    io:println(y.toJsonString()); // [2, 3]
    io:println(y is int[2]); //  should this be true?
}
@jclark
Copy link
Collaborator

jclark commented Jun 19, 2020

We want slice (and other lang lib functions) to behave for the most part like generic functions. The language doesn't have generic functions yet, but the basic concept is that you instantiate a generic function with a type parameter, which is determined at compile-time, to get a normal function. For slice, the instantiated function should work as if it was written in a normal way in Ballerina. This means it would create a list of type T[], where T is the type used to instantiate the generic function.

So in your case, in order to call array:slice, we need to instantiate array:slice with Type as (int|string|boolean). So the inherent type of the returned array would be (int|string|boolean)[].

(One of the reasons lang.array is called lang.array not lang.list is that it treats its first argument as an array.)

@MaryamZi
Copy link
Member Author

Ack, thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants