module Dry::Types::Builder

def default(input = Undefined, options = EMPTY_HASH, &block)

Other tags:
    Api: - public

Returns:
  • (Default) -

Raises:
  • (ConstraintError) -

Parameters:
  • block (#call, nil) --
  • options (Hash) --
  • input (Object) --
def default(input = Undefined, options = EMPTY_HASH, &block)
  unless input.frozen? || options[:shared]
    where = Dry::Core::Deprecations::STACK.()
    Dry::Core::Deprecations.warn(
      "#{input.inspect} is mutable."\
      ' Be careful: types will return the same instance of the default'\
      ' value every time. Call `.freeze` when setting the default'\
      ' or pass `shared: true` to discard this warning.'\
      "\n#{where}",
      tag: :'dry-types'
    )
  end
  value = input.equal?(Undefined) ? block : input
  if value.respond_to?(:call) || valid?(value)
    Default[value].new(self, value)
  else
    raise ConstraintError.new("default value #{value.inspect} violates constraints", value)
  end
end