Comments (9)
It's not a bug, it's just how ruby works. You'll get the same results with something like
GemTypes = Dry.Types()
GemTypes::Params::Integer # => returns type, ok
module Types
include GemTypes
# here Params::Integer is still available
# because it's inherited from GemTypes
module Params # this shadows Params of GemTypes
# now Types::Params references to another module, not one defined in GemTypes
end
end
There are a bunch of workarounds you can apply. Simplest:
# don't use inheritence
Types = Dry.Types()
# instead, work with the module created by dry-types directly
module Types
module Params
PageSize = Types::Params::Integer
end
end
Another option is avoiding shadowing:
module Types
include Dry.Types()
Params::PageSize = Params::Integer
end
or
module Types
types = Dry::Types().tap { include _1 }
module types::Params
PageSize = Types::Params::Integer
end
end
or
module Types
include Dry::Types()
module ancestors[1]::Params
PageSize = Types::Params::Integer
end
end
from dry-types.
Wait, this is counter-intuitive. I would expect this to work. Given that Types::Params
works, why re-opening the module does not?
from dry-types.
@solnic because it's not reopening the module but creating a new one. We can't control it. We could change our examples to Types = Dry::Types()
, it'll help somewhat.
from dry-types.
@flash-gordon do you know if it's possible to define regular modules for Param
JSON
etc.? It feels like we're violating POLS here.
from dry-types.
@solnic I'm pretty sure you can subscribe to the included
hook and define modules there. This would be surprising to me OTOH because it has nothing to do with dry-types rather how ruby operates itself. I've once heard of a framework that tries to improve ruby, not sure they really pulled this off. I suggest updating docs to Types = Dry.Types()
and call it a day. Same effect, no magic.
Keep in mind, lexical scoping in ruby is far from being self-evident. For instance, you can't make it work with either approach:
RSpec.describe 'foo' do
include Dry.Types
specify do
Params # boom, no params!
end
end
Oh! I have another suggestion actually! We can detect when the user overrides exported modules and give a proper warning. WDYT? This way we won't bend ruby (with unforeseeable consequences) and provide robust UX.
from dry-types.
I've been bitten by this before too, ended up scratching my head for a bit and then went for one of the workarounds.
I think if we follow this recommendation of @flash-gordon's:
I suggest updating docs to
Types = Dry.Types()
and call it a day. Same effect, no magic.
We'd effectively solve this issue for 99.9% of our users.
Is this the one you're suggesting, @flash-gordon?
# don't use inheritence
Types = Dry.Types()
# instead, work with the module created by dry-types directly
module Types
module Params
PageSize = Types::Params::Integer
end
end
That said, this suggestion does sound good too, if we're able to do it :)
We can detect when the user overrides exported modules and give a proper warning. WDYT?
from dry-types.
Is this the one you're suggesting, @flash-gordon?
Yep.
I think applying both improvements is a good way of solving this.
from dry-types.
Yeah let's just fix it via docs and:
We can detect when the user overrides exported modules and give a proper warning. WDYT?
sounds good to me too.
from dry-types.
hm, it's not possible to detect if there was a constant definition, not without tracepoint API at least, that'd be overkill FWIW https://bugs.ruby-lang.org/issues/17881
from dry-types.
Related Issues (20)
- Possible compatibility issue between dry-struct and dry-monads HOT 3
- Adding bottom type HOT 2
- Include `Types::Params::String` HOT 8
- Type#fallback to mock previous implementation of Type#default HOT 10
- Constructor is not applied properly to the result of types Sum HOT 5
- Types::Params::Integer incorrect coercion for numeric values HOT 9
- Types::Params::*.optional not handling empty string HOT 4
- Anonymous Dry::Types based module cannot be extended HOT 4
- Params coercion for Array works in an unintuitive way HOT 1
- https://dry-rb.org/gems/dry-types/ not at latest version HOT 1
- [Security] Workflow ci.yml is using vulnerable action actions/checkout
- Compound Types with Examples HOT 1
- Dry::Container::Error: There is already an item registered with the key "nominal.string" on jruby-head HOT 4
- Future-proof local override for Types::Instance/Constructor classes that potentially collide with predicates
- Unexpected error raised from Sum type HOT 7
- Types::Coeercible::Integer, should this use Base 10? HOT 2
- Add Zeitwerk autoloader
- Using transform_keys breaks error handling
- Undefined method after upgrading dependencies. HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dry-types.