emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: combining cond and let, to replace pcase.


From: Stefan Monnier
Subject: Re: combining cond and let, to replace pcase.
Date: Mon, 27 Nov 2023 18:12:11 -0500
User-agent: Gnus/5.13 (Gnus v5.13)

>> >   > >        ;; Like above but always falls thru to next clause.
>> >   > >        (:match (`(expt ,foo ,bar) x))
>> >   > >        ;; Bindings continue in effect.
>> >
>> >   > What happens if `(car x)` is not equal to `expt` or if `x` is a string?
>> >
>> > This form of clause should bind the variables unconditionally.  If
>> > matching provides a value to give a certain variable, the variable
>> > should get that value.  Otherwise it should be bound to nil.
>>
>> So these form of :match clause would bind variables even though it did
>> not match?  That seems counter-intuitive to me.
>
> Not only that, it’s just plain wrong.
>
>     (pcase 42
>       (`(,x . ,y) (format "%s:%s" x y))
>       (_ "not a cons cell"))
>     → not a cons cell

But those above `:match` is not a test, it's a destructuring operation
only, so it's more like

    (pcase-let ((`(,x . ,y) 42))
      ...)

> If I understand Richard correctly, the corresponding :match will
> unconditionally attempt to bind x to (car 42) and y to (cdr 42), which
> results in a run-time error.

`pcase` uses `car-safe` and `cdr-safe` tho it was not to avoid such
errors (it was to allow those operations to be optimized away if
the result is not used).

`:match` could similarly use `car-safe` and `cdr-safe`.


        Stefan




reply via email to

[Prev in Thread] Current Thread [Next in Thread]