ifNotNil argument

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

ifNotNil argument

Ian Bartholomew-20
If you have the following code in a method then the compiler warns that #arg
is assigned but is not used.  The CodeMentor does not detect any problems.

 1 ifNil: [self test] ifNotNil: [:arg | self test]

I tend to agree with the CodeMentor in this case.

Ian


Reply | Threaded
Open this post in threaded view
|

Re: ifNotNil argument

Louis Sumberg-2
Somewhat related, the compiler flags as illegal an assignment to arg.
The following pseudo-code works in D5 but not in D6

        ^someExpression ifNotNil:
                [:value |
                        value := value doSomething.
                        value := value doSomethingElse]

This can be changed to:
        |value|
        ^someExpression isNil ifFalse:
                [value := value doSomething.
                value := value doSomethingElse]

which is probably what most of my methods like this were before I
changed them to use ifNotNil:, taking advantage of the "free" temporary.

-- Louis


Reply | Threaded
Open this post in threaded view
|

Re: ifNotNil argument

Louis Sumberg-2
Louis Sumberg wrote too hastily but meant:

This can be changed to:
      |value|
      ^(value := someExpression) isNil ifFalse:
          [value := value doSomething.
          value := value doSomethingElse]


Reply | Threaded
Open this post in threaded view
|

Re: ifNotNil argument

Blair McGlashan
In reply to this post by Ian Bartholomew-20
"Ian Bartholomew" <[hidden email]> wrote in message
news:[hidden email]...
> If you have the following code in a method then the compiler warns that
> #arg
> is assigned but is not used.  The CodeMentor does not detect any problems.
>
> 1 ifNil: [self test] ifNotNil: [:arg | self test]
>
> I tend to agree with the CodeMentor in this case.
>

This is by design. There is an assignment to the ifNotNil block's 'arg'
argument, the one which is generated to set the value of the argument. In
this case the compiler is effectively warning you that you should use the
niladic form, i.e.

    1 ifNil: [self test] ifNotNil: [self test]

Regards

Blair


Reply | Threaded
Open this post in threaded view
|

Re: ifNotNil argument

Blair McGlashan
In reply to this post by Louis Sumberg-2
"Louis Sumberg" <[hidden email]> wrote in message
news:[hidden email]...
> Somewhat related, the compiler flags as illegal an assignment to arg.

Which is indeed the case - it is illegal. Its worth reading the class
comment of BlockClosure to understand a bit more about how blocks are
implemented in D6 vs earlier versions of Dolphin.

Regards

Blair


Reply | Threaded
Open this post in threaded view
|

Re: ifNotNil argument

Ian Bartholomew-20
In reply to this post by Blair McGlashan
Blair,

>       In this case the compiler is effectively warning you that you should
> use the niladic form, i.e.

A niladic form, nice!

It looks like the list of changes is going to need a very careful study :-)

Thanks
    Ian