Boost logo

Boost :

From: Joel de Guzman (joel_at_[hidden])
Date: 2003-10-17 11:15:12


Joel de Guzman <joel_at_[hidden]> wrote:
> John Torjo <john.lists_at_[hidden]> wrote:
>> In the docs for lambda, at a point, Joel de Guzmann suggested a better
>> syntax for control structures, namely:
>>
>> if_(condition)[then_part]
>> if_(condition)[then_part].else_[else_part]
>> while_(condition)[body]
>> do_[body].while_(condition)
>> for_(init, condition, increment)[body]
>>
>> I strongly prefer this, but the same could be done for switch_statement,
>> which is pretty huge otherwise:
>>
>> switch_(condition)
>> .case_<label1>[lambda_expression]
>> .case_<label2>[lambda_expression]
>>
>> What do you think?
>
> Not possible. case_<label1>[lambda_expression] is a
> syntax error.
>
> Phoenix-2 will have:
>
> switch_(condition)
> [
> case_(label1)[expr1],
> case_(label2)[expr2],
> case_(label3)[expr3]
> default_[expr4]
> ]
>
> This was already discussed at great lengths but somehow, the implementation
> fell through the cracks. Spirit is already using this syntax since Hartmut
> Kaiser implemented the switch_p deterministic parsers.

Whoops! Wrong! That should be:

    switch_(condition)
    [
        case_<label1>(expr1),
        case_<label2>(expr2),
        case_<label3>(expr3),
        default_(expr4)
    ]

The parens are unavoidable. This is the best I can think of. Unfortunately,
multiple expressions will have to be written as:

        case_<label1>
        ((
            expr1,
            expr2,
            expr3
        ))

If you really want to keep using the braces, it will be:

    switch_(condition)
    [
        case_<label1>()[expr1],
        case_<label2>()[expr2],
        case_<label3>()[expr3],
        default_(expr4)
    ]

The extra parens are not quite pleasing to the eyes ;-(

One more alternative, using MPL is:

    switch_(condition)
    [
        case_(int_<label1>())[expr1],
        case_(int_<label2>())[expr2],
        case_(int_<label3>())[expr3],
        default_(expr4)
    ]

Yaiks!

Actually,

     switch_(condition)
    [
        case_(label1)[expr1],
        case_(label2)[expr2],
        case_(label3)[expr3]
        default_[expr4]
    ]

will work if label1..3 is declared as (example):

    mpl::int_<1> label1;

    mpl::int_<2> label2;

    mpl::int_<3> label3;

Choose your pick :-)

-- 
Joel de Guzman
http://www.boost-consulting.com
http://spirit.sf.net

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk