6 Replies
Trying to create a
Secret
class in Mojo such that if EC
is greater than MEC
, i.e. expose_secret()
method has been called too many times, then it raises a compile time error. Sadly, I cannot really use Mojo ListLiteral
or Tuple
for this as I am returing a Tuple
with my Secret[T, MEC, EC+1]
type therein but I cannot retrieve it.
@Moderator
Error:
For clarity, which Mojo version are you running this under?
As the error message suggests, it does look like due to the definition of
ListLiteral
that Secret
or whatever is being carried has to be register passable, so I went ahead and changed the Secret
struct. This might be wildly contrary to what you initially wanted, but it does look like in order to use a heterogenous collection, the types have to be register passable: including ListLiteral
and Tuple
.
You can see (along with my great variable naming here) that the following code does not compile:
However, if you remove one of the first my_secret
s and adjust the names appropriately, it will compile. So I think this is closer to fulfilling your goal; correct me if this is wrong though.
Side note: It is strange that in the last .get
call, you can change the EC and MEC type params in the Secret[Int, ...]
part to be basically anything, and the compiler doesn't complain. However, if you change the initial type from Int
to something else, it complains. I guess this might make sense considering the result type of the pesky MLIR operation kgen.pack.get
cares about the types and might handle the value parameters differently, but it's still quite interesting.
It seems to produce the same errors on the most recent, 0.6.0 (d55c0025)Hi @Alex Kirchhoff, this is on Mojo 0.6.0.
Issue is created: https://github.com/modularml/mojo/issues/1397
GitHub
[Modular CLI]: · Issue #1397 · modularml/mojo
Issue description A method with parameter owned self declared does not actually 'consume' self, i.e. self is still usable after it has been 'consumed'. Steps to reproduce Code snipp...
I responded in the issue, but the direct cause of the error on the
get
line is because Secret
is not register-passable, but Tuple
only works on register-passable types at the moment. And given the purpose of Secret
, you wouldn't want it to be register-passable, so your best shot is to avoid Tuple
for now and make your own similar type.