Why is get(dict,value,default) evaluated eagerly?

16 views
Skip to first unread message

Lifepillar

unread,
May 21, 2020, 12:05:28 PM5/21/20
to vim...@googlegroups.com
I have always believed that, in get(X,y,z), z would be evaluated only
when dictionary X does not contain key y. Today, to my surprise, I have
discovered that it is not the case:

fun! Loop()
call Loop()
endf

let H = { 'a': 1 }

if has_key(H, 'a') || Loop() " Fine, this is evaluated lazily
endif

echo (get(H, 'a') != 0) ? "" : Loop() " Fine, lazy

" But, endless recursion here:
echo get(H, 'a', Loop())

What is the rationale for this behaviour?

Thanks,
Life.

Tony Mechelynck

unread,
May 21, 2020, 12:53:18 PM5/21/20
to vim_use
IIUC, any expression in the arguments of a function (other than a
variable of type List, Dictionary, Funcref or Blob) is passed "by
value" after having been evaluated by the caller. In get(H, a, Loop())
the third argument passed is the _value_ of Loop() as evaluated
immediately before the call.

Best regards,
Tony.

Lifepillar

unread,
May 21, 2020, 1:23:52 PM5/21/20
to vim...@googlegroups.com
Ah ok, that makes sense.

Thanks!
Life.

Reply all
Reply to author
Forward
0 new messages