help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: pcase and minus-sign


From: Andreas Röhler
Subject: Re: pcase and minus-sign
Date: Wed, 30 Nov 2016 16:23:57 +0100
User-agent: Mozilla/5.0 (X11; Linux i686; rv:45.0) Gecko/20100101 Icedove/45.4.0



On 30.11.2016 14:56, Joost Kremers wrote:

On Wed, Nov 30 2016, Andreas Röhler wrote:
Thanks, that helps. Seems it relates to the following in docstring:

SYMBOL    matches anything and binds it to SYMBOL.

Yup.

Now if I use some arbitrary char, like "a",

(defun foo (arg)
   (interactive "P")
   (pcase arg
     (a (message "%s" "ARG was `a'"))
     (1 (message "%s" "ARG was `1'"))
     ('- (message "%s" "ARG was minus-sign"))
     (_ (message "%s" "ARG not minus-sign"))))

It picks that a-branch at any case - as documented but strange.

I don't think it's strange, TBH. `a' is essentially a let-bound variable. Having the ability to bind values is extremely useful in pcase, I'd say.

It may be a bit surprising that - functions the same way, but it's not strange once you realise that - is not a special character in Lisp (unlike most other programming languages). For example, the following code works fine:

(let ((- "hi"))
 (message "%s" -))

Note BTW that the same if true for the underscore: in your example, _ is also just a symbol used to let-bind some value. The only special thing about _ is that the byte-compiler doesn't complain about an unused variable if its name starts with an underscore. But again, the following code works just fine:

(let ((_ "hi"))
 (message "%s" _))




Hmm, thanks all.

But what make the char `a' so special WRT char `1'?:

(defun foo (arg)
  (interactive "P")
  (pcase arg
    (1 (message "%s" "ARG was `1'"))
    (a (message "%s" "ARG was `a'"))
    ('- (message "%s" "ARG was minus-sign"))
    (_ (message "%s" "ARG not minus-sign"))))



reply via email to

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