Dry-Monads: Extend `Failure` Constructor

Is it possible to extend Failure with option trace?

def Failure(value = Undefined, trace: RightBiased::Left.trace_caller, &block)
    v = Undefined.default(value, block || Unit)
    Failure.new(v, trace)
end

Here is what I want to do.
I’m creating a gem for coding standard(style) with dry-monads, and use it in my project with my team members. The problem is when unexpected error occurs (for example: syntax error), the failure always trace back to my gem’s base class, not the place where it really appeared. This situation occurs because I try to catch standard error and pass it to Failure. So I want to customize trace string in Failure constructor.

If the idea is accepted, I can open a PR in the repo.

1 Like

I’m pretty sure it will break everything because of how keywords work in ruby. Ruby will start treating value as keywords when value is a hash, smth like that. I suggest adding a separate module with constructors that work as you need:

module ShinyGem
  module Monads
    include ::Dry::Monads[:result]

    def Failure(value = Undefined, trace: ...)
      ...
      Failure.new(v, trace)
1 Like

Thanks for your reply.
I’ll take that solution! :grinning: