How to check if a constant is defined in Crystal
Asked Answered
I

1

7

I need to verify if a constant is defined to do a conditional.

I was trying this but "defined" method not exists on this language:

if defined(constant)
  value = :foo
else
  value = :bar
end
Isadora answered 27/8, 2017 at 18:4 Comment(0)
B
7

You can use macro and TypeNode#has_constant?:

FOO = 1

value = nil
{% if @type.has_constant? "FOO" %}
  value = :foo
{% else %}
  value = :bar
{% end %}

pp value #=> :foo

Or even better, you can write a short custom macro for this:

macro toplevel_constant_defined?(c)
  {{ @type.has_constant? c }}
end

pp toplevel_constant_defined? "FOO" # => true
pp toplevel_constant_defined? "BAR" # => false

Note: as mentioned by Jonne Haß, you only ever should need this in advanced macro programming, everywhere else it's a huge code smell, regardless of the language used.

Baronial answered 27/8, 2017 at 19:43 Comment(1)
Though it should be noted that you only ever should need this in advanced macro programming, everywhere else it's a huge code smell, regardless of the language used.Sid

© 2022 - 2024 — McMap. All rights reserved.