# Question on enum predicate methods and the syntax

**URL:** <https://forum.crystal-lang.org/t/question-on-enum-predicate-methods-and-the-syntax/9257>\
**Category:** Help & Support\
**Created:** [November 2, 2026, 4:50pm UTC](https://forum.crystal-lang.org/t/question-on-enum-predicate-methods-and-the-syntax/9257 "2026-11-02T16:50:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mzs114](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@mzs114](https://forum.crystal-lang.org/u/mzs114)\
**Post date:** [November 2, 2026, 4:50pm UTC](https://forum.crystal-lang.org/t/question-on-enum-predicate-methods-and-the-syntax/9257/1 "2026-11-02T16:50:58Z")

</div>

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.

```crystal
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.

> **[Enums - Crystal](https://crystal-lang.org/reference/1.22/syntax_and_semantics/enum.html)**

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [November 2, 2026, 5:10pm UTC](https://forum.crystal-lang.org/t/question-on-enum-predicate-methods-and-the-syntax/9257/2 "2026-11-02T17:10:18Z")

</div>

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

```auto
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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![mzs114](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@mzs114](https://forum.crystal-lang.org/u/mzs114)\
**Post date:** [November 2, 2026, 5:19pm UTC](https://forum.crystal-lang.org/t/question-on-enum-predicate-methods-and-the-syntax/9257/3 "2026-11-02T17:19:04Z")

</div>

> [@Blacksmoke16](#):
>
> `#==` 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`.

> <https://github.com/crystal-lang/crystal/issues/2177>
>
> Hello, I found in last \[ruby version (2.3.0) \](https://www.ruby-lang.org/en/news…/2015/12/25/ruby-2-3-0-released/) "lonely operator" implementation. I found it very useful. 
> 
> Can it be implemented in Crystal?

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [November 2, 2026, 5:34pm UTC](https://forum.crystal-lang.org/t/question-on-enum-predicate-methods-and-the-syntax/9257/4 "2026-11-02T17:34:58Z")

</div>

> [@mzs114](#):
>
> Any reasons to prefer the other ones over this? Like efficiency, performance, etc.  
> As the `#==` seems simple and easy to understand.

`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.

---

<div class="post-metadata">

**Author:** ![Sija](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/sija/32/737_2.png) [@Sija](https://forum.crystal-lang.org/u/Sija)\
**Post date:** [November 2, 2026, 7:34pm UTC](https://forum.crystal-lang.org/t/question-on-enum-predicate-methods-and-the-syntax/9257/5 "2026-11-02T19:34:48Z")

</div>

Also, there’s no `&.` operator in Crystal. We use the [`Object#try(&)`](https://crystal-lang.org/api/Object.html#try(&)-instance-method) method instead.
