Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

default values cannot be lazy evaluated #126

Open
ojab opened this issue Sep 14, 2021 · 0 comments
Open

default values cannot be lazy evaluated #126

ojab opened this issue Sep 14, 2021 · 0 comments

Comments

@ojab
Copy link
Contributor

ojab commented Sep 14, 2021

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant