Skip to content

default values cannot be lazy evaluated #126

Open
@ojab

Description

@ojab

Describe the bug

Right now default: value is evaluated at the time of declaration, so object is always created. For karafka logger

setting :logger, ::Karafka::Instrumentation::Logger.new

this caused issues with initialization on RO filesystem (1, 2) even if non-default logger was passed.
In waterdrop it's kinda workarounded via

setting(:logger, false) { |logger| logger || Logger.new($stdout, level: Logger::WARN) }

but it's unnecessary complication and with real constructor would be even less readable.

To Reproduce

require 'dry-configurable'

RAISE_EXCEPTION = -> { raise }

class Config
  include Dry::Configurable

  setting :default_failure, default: RAISE_EXCEPTION.call
end

Expected behavior

Some API that would allow to skip default value initialization if non-default one is passed

require 'dry-configurable'

RAISE_EXCEPTION = -> { raise }

class Config
  include Dry::Configurable

  setting :default_failure, default: -> { RAISE_EXCEPTION.call }
end

Config.new(default_failure: 'does not fail')

My environment

  • Affects my production application: Kinda, PR to karafka was needed
  • Ruby version: 3.0.2
  • OS: linux

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions