Hi,
I am experimenting with Mojo 0.26.3 and I got stuck with my code of struct matrix with a buffer. Could you help me what the issue is with it?
from std.memory import UnsafePointer
comptime RowMajor = "F"
comptime ColMajor = "C"
struct Buffer[T: AnyType, layout: StringLiteral]:
var storage: UnsafePointer[Self.T, _]
var rows: Int
var cols: Int
def __init__(out self, rows: Int, cols: Int):
self.rows = rows
self.cols = cols
self.storage = alloc[Self.T](rows * cols)
def __del__(owned self):
if self.storage:
self.storage.free()
def get_offset(self, i: Int, j: Int) -> Int:
# Indexing is decided here based on the "Buffer type"
comptime if Self.layout == RowMajor:
return i * self.cols + j
else:
return j * self.rows + i
def __getitem__(self, i: Int, j: Int) -> Self.T:
return self.storage.load(self.get_offset(i, j))
def __setitem__(mut self, i: Int, j: Int, val: Self.T):
self.storage.store(self.get_offset(i, j), val)
struct Matrix[T: AnyType, layout: StringLiteral]:
var data: Buffer[Self.T, Self.layout]
def __init__(out self, rows: Int, cols: Int):
self.data = Buffer[Self.T, Self.layout](rows, cols)
def __getitem__(self, i: Int, j: Int) -> Self.T:
return self.data[i, j]
def __setitem__(mut self, i: Int, j: Int, val: Self.T):
self.data[i, j] = val
I get this error:
/home/test_mojo/matrix.mojo:7:31: error: 'UnsafePointer[T, ?]' is not concrete, use '[]' to bind missing parameters
var storage: UnsafePointer[Self.T, _]
^
/home/test_mojo/matrix.mojo:16:23: error: expected ')' in argument list
def __del__(owned self):
^
/home/test_mojo/matrix.mojo:14:37: error: ambiguous call to '__init__'
self.storage = alloc[Self.T](rows * cols)
~~~~~~~~~~~~~^~~~~~~~~~~~~
/home/test_mojo/matrix.mojo:1:1: note: candidate declared here
from std.memory import UnsafePointer
^
/home/test_mojo/matrix.mojo:1:1: note: candidate declared here
from std.memory import UnsafePointer
^
/home/test_mojo/matrix.mojo:28:28: error: no matching method in call to 'load'
return self.storage.load(self.get_offset(i, j))
~~~~~~~~~~~~^~~~~
/home/test_mojo/matrix.mojo:1:1: note: candidate not viable: expected at most 1 positional argument, got 2
from std.memory import UnsafePointer
^
/home/test_mojo/matrix.mojo:1:1: note: candidate not viable: value passed to 'self' cannot be converted from 'UnsafePointer[T, ?]' to 'UnsafePointer[Scalar[dtype], self.origin, address_space=self.address_space]', it depends on an unresolved parameter 'dtype'
from std.memory import UnsafePointer
^
/home/test_mojo/matrix.mojo:1:1: note: candidate not viable: value passed to 'self' cannot be converted from 'UnsafePointer[T, ?]' to 'UnsafePointer[Scalar[dtype], self.origin, address_space=self.address_space]', it depends on an unresolved parameter 'dtype'
from std.memory import UnsafePointer
^
/home/test_mojo/matrix.mojo:31:21: error: no matching method in call to 'store'
self.storage.store(self.get_offset(i, j), val)
~~~~~~~~~~~~^~~~~~
/home/test_mojo/matrix.mojo:1:1: note: candidate not viable: value passed to 'self' cannot be converted from 'UnsafePointer[T, ?]' to 'UnsafePointer[Scalar[dtype], self.origin, address_space=self.address_space]'
from std.memory import UnsafePointer
^
/home/test_mojo/matrix.mojo:1:1: note: candidate not viable: value passed to 'self' cannot be converted from 'UnsafePointer[T, ?]' to 'UnsafePointer[Scalar[dtype], self.origin, address_space=self.address_space]'
from std.memory import UnsafePointer
^
/home/test_mojo/matrix.mojo:1:1: note: candidate not viable: expected at most 2 positional arguments, got 3
from std.memory import UnsafePointer
^
mojo: error: failed to parse the provided Mojo source module
Thank you in advance!