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

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Lifepillar
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.

--
--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/ra68ro%241f42%241%40ciao.gmane.io.
Reply | Threaded
Open this post in threaded view
|

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

Tony Mechelynck
On Thu, May 21, 2020 at 6:05 PM Lifepillar <[hidden email]> 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.

Best regards,
Tony.

--
--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/CAJkCKXvw8NBTSQY3%3D_D4%3DY5HN%2BS5PqD9UKdr_D%2B%3DhtZW184uxg%40mail.gmail.com.
Reply | Threaded
Open this post in threaded view
|

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

Lifepillar
On 2020-05-21, Tony Mechelynck <[hidden email]> wrote:

> On Thu, May 21, 2020 at 6:05 PM Lifepillar <[hidden email]> 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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/ra6deq%242sfu%242%40ciao.gmane.io.