txt: ## is-20required.md
  is required
  
  Combined from primary sources listed below.
  
  # [In Type system](#___top "go to top of document")[§](#(Type_system)_trait_is_required "direct link")
  
  See primary documentation
  [in context](/language/typesystem#trait_is_required)
  for **trait is required**.
  
  ```raku
  multi trait_mod:<is>(Attribute $attr, :$required!)
  multi trait_mod:<is>(Parameter:D $param, :$required!)
  ```
  
  Marks a class or roles attribute as required. If the attribute is not initialized at object construction time throws [`X::Attribute::Required`](/type/X/Attribute/Required).
  
  ```raku
  class Correct {
      has $.attr is required;
  }
  say Correct.new(attr => 42);
  # OUTPUT: «Correct.new(attr => 42)␤»
  
  class C {
      has $.attr is required;
  }
  C.new;
  CATCH { default { say .^name => .Str } }
  # OUTPUT: «X::Attribute::Required => The attribute '$!attr' is required, but you did not provide a value for it.␤»
  ```
  
  Note a class with a private attribute will give the same error:
  
  ```raku
  class D {
      has $!attr is required;
  }
  D.new;
  CATCH { default { say .^name => .Str } }
  # OUTPUT: «X::Attribute::Required => The attribute '$!attr' is required, but you did not provide a value for it.␤»
  ```
  
  You can provide a reason why it's required as an argument to `is required`
  
  ```raku
  class Correct {
      has $.attr is required("it's so cool")
  };
  say Correct.new();
  # OUTPUT: «The attribute '$!attr' is required because it's so cool,␤but you did not provide a value for it.␤»
  
  ```
  