Return compile time constant as runtime value

I have got the following struct:

struct Table[dtype: DType=DType.float32]:
var data: UnsafePointer[Scalar[dtype]]
var rows: Int
var cols: Int
#var data_type: DType

fn __init__(out self, rows: Int, cols: Int):
    self.rows = rows
    self.cols = cols
    #self.data_type = dtype
    self.data = UnsafePointer[Scalar[dtype]].alloc(rows * cols)
    
fn datatype(self) -> DType:
    return dtype
    
fn main():
    
    tensor1 = Tensor[DType.uint8](10, 10)
	print(tensor1.datatype())

This call to “datatype” method - works fine in same file - it is not recognized if this file is used outside! How can I solve this?

Welcome. The Modular forum has the ability to display code in mojo.

If you use three “ticks” and the word “mojo”, it will display your code correctly. Currently it is seeing the fn and then putting only that part in a code block.

For example:

```mojo
<Your code here>
```

returns

struct Table[dtype: DType=DType.float32]:
var data: UnsafePointer[Scalar[dtype]]
var rows: Int
var cols: Int
#var data_type: DType

fn __init__(out self, rows: Int, cols: Int):
    self.rows = rows
    self.cols = cols
    #self.data_type = dtype
    self.data = UnsafePointer[Scalar[dtype]].alloc(rows * cols)
    
fn datatype(self) -> DType:
    return dtype
    
fn main():
    
    tensor1 = Tensor[DType.uint8](10, 10)
	print(tensor1.datatype())

Apologies for not having the answer to your code question.

struct Table[dtype: DType=DType.float32]:
    var data: UnsafePointer[Scalar[dtype]]
    var rows: Int
    var cols: Int
    # var data_type: DType

fn __init__(out self, rows: Int, cols: Int):
    self.rows = rows
    self.cols = cols
    # self.data_type = dtype
    self.data = UnsafePointer[Scalar[dtype]].alloc(rows * cols)
    
fn datatype(self) -> DType:
    return dtype
    
fn main():
    tensor1 = Tensor[DType.uint8](10, 10)
    print(tensor1.datatype())
1 Like

Thanks for the reply. I posted it again as a comment.

Given the following struct - The datatype function works fine in the same file.

Calling datatype() function from other files - does not reconize it! Why?

struct Tensor[dtype: DType=DType.float32]:
    var data: UnsafePointer[Scalar[dtype]]
    var rows: Int
    var cols: Int
    var data_type: DType

    fn __init__(out self, rows: Int, cols: Int):
        self.rows = rows
        self.cols = cols
        self.data_type = dtype
        self.data = UnsafePointer[Scalar[dtype]].alloc(rows * cols)

    fn datatype(self) -> DType:
        return self.data_type

fn main():
    print("In main")
    tensor1 = Tensor[DType.uint8](10, 10)
    print(tensor1.datatype()) #Works

#Separate file caller.mojo

from tensor import Tensor

fn main():
    #tensor1 = Tensor(2, 3)
    tensor1 = Tensor[DType.float32](2, 3)
    print(tensor1.datatype())# This does not work!
    

Please help!

That should work, what error is the compiler giving you?

Which version are you on? It works fine on e.g. mojo 25.3.0.dev2025042205 (d893abbe).

And a little tip: you can edit your post.

The error when called from another file caller.mojo - I get is:

/tmp/checkout/caller.mojo:6:18: error: 'Tensor[float32]' value has no attribute 'datatype'
    print(tensor1.datatype())# This does not work!
mojo: error: failed to parse the provided Mojo source module

In the same file it does work.

My mojo version is latest:

> mojo --version
mojo 25.3.0.dev2025042205 (d893abbe)

My mojo version is: mojo 25.3.0.dev2025042205 (d893abbe)
It does work in the same file - but not when ‘Tensor’ struct is imported and called from another file(caller.mojo) - it throws up.

This may be an import order issue, since tensor is a namespace built into Mojo. Try renaming the file and the import

You are absolutely correct! Renaming the file - did fix the issue.

Thanks a lot.