Fwd: Block explicit return

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

Fwd: Block explicit return

stephane ducasse
>
>
>
>
> On Sep 14, 2013, at 2:04 AM, blake <[hidden email]> wrote:
>
>> Page 55
>>
>> "The block answers the value of the last expression in its body,
>> unless there is an explicit return (with ↑), in which case it does not answer
>> any value."
>>
>> "...in which case it answers with the expression following the ↑, as usual."
>>
>> [3 + 4] value -> 7
>> [3 + 4. ^5] value ->5
>
> we should revisit the phrasing.
>
>> "The block answers the value of the last expression in its body,
>> unless there is an explicit return (with ↑), in which case it does not answer
>> any value."
>
> I propose
>
>> The block answers the value of the last expression in its body,
>> unless there is an explicit return (with ↑), in which case it will escape the current method execution
> and return the expression following the return.
>
> damien we can create a google docs to log the changes that should be done.
>
> Stef
>
>> _______________________________________________
>> Sbe-discussion mailing list
>> [hidden email]
>> https://www.iam.unibe.ch/mailman/listinfo/sbe-discussion
>
>
>
>


_______________________________________________
Sbe-discussion mailing list
[hidden email]
https://www.iam.unibe.ch/mailman/listinfo/sbe-discussion
Reply | Threaded
Open this post in threaded view
|

Re: Fwd: Block explicit return

blake watson
Yes, that's clear. 

One thing I've noted is that it's natural to go through these things in the workspace, which means you're not really seeing functional use, which is likely to result in surprises later on.

So the principle being discussed is illustrated by 

[
 [ 1+1 ] value * 5
] value  ----> 10

versus:

[
 [ ^1+1 ] value * 5
] value  ----> 2



On Fri, Sep 20, 2013 at 2:06 AM, stephane ducasse <[hidden email]> wrote:
>
>
>
>
> On Sep 14, 2013, at 2:04 AM, blake <[hidden email]> wrote:
>
>> Page 55
>>
>> "The block answers the value of the last expression in its body,
>> unless there is an explicit return (with ↑), in which case it does not answer
>> any value."
>>
>> "...in which case it answers with the expression following the ↑, as usual."
>>
>> [3 + 4] value -> 7
>> [3 + 4. ^5] value ->5
>
> we should revisit the phrasing.
>
>> "The block answers the value of the last expression in its body,
>> unless there is an explicit return (with ↑), in which case it does not answer
>> any value."
>
> I propose
>
>> The block answers the value of the last expression in its body,
>> unless there is an explicit return (with ↑), in which case it will escape the current method execution
> and return the expression following the return.
>
> damien we can create a google docs to log the changes that should be done.
>
> Stef
>
>> _______________________________________________
>> Sbe-discussion mailing list
>> [hidden email]
>> https://www.iam.unibe.ch/mailman/listinfo/sbe-discussion
>
>
>
>


_______________________________________________
Sbe-discussion mailing list
[hidden email]
https://www.iam.unibe.ch/mailman/listinfo/sbe-discussion


_______________________________________________
Sbe-discussion mailing list
[hidden email]
https://www.iam.unibe.ch/mailman/listinfo/sbe-discussion
Reply | Threaded
Open this post in threaded view
|

Re: Block explicit return

Prof. Andrew P. Black
>> The block answers the value of the last expression in its body,
>> unless there is an explicit return (with ↑), in which case it will escape the current method execution
> and return the expression following the return.

That's not clear.  I'm not even sure that it makes sense.  What is the "it" in "in which case it will escape the current method execution".   Syntactically, "it" should be "the block", which is a syntactic construct, and can't escape.

How about this:

——8X———————————————————————

A block answers the value of the last expression in its body.   It is possible that execution does not reach the last expression in a block's body, usually because of a return expression (with ↑) in the block, but sometimes because an exception has been signaled.  In such a case the block itself does not answer any value — but this is OK, because the context surrounding the block will never resume execution and thus will never expect a value.

Lest this sound complicated, let's look a some small examples.

factorial 
self < 0 ifTrue: [ArithmeticError signal: 'factorial of negative number'].
self = 0 ifTrue: [^ 1].
^ self * (self -1) factorial

The first block, which is executed when self < 0, never produces a value.  Its execution signals an ArithmeticError exception, which terminates the execution of the block.  This exception also terminates the execution of the factorial method, and any methods that enclose the send of factorial, until a context that handles the signaling of the ArithmeticError is reached.

The second block, which is executed when self = 0, never produces a value either.  That's because the return expression ^1 returns from the enclosing method.  In the example, this means that the factorial method itself answers 1, which is of course exactly what we want.

All you need to remember is that a ↑ in a block returns not just from the block, but also from the enclosing method.   So don't use ↑ in a block unless that's what you want!

——8X———————————————————————

_______________________________________________
Sbe-discussion mailing list
[hidden email]
https://www.iam.unibe.ch/mailman/listinfo/sbe-discussion