ModularM
Modular3y ago
39 replies
JIMC

Error:cannot construct 'Array' from 'ListLiteral[Int, Int, Int, Int, Int]' value in 'var' initialize

I'm trying to create a Array[T: AnyType, N: Int] struct but encountered this error when trying to run it:
from memory import stack_allocation

struct Array[T: AnyType, N: Int]:
   constrained[N <= 32, "`size` of N cannot be larger than 1024"]()
   alias size: Int = N
   alias type: AnyType = T 

   var capacity: Int
   var length: Int
   var storage: Pointer[T]

   fn __init__[*Ts: AnyType](inout self, owned iterable: ListLiteral[Ts]) raises:
      if N != len(iterable):
         raise Error("`size` of `iterable` is not equal to the `size` of array")
      self.capacity = self.size * 2
      self.storage = stack_allocation[N*2, T]()
      let src: Pointer[T] = Pointer.address_of(iterable).bitcast[T]()
      for i in range(len(iterable)):
         self.storage.store(i, src.load(i))
      

fn main():
   var myarray: Array[Int, 5] = Array([1, 2, 3, 4, 5])

Please help.
Was this page helpful?