HomeForumSourceResearchGuide
Sign in to reply to forum posts.
"from" notation with dynamic array constructions

Hello,

When one is constructing a data of type array dynamically with the "from" notation, does one give it the type of the array itself, or the type of the cell of the array?

Example:

    Type cellType = new Type(Type.DATA, 0, 0, new Field(new Type(Type.INTEGER, 0, size), "Integer"))
    // Construct the array type.
    // TODO: Not sure if this has to have a size.
    Type arrayType = new Type(Type.ARRAY, 0, 0, new Field[](new Field(cellType, "WrappedInteger")))
    Data curCell = new Data() from cellType
    Data curArray[] = new Data[] from arrayType

Does one use arrayType on the last line or cellType on the last line, as both seem to work.

Many thanks,

Sava

Hi Sava :-)

I've done quite a bit working with dynamic typing in Dana for a recent project. I'm not sure this is documented, but the language definitely supports either form (if you test its behaviour, which I think I did once, you can infer it automatically upgrades a non-array type to an array one, and doesn't do anything to the type if given an array type).

By the way, for your comment "Not sure if this has to have a size.", if you provide a size you get a fixed-length array type, as if you'd done char q[5]. If you don't provide a size you get a dynamic-length array, which is what you'd normally want, like char q[].

Hope that helps!

jess

Hello jess,

Many thanks for your thorough response! And yes, it does help a lot, I presumed that was happening underneath as both options seemed to produce the same array type.

I have a quick follow up question, though. How are dynamic length arrays different to fixed-length arrays? Aren't "arrays" supposed to be fixed-length ADTs by definition? I guess my question is what are the implications of having a dynamic-sized array as opposed to a fixed-length one?

Many thanks,

Sava

Hi Sava :-)

You're welcome!

The dynamic/fixed-size array thing works something like this. When I said "fixed" I probably should have said "static", since that's where the difference really is.

If you do this:

char str[5]

...Dana is going to pre-allocate this array for you. You can then assign its various indices directly, without first having to do a new(). The array is always going to be of length 5.

If you do this:

char str[]

...Dana will not pre-allocate this array and you're going to need to do an = new char[]() kind of thing at some point. This str variable can point to any array length, and can point at different arrays over time.

I think this makes sense if you're coming from C, which has a similar concept of static vs dynamic arrays.

I guess there are other uses for statically-defined arrays, but for me the neat thing about them is network data serialisation. If we do something like this:

data Packet {
	int8 type
	byte kload[16]
	int8 flags
}

...Dana can directly serialise this into bytes, like:

Packet p = new Packet()
byte stream[] = dana.serial(p)

The length of the stream array here is exactly 32 bytes (it's the raw data of the packet). This is really useful when you're working with low-level network protocols which specify particular packet formats. I can't think of another high-level language that can do this kind of low-level data framing so concisely :)

jess

[edited by forum mods to fix a username association error, thanks]