Skip to content

Possible ideal use case for refinements? #292

@lazyatom

Description

@lazyatom

We found this library as a dependency of another library (snowplow/snowplow-ruby-tracker) that we use in our application. Unfortunately, we also have our own top-level class called Contract, which basically means we have to fork our dependency to remove this gem in order to get things working again. Not great!

If I was in control of everything (the world!), I'd probably consider reworking this gem to use method calls rather than global constants to do its work, so that the external surface area of the API is as small as possible e.g.

class Something
  include Contracts::Behaviour

  contract Double => Double # contract is a method from `Contracts::Behaviour`, not a constant
  def wem(x); x * 2; end

  contract array_of(Hash) => nil # array_of is a method from `Contracts::Behaviour`, not a constant
  def wibble(args); ...; end
end

... however, that's obviously a massive change and I'm not familiar enough with this gem to spot any obvious gotchas, of which there might be many.

However, one way to keep the usage of the gem the same, but not prevent dependencies at any level from defining a Contract class, might be to make these available as refinements that can be explicitly opted-in for classes that want to use verification.

class Something
  using Contracts::Behaviour

  Contract Double => Double # same API
  def wem(x); x * 2; end

  Contract ArrayOf[Hash] => nil # same API
  def wibble(args); ...; end
end

class OtherThing
  def foo; Contract.first; end # uses existing app-specific `Contract` constant 
end

Is this something you'd be interested in?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions