Question on enum predicate methods and the syntax

Hello, I am trying to use the below Crystal syntax in Marten Model classes, it works for one and failed for the other ones, I am trying to understand why it failed and learn. The LLM gave some reasons but I could not trust it 100% and hence not convinced.

enum MembershipRole
Admin = 1
Member = 2
end

#Works:

def is_admin? : Bool
role == MembershipRole::Admin
end

When it was just role.admin? or role&.admin? || false it fails:

role.admin?
^-----
Error: undefined method 'admin?' for Nil (compile-time type is (MembershipRole | Nil))

role&.admin? || false
^
Error: unexpected token: "."

I mean how can it be Nil, when I am mentioning null: false in the model field? Even if nil, why did the other one fail? Any other approaches that are idiomatic or the one explicit one is better?

It seems to be valid syntax from the below link. Thanks.

I think the gist of it is because of what Marten generates given the model:

enum MembershipRole
  Admin  = 1
  Member = 2
end

class Membership < Marten::Model
  field :id, :big_int, primary_key: true, auto: true
  field :role, :enum, values: MembershipRole, null: false
end

which expands to something along the lines of:

class Membership
  register_field(Marten::DB::Field::Enum.new("role", values: MembershipRole, null: false, enum_class: MembershipRole))

  @role : String?              # raw DB value, e.g. "admin"
  @enum_role : MembershipRole? # parsed and memoized enum

  def raw_role : String?
    @role
  end

  def raw_role!
    @role.not_nil!
  end

  # Nilable, whatever `null:` says
  def role : MembershipRole?
    @enum_role ||= if !raw_role.nil?
                     MembershipRole.parse(raw_role!)
                   end
  end

  def role!
    role.not_nil!
  end

  # Presence check (not nil and not empty). This is NOT an enum predicate.
  def role?
    raw_role?
  end

  def role=(value : MembershipRole?)
    # sets both @role and @enum_role (or nils both)
  end

  def role=(value : String?)
    # same, but parses the string
  end
end

So when you call #role it’s using the nilable getter for that property, which means you ofc have to handle nil value. null: false in model definition is, best i can tell, only used for validations and migrations.

Easiest workaround would be to do role!.admin? and not role.admin? so it uses the non-nilable getter. Otherwise you could handle nil via role.try &.admin? || false. #== works because it’s possible to comparisons against nil values, but i’d suggest using one of the other options.

Any reasons to prefer the other ones over this? Like efficiency, performance, etc.
As the #== seems simple and easy to understand.

I also came across the other syntax foo.try(&bar) but that requires || false.

role!.admin? is what i’d go with personally. Mainly for the type safety you get. E.g. if you typo something you’d get a compile time error saying a #admin? method doesn’t exist vs just silently return false on the comparison. There are some other gotchas with #== that aren’t relevant for this use case, but .admin? is just more idiomatic.

Also, there’s no &. operator in Crystal. We use the Object#try(&) method instead.