red / REP

Red Enhancement Process
BSD 3-Clause "New" or "Revised" License
10 stars 4 forks source link

WISH: `get-quiet` routine or /quiet (or /safe) refinement for `get` action on paths #113

Open hiiamboris opened 2 years ago

hiiamboris commented 2 years ago

I often have to write long code to guard against path errors, e.g.: width: all [layout layout/size layout/size/x]

This is a long known pain point since Rebol with no good solution.

attempt [layout/size/x] is often used in this situation, but it's only acceptable in low-performance code parts (which in my code are usually very few):

>> o: object [x: 2x3 y: none]
>> clock/times [all [o o/a o/a/x] all [o o/b o/b/x]] 1e6
0.81 μs [all [o o/a o/a/x] all [o o/b o/b/x]]
>> clock/times [attempt [o/a/x] attempt [o/b/x]] 1e6
3.98 μs [attempt [o/a/x] attempt [o/b/x]]

I propose adding an efficient get/quiet 'layout/size/x call which if any of the path items are not accessible, instead of erroring out would return none.

greggirwin commented 2 years ago

What about safe as a refinement/routine name? Aligns with do-safe and attempt/safer. There's also a slight mismatch in behavior today, because set does not error when trying to set a word that doesn't exist in a context. Just as do-safe is marked for internal use, get-safe aligns with that as a routine. I can see people wanting to use it regularly though, simply because checking for none seems nicer in many cases than handling errors.

Oldes commented 2 years ago

The object in the example should be: o: object [a: 2x3 b: none] else there would be uncaught error:

>> all [o o/a o/a/x]
*** Script Error: cannot access a in path o/a
hiiamboris commented 2 years ago

Indeed. Copied something else :D