#TealScript: Can you use a template variable to define the length for a BoxKey static array?

15 messages · Page 1 of 1 (latest)

faint halo
#

I want to do something like this:

export class Mystery extends Contract {
  arrayLength = TemplateVar<uint64>();
  
  arr = BoxKey<StaticArray<uint64, this.arrayLength.value>>({ key: 'curious' });

But I can't get it to compile. Is there any reasonable way for me to accomplish this?

craggy sedge
#

I'm not too familiar with ts but I believe the second value needs to be a literal, eg; static length will be 32

#
  arrayLength = TemplateVar<uint64>();
  
  arr = BoxKey<StaticArray<uint64, 32>>({ key: 'curious' });

}```
#

This compiled for me

#

@faint halo

#

If you weren't sure what the length would be you would use a DynamicArray-- which I believe would look like this:

#

Well… this defeats the purpose of the templatevar lol

#

Will it be unknown for sure?

faint halo
#

Yeah, it would be cool if you could use a template variable in this way! And I feel like it should maybe be possible since the length of the array would be a literal at compile time

faint halo
craggy sedge
#

Hmm well if it’s unknown what do you think about using a dynamic array?

craggy sedge
#

This compiled for me as well:

  
  arrayLength = TemplateVar<uint64>();
  arr = BoxKey<uint64[]>({ key: 'curious' });

  constructor(){
    super();
    this.arrayLength = this.arr.value.length
  }
}```
#

Maintains your arrayLength TemplateVar while accounting for the ambiguity of arr.value.length

faint halo
#

I have simplified my use case for this example. I'm not interested in using a dynamic array. @dim crystal Is there any reason why this can't be done with a template var?