# The "when" block (theoretical)

**URL:** <https://forum.kirupa.com/t/the-when-block-theoretical/324943>\
**Category:** flash\
**Created:** [September 17, 2011, 12:23am UTC](https://forum.kirupa.com/t/the-when-block-theoretical/324943 "2011-09-17T00:23:16Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![IQAndreas](https://avatars.discourse-cdn.com/v4/letter/i/f08c70/32.png) [@IQAndreas](https://forum.kirupa.com/u/IQAndreas)\
**Post date:** [September 17, 2011, 12:23am UTC](https://forum.kirupa.com/t/the-when-block-theoretical/324943/1 "2011-09-17T00:23:16Z")

</div>

**Note:** Once again I am playing around with my purely fictional language beyond the confined bounds of AS3. This post has no practical applications what so ever, and is merely an exercise of the mind.

This post serves absolutely no purpose, as the “ **when** ” block does not exist in ActionScript. I just thought it was a brilliant idea and felt like ranting about it (though, I’m sure other “superior” programming languages may have a similar system in them).

[HR][/HR]

You have all been there. You only want to perform a certain action if the instance you have is of a certain type.

For instance, say you have  
[LIST]  
[_]a class named **Block** with many subclasses  
[_]an interface named **ITextured** with one required function, “**getTexture()**”  
[\*]The **ITextured** interface may be applied to some, but not all, of the Block’s subclasses  
[/LIST]Likely, your code looks like this (poor example, but I couldn’t think of a better one):

```auto
var block:Block = ...;

if (block is ITextured)
{
    var texturedBlock:ITextured = block as ITextured;
    return texturedBlock.getTexture();
}
else
{
    return textures[block.textureID];
}

```

Note that the code contains two flaws.[LIST=1]  
[_]You need to create an extra variable ( **texturedBlock** ) that references the exact same instance.  
[_]The extra variable ( **texturedBlock** ) cannot be treated as a “ **Block** ” (of course, you can still use the old “ **block** ” variable for that.  
[/LIST]But, it’s not that much extra work, so we suck it up and deal with it.

[SIZE=3]\*\*The “when” block  
\*\*[/SIZE]Instead, I propose just adding a minor syntax “addition”, the “when” block:

```auto
when (block is ITextured)
{
    return block.getTexture();
}
else
{
    return textures[block.textureID];
}

```

[SIZE=3]\*\*Syntax rules  
\*\*[/SIZE]  
[LIST]  
[_]The “when” block will only execute if the passed in variable is an instance of the specified Class or Interface.  
[_]Within the “when” block, the IDE knows to auto-complete with both the “original” typing of the specified variable, as well as the “additional” typing specified.  
[\*]It should theoretically be possible to nest “when” blocks, adding a third or more extra “typings” to the variable.  
[/LIST][SIZE=3]\*\*Additional Examples  
\*\*[/SIZE]Just because I feel like showing off, I’ll do an outrageous example

```auto
public function render(item:GameObject)
{
    when (item is null)
    {
        throw new Error("Cannot render a null object");
    }
    else when (item is ISpecialRenderer)
    {
        item.renderEngine.render(this);
    }
    else when (item is Block)
    {
        when (item is ITextured)
        {
            // function renderBlock( param0:Block, param1:Texture)
            this.renderBlock(item, item.getTexture());
        }
        else
        {
            // function renderBlock( param0:Block, param1:Texture)
            this.renderBlock(item, Block.defaultTexture);
        }
    }
    else 
    {
        //NOTE: This part could also use some cleaning up, but doesn't fit in with the "when" block...
        // function getRenderer(param0:GameObject):Renderer
        var renderer:Renderer = world.getRenderer(item);
        if (renderer)
        {
            renderer.render(item);
        }
        else
        {
            throw new Error("Unable to render item " + item.toString());
        }
    }
}

```
