From: "nevans (Nicholas Evans)" Date: 2022-07-04T17:38:47+00:00 Subject: [ruby-core:109136] [Ruby master Feature#18773] deconstruct to receive a range Issue #18773 has been updated by nevans (Nicholas Evans). Oops, I made some mistakes in my "needs disambiguation" examples. Assuming that `pre+post` members will always be returned when present, padding is only needed when the largest pattern with no rest arg is equal to `pre+post`. Which means it isn't necessary if both pre *and* post exist in any clauses. So: ```ruby # needs padding: 6 == 6+0 # deconstruct(3, true, 6, 0) case obj in a,b,c,x,y,z # args:6, rest:false in A,B,C,* # pre: 3, rest:true in A,B,c,d,* # pre: 4, rest:true end # only post args: depends on whether we consolidate no-rest args with post-args, # or simply always count them as pre-args # does need padding: 6 == 0+6 # deconstruct(3, true, 0, 6) # padding not needed: 6 < 6+4 # deconstruct(3, false, 6, 4) case obj in a,b,c,x,y,z # args:6, rest:false in *, X,Y,Z # post:3, rest:true in *, W,X,Y,Z # post:4, rest:true end # doesn't need padding. # deconstruct(3, true, 6, 3) case obj in a,b,c,x,y,z # args:6, rest:false in A,B,C,* # pre: 3, rest:true in *,X,Y,Z # post:3, rest:true end ``` Of course, it would be fine to *always* send padding whether needed or not. But it would be simpler to lean on `Enumerable`. We don't need `deconstruct` to short-circuit based on size and slice the data; that duplicates logic that's already in the pattern match code. Creating the input array using `size`, `to_a`, `first`, and `last` gives us everything that `deconstruct` could. And theoretically, if/when pattern matching can lazily iterate, an enumerator could be instrumented and feed that into a query optimizer, which might know which args are unlikely and which should be fetched eagerly. e.g. no use fetching rest and post args if we only rarely need them: ```ruby case data_scope in Likely, ToSucceed, * # only need first 2 in Unlikely, ToSucceed, *rest, Tail # fetch tail? fetch everything? in Rarity, a,b,c,d,e,f,h,i,j,k,l,m # maybe don't prefetch all 13? end ``` ---------------------------------------- Feature #18773: deconstruct to receive a range https://bugs.ruby-lang.org/issues/18773#change-98277 * Author: kddeisz (Kevin Newton) * Status: Assigned * Priority: Normal * Assignee: ktsj (Kazuki Tsujimoto) ---------------------------------------- Currently when you're pattern matching against a hash pattern, `deconstruct_keys` receives the keys that are being matched. This is really useful for computing expensive hashes. However, when you're pattern matching against an array pattern, you don't receive any information. So if the array is expensive to compute (for instance loading an array of database records), you have no way to bail out. It would be useful to receive a range signifying how many records the pattern is specifying. It would be used like the following: ```ruby class ActiveRecord::Relation def deconstruct(range) (loaded? || range.cover?(count)) ? records : nil end end ``` It needs to be a range and not just a number to handle cases where `*` is used. You would use it like: ```ruby case Person.all in [] "No records" in [person] "Only #{person.name}" else "Multiple people" end ``` In this way, you wouldn't have to load the whole thing into memory to check if it pattern matched. The patch is here: https://github.com/ruby/ruby/pull/5905. -- https://bugs.ruby-lang.org/ Unsubscribe: