M
Modular14mo ago
Aziz

Check types

How to check a type of a variable?
type(x)
type(x)
raises an error
isinstance(1, Int)
isinstance(1, Int)
also raises an error I was only able to print the types using:
struct print_type:
fn __init__(inout self, type:Int):
print("Int")
fn __init__(inout self, type:Float64):
print("Float64")
# ...
struct print_type:
fn __init__(inout self, type:Int):
print("Int")
fn __init__(inout self, type:Float64):
print("Float64")
# ...
However it seems to be not possible to return the type instead of just printing it
struct print_type:
fn __init__(inout self, type:Int) -> AnyType:
print("Int")
return Int. # Error
struct print_type:
fn __init__(inout self, type:Int) -> AnyType:
print("Int")
return Int. # Error
No description
6 Replies
mad alex 1997
mad alex 199714mo ago
for your struct type is the variable you are passing in so you would have to call that if you want it to return from your type printing struct. If you are trying to return the Int type so you can use it somewhere else that won't work as types have to be compile time static. AnyType is a type which can be used as a placeholder for different types but it does not encompass types themselves as returns. I think the thing you are trying to do is blocked by traits.
Aziz
AzizOP14mo ago
I see
Aziz
AzizOP14mo ago
I am looking for a direct way to do somthing like this
No description
Aziz
AzizOP14mo ago
which is in python:
if type(1) == int: print("Handle Integer")
if type(1) == int: print("Handle Integer")
mad alex 1997
mad alex 199714mo ago
Until we get enums and/or the ability to create generic types you literally just have to write overloads for every type of input you want. However you can use the fact that all the scalar types are simd and simd's have casting ability for example.
@always_inline
fn __imul__(inout self, rhs: SIMD[dtype,1])raises:
self.iarithmatic_Array_SIMD[SIMD.__mul__](rhs)
@always_inline
fn __imul__(inout self, rhs: SIMD[dtype,1])raises:
self.iarithmatic_Array_SIMD[SIMD.__mul__](rhs)
when inside of a struct that defines dtype will cast any numerical value (except bools and maybe index) to the dtype specified on creation of the struct. For the purpose of being able to a*=b where a is an has a defined type and b is any scalar numeric.
Aziz
AzizOP14mo ago
I see. Thanks
Want results from more Discord servers?
Add your server