Search code examples
arrayscompiler-construction

Is an array a type or a value?


I'm writing my own language (just for fun), and I'm about to implement arrays. I define an array as a datastructure with a fixed, final length, that contains multiple values. So they should work like in Java i.e. C-like

An array should be created with:

nr[] a = [1, 2, 5, 3, 4]

Where nr is the type of the values, a is the name, and [1, 2, 5, 3, 4] the container.

Arrays shall be passed to functions per name with:

main {
    nr[] a = [1, 2, 5, 3, 4]
    printArray(a)
}

func printArray(array):
    print(array)

or literally

main:
    printArray([1, 2, 5, 3, 4])

func printArray(array):
    print(array)

The language contains three main datatypes, bool, text and nr, as well as the var keyword, which sets the type accordingly to the value. An array can be passed between methods, and also stores values itself, like a variable.

I call things that store a value, like literals (5, "text" or false), or names (a = 10), or calls (function()), "ValueHolders", because they all hold one specific value.

Now onto my question: Is my array a ValueHolder or a datatype, or do I have to come up with something completely new for that?

Please shower be with examples from other languages, I'd love to learn more!

EDIT; A few examples:

  • A type with Arraybrackets is an "arraytype": nr[] a = ...

Anything behind that declaration gets implicitly casted to a nr-array.

  • Arraybrackets with values/calls create an "arrayliteral": ["a", b(), [1, 2, 3]]

This is a var-array, where the type of the values in the array is unknown. There is no way of finding out in advance, which type these values share, because there not generated, until the line gets executed. Why? If a value in this array is the return-value of a funtion, the function has to get invoked, and in a non-functional language, this can possibly alter the whole structure of the program.

  • A function can set an array as a return-value: func a() -> nr:

That just means: Cast every return-value to a nr-array. If this doesn't work, the returnvalue is invalid, an the program throws a runtime-exception. (Not the cleanest way, i know, but returntypes are optional, and more like a guideline, than a strict rule.)


Solution

  • I think what you want to know, really depends on the context, because var[] has a completely different purpose than [1, 2, 3].

    In your words:

    • the container [val1, val2, ...] is a ValueHolder, because it can be passed to functions and declarations. It returns an array value.
    • If you defined an array as var[] array = ..., "array" is also a ValueHolder, which returns an array value as well.
    • var[] a = ... is a declarative type, it tells which type is expected for the value after the =.
    • a[1] = ... is an array access, but neither a ValueHolder, or a declaration.
    • function(a[2]) is another array access, but this time, it holds a returnvalue. Imagine this as something similar to a function call.

    For your language, you have to deal with each of these cases seperatly, they all have different meanings. Good luck :)