Thursday, May 21, 2020

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

On 2020-05-21, Tony Mechelynck <antoine.mechelynck@gmail.com> wrote:
> On Thu, May 21, 2020 at 6:05 PM Lifepillar <lifepillar@lifepillar.me> wrote:
>>
>> 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.
>
> 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.

Ah ok, that makes sense.

Thanks!
Life.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/ra6deq%242sfu%242%40ciao.gmane.io.

No comments: