Class: NRSER::Types::Where
Overview
Where instances are predicate functions¹ as a type.
They have a #predicate block, and #test? calls it with values and returns the boolean of the result (double-bangs it ‘!!`).
Super simple, right? And easy! Why don’t we just use these things all over the place?
If you’re been around the programing block a few times, you probably saw this coming a mile away: you should avoid using them.
Yeah, sorry. Here’s the reasons:
-
They’re opaque - it’s hard to see inside a Proc… even if you got the source code (which seems like it requires gems and involves some amount of hackery), that wouldn’t really give you the whole picture because you need to look at the binding as well… Ruby procs capture their entire environment.
Essentially, they suck to easily and/or comprehensively communicate what they hell they do.
-
Like When, they’re totally Ruby-centric… we can’t really serialize them and pass them off anywhere, so they’re shitty for APIs and property types and stuff that you may want or need to expose outside the runtime.
In this sense they’re ok as implementations of types like file_path that represent an idea to be communicated to the outside world, where each system that handles that idea will need to have it’s own implementation of it.
Lit addresses a lot of this with serializable functions, but that’s nowhere near ready to rock, and support for it would probably be added along side Where, not in place of it (since Where is probably still going to be used and useful).
So please be aware of those, and be reasonable about your Wheres.
> ¹ I say functions, because they really should be functions (same > input always gets same output, pure, etc.). > > Yeah, there’s not much stopping you from making them state-based or > random or whatever, but please don’t do that shit unless you’ve really > thought it through. And if you still do, please write me and tell me > what you thought and why it’s a reasonable idea and I’ll update this.
Instance Attribute Summary collapse
-
#predicate ⇒ Proc<(V) => Boolean>
readonly
Predicate Proc used to test value for membership.
Instance Method Summary collapse
-
#explain ⇒ String
A string that is supposed to give our best concise description of the type.
-
#initialize(method = nil, **options, &block) ⇒ Where
constructor
Make a new Where.
-
#test?(value) ⇒ Boolean
Test a value for membership.
Methods inherited from Type
#===, #builtin_inspect, #check, #check!, #default_name, #default_symbolic, #from_data, #from_s, #has_from_data?, #has_from_s?, #has_to_data?, #inspect, #intersection, #name, #not, #respond_to?, #symbolic, #test, #to_data, #to_proc, #to_s, #union, #xor
Constructor Details
#initialize(method, **options) ⇒ Where #initialize(**options, &block) ⇒ Where
Documentation and examples are indented to illustrate behavior and aid in development. Please use the factory method Types.where to create instances - they allow us to easily improve and optimize
Make a new NRSER::Types::Where.
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/nrser/types/where.rb', line 123 def initialize method = nil, **, &block # Check up on what we got if method && block raise NRSER::ArgumentError.new \ "Can't supply both method", method, "(first arg)", "and &block", block elsif !method && !block raise NRSER::ArgumentError.new \ "Must provide either a Method<(Object)=>Boolean> as the first argument", "*or* a Proc<(Object)=>Boolean> as the block" end @predicate = method || block unless predicate.arity == 1 raise NRSER::ArgumentError.new \ "{NRSER::Types::Where} predicates must have arity 1", predicate: predicate, arity: predicate.arity, options: end unless [:name] if predicate.is_a?( Method ) [:name] = predicate.full_name else raise NRSER::ArgumentError.new \ "`name:` keyword argument is required when creating {Where}", "from `&block` predicates." end end super ** end |
Instance Attribute Details
#predicate ⇒ Proc<(V) => Boolean> (readonly)
Predicate Proc used to test value for membership.
75 76 77 |
# File 'lib/nrser/types/where.rb', line 75 def predicate @predicate end |
Instance Method Details
#explain ⇒ String
A string that is supposed to give our best concise description of the type.
NRSER::Types::Where sucks because we can’t really do much here.
180 181 182 |
# File 'lib/nrser/types/where.rb', line 180 def explain "#{ self.class.demod_name }<#{ @name }>" end |
#test?(value) ⇒ Boolean
Test a value for membership.
169 170 171 |
# File 'lib/nrser/types/where.rb', line 169 def test? value !!@predicate.call( value ) end |