#TealScript: Can you use a template variable to define the length for a BoxKey static array?
15 messages · Page 1 of 1 (latest)
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:
https://tealscript.netlify.app/guides/supported-types/arrays/#static-arrays
https://tealscript.netlify.app/guides/supported-types/arrays/#dynamic-arrays
Well… this defeats the purpose of the templatevar lol
Will it be unknown for sure?
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
It will be known at creation time but not before
Hmm well if it’s unknown what do you think about using a dynamic array?
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
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?