MonkeeSage
12/18/2007 11:15:00 PM
On Dec 17, 10:31 pm, Tom Agnew <agne...@nospam.net> wrote:
> Hello all,
>
> I have a suggestion for an enhancement that could add to the readability
> of my programs.
>
> I'm a frequent user of 'if' and 'unless' statement modifiers. It's a
> beautiful abbreviation that enhances the readability of my code.
>
> Here is a pattern that I use from time to time:
>
> result = func(x) unless func(x)==y
>
> This has the potential to invoke func(x) twice in some implementations.
>
> It occurred to me that the 'case' semantics could be borrowed to create
> a useful new modifier. Here's a suggestion that might be more efficient
> and possibly more readable:
>
> result = func(x) when result==y
I'm not sure how easy it would be to implement or what other
drawbacks / benefits it might entail, but just for clarification, this
looks like it would be sugar for something like this (I'd never write
this, just trying to illustrate what I think is happening)...
result = tmp = func(x) == y ? tmp : nil; tmp = nil
> The modifier would act as a mini-continuation, so that the result would
> not be updated (i.e., it would be rolled back) if the condition is not
> satisfied.
>
> Then 'when' modifier could invoke the case equality operator by default.
> For example:
>
> result = func1(x) when 200..499
> result = func2(x) when 500..999
>
> or even:
>
> result = translate(x) when Class1,Class2
> result = translate(x) when value1,range2,Class3
>
> Anyone else have a use for this? Any thoughts??
In these examples (besides allowing multiple conditions), what would
be the difference between 'when' and 'if'?
> Best regards,
> Tom Agnew
Regards,
Jordan