AdeptLanguage / Adept

The Adept Programming Language
GNU General Public License v3.0
121 stars 9 forks source link

[add] Better array literals #23

Closed IsaacShelton closed 3 years ago

IsaacShelton commented 4 years ago

I don't have a full idea yet of how to implement this, but the goal is to have the following:

arr *int = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
arr 10 int = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
arr <int> Array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
arr <int> List = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

I'm thinking this will probably require some built-in type kind of like std::initializer_list in C++.

ghost commented 3 years ago

@IsaacShelton Is there any progress with this feature or this issue is low-priority?

IsaacShelton commented 3 years ago

@t0md3an I'm still figuring out how to implement this, for now it will remain medium to lowish priority

IsaacShelton commented 3 years ago

Initializer Lists have finally been added.

They work on List and Array types by default:

import basics

func main {
    my_list <int> List = {0, 1, 2, 3, 4, 5}
    my_array <int> Array = {0, 1, 2, 3, 4, 5}
}

They can be nested

import basics

func main {
    my_integers <int> List = {0, 1, 2, 3, 4, 5}
    my_matrix <<int> List> List = {my_integers, my_integers, my_integers, my_integers}

    printMatrix(my_matrix)

    float_matrix <<float> List> List = {
        {1.0f, 0.0f, 0.0f, 0.0f} as <float> List,
        {0.0f, 1.0f, 0.0f, 0.0f},
        {0.0f, 0.0f, 1.0f, 0.0f},
        {0.0f, 0.0f, 0.0f, 1.0f}
    }

    printMatrix(float_matrix)
}

func printMatrix(matrix <<$T> List> List) {
    each list <$T> List in matrix {
        each item $T in list {
            place(item)
            place(" ")
        }
        newline()
    }
}

Initializer lists don't work with fixed arrays, but the following is effectively equivalent:

my_integers <int> Array = {0, 1, 2, 3, 4, 5}

Array values can be used like:

my_integers[0] = 10

The length of an Array can be accessed with:

length_of_the_array usize = my_array.length

The raw pointer can be accessed with:

raw_pointer *int = my_array.items

NOTE Array is a dumb structure, just a pointer and a length, it's literally declared as:

struct <$T> Array (items *$T, length usize)