argument of ifNotNil: must be a 0-argument block

Mathieu mathk.sue at gmail.com
Fri Sep 1 23:57:20 UTC 2006


Zulq Alam a écrit :
> Hi List,
> 
> Why does "Object new ifNotNil: [:object | ]" result in the syntax error
> "argument of ifNotNil: must be a 0-argument block" when the code appears
> to be fine with this?
> 
> 
> ProtoObject>>ifNotNil: ifNotNilBlock
>     "Evaluate the block, unless I'm == nil (q.v.)"
> 
>     ^ ifNotNilBlock valueWithPossibleArgs: {self}
> 
> 
> BlockClosure>>valueWithPossibleArgs: anArray
> 
>     | n |
>     (n := self numArgs) = 0 ifTrue: [^ self value].
>     n = anArray size ifTrue: [^ self valueWithArguments: anArray].
>     ^ self valueWithArguments: (n > anArray size
>         ifTrue: [anArray, (Array new: n - anArray size)]
>         ifFalse: [anArray copyFrom: 1 to: n]): anArray
> 
> 
> 
> A quick search of Mantis for ifNotNil or valueWithPossibleArgs hasn't
> left me any wiser. Something to do with the interpreter, perhaps
> optimisation?
> 
> Thanks,
> Zulq.
> 
> 
> 
Because this is checked at compile time:

MessageNode>>transformIfNotNilIfNil: encoder
	((self checkBlock: (arguments at: 1) as: 'NotNil arg' from: encoder)
		and: [self checkBlock: (arguments at: 2) as: 'Nil arg' from: encoder])
		ifTrue:
			[selector _ SelectorNode new key: #ifTrue:ifFalse: code: #macro.
			receiver _ MessageNode new
				receiver: receiver
				selector: #==
				arguments: (Array with: NodeNil)
				precedence: 2
				from: encoder.
			arguments swap: 1 with: 2.
			^true]
		ifFalse:
			[^false]

So you can see that this message is transform and never send to the object. And it is the #==
message who are send...

But I don't know why the "dummy" method call #valueWithPossibleArgs:.

This it is how it's done in the old compiler but if we tack a look inside the new one(load
NewCompiler + AST package) it can compile and give you what you were expecting:

	Object new ifNotNil: [:each | ^each]

give you in bytecode:

17 <40> pushLit: Object
18 <CC> send: new
19 <81 40> storeIntoTemp: 0
21 <73> pushConstant: nil
22 <C6> send: ==
23 <A8 02> jumpTrue: 27
25 <10> pushTemp: 0
26 <7C> returnTop
27 <78> returnSelf

Math








More information about the Squeak-dev mailing list