_ and precedence

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

_ and precedence

Stéphane Ducasse
Hi 

Doig the XP for magic literals I found

Symbol >> precedence
"Answer the receiver's precedence, assuming it is a valid Smalltalk
message selector or 0 otherwise.  The numbers are 1 for unary,
2 for binary and 3 for keyword selectors."

self size = 0 ifTrue: [^ 0].
(self first isLetter or: [ self first = $_ ]) ifFalse: [^ 2].
self last = $: ifTrue: [^ 3].
^ 1



#'_foo' precedence.
"1"
#'_+' precedence.
"1"
#'+' precedence. 
"2"
#'foo' precedence.
"1"
#'foo:' precedence.
  "3"
#'_foo:' precedence. 
“3"

And I wonder why we are concerned with _

S. 

--------------------------------------------
Stéphane Ducasse
03 59 35 87 52
Assistant: Julie Jonas 
FAX 03 59 57 78 50
TEL 03 59 35 86 16
S. Ducasse - Inria
40, avenue Halley, 
Parc Scientifique de la Haute Borne, Bât.A, Park Plaza
Villeneuve d'Ascq 59650
France

Reply | Threaded
Open this post in threaded view
|

Re: _ and precedence

Nicolas Anquetil


isn't it historical ?
it used to mean :=, no?

nicolas

On Mon, 2020-04-27 at 14:51 +0200, Stéphane Ducasse wrote:

> Hi
>
> Doig the XP for magic literals I found
>
> Symbol >> precedence
> "Answer the receiver's precedence, assuming it is a valid
> Smalltalk
> message selector or 0 otherwise.  The numbers are 1 for unary,
> 2 for binary and 3 for keyword selectors."
>
> self size = 0 ifTrue: [^ 0].
> (self first isLetter or: [ self first = $_ ]) ifFalse: [^ 2].
> self last = $: ifTrue: [^ 3].
> ^ 1
>
>
>
> #'_foo' precedence.
> "1"
> #'_+' precedence.
> "1"
> #'+' precedence.
> "2"
> #'foo' precedence.
> "1"
> #'foo:' precedence.
>   "3"
> #'_foo:' precedence.
> “3"
>
> And I wonder why we are concerned with _
>
> S.
>
> --------------------------------------------
> Stéphane Ducasse
> http://stephane.ducasse.free.fr / http://www.pharo.org 
> 03 59 35 87 52
> Assistant: Julie Jonas
> FAX 03 59 57 78 50
> TEL 03 59 35 86 16
> S. Ducasse - Inria
> 40, avenue Halley,
> Parc Scientifique de la Haute Borne, Bât.A, Park Plaza
> Villeneuve d'Ascq 59650
> France
>
--
Nicolas Anquetil
RMod team -- Inria Lille


Reply | Threaded
Open this post in threaded view
|

Re: _ and precedence

John Brant-2
In reply to this post by Stéphane Ducasse
On 4/27/20 7:51 AM, Stéphane Ducasse wrote:

> Hi
>
> Doig the XP for magic literals I found
>
> Symbol >> precedence
> "Answer the receiver's precedence, assuming it is a valid Smalltalk
> message selector or 0 otherwise.  The numbers are 1 for unary,
> 2 for binary and 3 for keyword selectors."
>
> self size = 0 ifTrue: [^ 0].
> (self first isLetter or: [ self first = $_ ]) ifFalse: [^ 2].
> self last = $: ifTrue: [^ 3].
> ^ 1
>
>
>
> #'_foo' precedence.
> "1"
> #'_+' precedence.
> "1"
> #'+' precedence.
> "2"
> #'foo' precedence.
> "1"
> #'foo:' precedence.
> "3"
> #'_foo:' precedence.
> “3"
>
> And I wonder why we are concerned with _

Underscores and letters are treated the same by the Smalltalk grammar.
Unary, keyword and variables can all begin with letters or underscores.
"_" is a valid variable and unary message name. As for your tests, you
would get the same results if you replaced $_ with $a -- "#'a+'
precedence = 1".


John Brant