From e55a325abb0972564498f87955b3d3a4cf3d8c5e Mon Sep 17 00:00:00 2001 From: Jeremy Woertink Date: Sat, 15 Jun 2024 14:30:42 -0700 Subject: [PATCH] Add a catch for Nilable ProcNotations. Fixes #90 (#91) --- spec/habitat_spec.cr | 2 +- src/habitat.cr | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/spec/habitat_spec.cr b/spec/habitat_spec.cr index 18929ad..445633f 100644 --- a/spec/habitat_spec.cr +++ b/spec/habitat_spec.cr @@ -141,7 +141,7 @@ end class ASettingForEverything alias Dot = Int32 Habitat.create do - setting proc_notation : String -> Nil + setting proc_notation : (String -> Nil)? setting alias_setting : Dot = 3 setting point : Point = Point.new(x: 3, y: 4) setting mod : SuperMod diff --git a/src/habitat.cr b/src/habitat.cr index a80fcda..1d3b467 100644 --- a/src/habitat.cr +++ b/src/habitat.cr @@ -34,7 +34,7 @@ class Habitat {% for type in TYPES_WITH_HABITAT %} {% for setting in type.constant(:HABITAT_SETTINGS) %} {% if !setting[:decl].type.is_a?(Union) || - (setting[:decl].type.is_a?(Union) && !setting[:decl].type.types.any?(&.names.includes?(Nil.id))) %} + (setting[:decl].type.is_a?(Union) && !setting[:decl].type.types.any? { |t| t.is_a?(ProcNotation) ? false : t.names.includes?(Nil.id) }) %} if {{ type }}.settings.{{ setting[:decl].var }}?.nil? raise MissingSettingError.new {{ type }}, setting_name: {{ setting[:decl].var.stringify }}, example: {{ setting[:example] }} end @@ -231,7 +231,7 @@ class Habitat # NOTE: We can't use the macro level `type.resolve.nilable?` here because # there's a few declaration types that don't respond to it which would make the logic # more complex. Metaclass, and Proc types are the main, but there may be more. - {% if decl.type.is_a?(Union) && decl.type.types.any?(&.names.includes?(Nil.id)) %} + {% if decl.type.is_a?(Union) && decl.type.types.any? { |t| t.is_a?(ProcNotation) ? false : t.names.includes?(Nil.id) } %} {% nilable = true %} {% else %} {% nilable = false %}