(solved) Problems with algebraic data types (enum)

Hey guys,

I’m trying to use an algebraic data type (in order to act like enum in solidity),
but I’m lost in the docs and can’t figure out how to make it work.

In solidity I have this:

enum SwapStatus { INVALID, ACTIVE, REFUNDED, WITHDRAWN, EXPIRED }

and I’m using it here:

 struct LockContract {
   SwapStatus status;
}

I constructed this:

 contract Test =

   datatype status('a, 'b, 'c, 'd) = Active('a) | Withdrawn('b) | Refunded('c) | Expired('d)
   
   record lock_contract = {
      id: int, 
      status: status(Active()) }

   let new_contract : lock_contract = {
      id = 6,
      status = (Active) }

   entrypoint get_active(x : status('a, 'b, 'c, 'd)) : option('a) =
      switch(x)
         Active(x)    => Some(x)
         Withdrawn(_)   => None
         Refunded(_)   => None
         Expired(_)   => None

How am I supposed to initialize the get_active state and then check what the contracts state is

@philipp.chain , any ideas?

Thanks in advance :smile:

Hello, is there a specific reason you give each of your possible Enum-Values an argument, but don’t use it?

you define: Active('a) where 'a is a generic typed argument, but then you initialize as Active()/(Active) without passing any argument there.

When you don’t need arguments you can simplify your code to be e.g. like this:

 contract Test =
  datatype status = Active | Withdrawn | Refunded | Expired
   
  record lock_contract = { id : int, status : status }
      
  entrypoint test() =
    let new_contract : lock_contract = { id = 6, status = Active }
    is_active(new_contract.status)

  entrypoint is_active(x : status) : bool =
    switch(x)
      Active => true
      _ => false
1 Like

Thank you very much!

It’s actually amazingly easy then. When reading the documentation examples I got extremely lost, if you put there what you just gave me as an example I think it would help people a lot more. :smiley: