type conversion between alias types

A matrix type is defined as
alias type = DType.float32

struct Matrix[rows: Int, cols: Int]:
var data: DTypePointer[type]

@staticmethod
fn rand() -> Self:
var data = DTypePointer[type].alloc(rows * cols)
rand(data, rows * cols)
return Self(data)
alias type = DType.float32

struct Matrix[rows: Int, cols: Int]:
var data: DTypePointer[type]

@staticmethod
fn rand() -> Self:
var data = DTypePointer[type].alloc(rows * cols)
rand(data, rows * cols)
return Self(data)
Then a Logits type is defined with Matrix with only 1 column:
struct Logits[rows: Int]:
alias LogitsM = Matrix[rows, 1]
var logits: Self.LogitsM
fn __init__(inout self):
self.logits.__init__()
@staticmethod
fn rand() -> Self:
return Self.LogitsM.rand()
struct Logits[rows: Int]:
alias LogitsM = Matrix[rows, 1]
var logits: Self.LogitsM
fn __init__(inout self):
self.logits.__init__()
@staticmethod
fn rand() -> Self:
return Self.LogitsM.rand()
Got compile error:
error: Expression [20]:9:33: cannot implicitly convert 'Matrix[rows, 1]' value to 'Logits[rows]' in return value
return Self.LogitsM.rand()
~~~~~~~~~~~~~~~~~^~
expression failed to parse (no further compiler diagnostics)
error: Expression [20]:9:33: cannot implicitly convert 'Matrix[rows, 1]' value to 'Logits[rows]' in return value
return Self.LogitsM.rand()
~~~~~~~~~~~~~~~~~^~
expression failed to parse (no further compiler diagnostics)
1 Reply
Ryulord
Ryulord5mo ago
LogitsM is a Matrix and you're calling its rand method, which will return a value of type Matrix but the rand method on Logits says it returns a value of type Logits (Self) so you need to convert from Matrix to Logits and it can't do that implicitly. You'll need to write an init method to create Logits from a Matrix
Want results from more Discord servers?
Add your server