HomeForumSourceResearchGuide
Sign in to reply to forum posts.
Getting type of array cell from array type vs the first element in the array

Hello,

I'm trying to access the primitive type of a data type that wraps it from an array of those Data's.

Made simple: Array of Datas that wrap a single primitive type.

My question is the following:

Why does the following work and the latter doesn't?

    Type columnType = typeof(wrappedColumn[0]).fields[0].type
    Type columnType = typeof(wrappedColumn).fields[0].type.fields[0].type
The first gets the type of the data in the array (typeof(wrappedColumn[0])) and gets the primitive type (.fields[0].type) from the Data.

The second gets the type of the array, which should contain a single field holding the cell type (typeof(wrappedColumn).fields[0].type), and gets the primitive type (.fields[0].type) from the Data as in the previous line. However, this results with an index out of bounds exception.

I am unsure, whether, I'm doing something wrong or something else is taking place.

If you are struggling to recreate the error I could provide the complete code.

Many thanks,

Sava

Hi Sava :-)

I'm not 100% sure I understand your scenario; could you post a more complete example with the data types involved and maybe I can see something?

jess

Hello again, jess,

Apologies for the vague question. Here is the concrete example:

https://github.com/savakazakov/CSVUtilExtension/blob/07da6e213d95cd7384bf67cfc3e96e015c111780/CSVUtilExtension.dn#L40

Originally I had the following line:

  Type columnType = typeof(wrappedColumn[0]).fields[0].type

However, this runs into the problem of having the user supply an empty wrappedColumn, at which point this will throw an index out of bounds exception. Then I realised that one can get the type of the cells in the array like this:

 typeof(wrappedColumn).fields[0].type

And if we combine both, we get:

 Type columnType = typeof(wrappedColumn).fields[0].type.fields[0].type

Unfortunately, this somehow loses the information of the "lowest-level" type, i.e. columnType is empty. I might be missing something very obvious here, but I think these two variants should have the same semantics.

Many thanks,

Sava

Hi Sava :-)

I have tried to adapt your code to an example of the simplest, but hopefully equivalent, form. I think the below code is a faithful representation:

component provides App requires io.Output out, data.IntUtil iu {

	void takeData(Data d[]) {
		Type columnType = typeof(d).fields[0].type.fields[0].type

		if (columnType == null)
			out.println("no type!")
			else
			out.println("type-class: $(columnType.class); size: $(columnType.size)")
	}

	int App:main(AppParam params[]) {
		
		int8 val = 9

		Type cellType = new Type(Type.DATA, 0, 0, new Field(new Type(Type.INTEGER, 0, 8), "Integer"))
		Data d = new Data() from cellType

		Type arrayType = new Type(Type.ARRAY, 0, 0, new Field[](new Field(cellType, "WrappedInteger")))
		Data curArray[] = new Data[1] from arrayType

		Data curCell = new Data() from cellType

		curCell:.0 = val
		curArray[0] = curCell

		takeData(curArray)

		return 0
	}
	
}

...but this seems to print out the expected thing, so I think I still might be missing something here :-(

jess