Boost logo

Boost :

From: Hans Dembinski (hans.dembinski_at_[hidden])
Date: 2019-11-12 10:31:08


> On 11. Nov 2019, at 18:39, рустам абдумаликов via Boost <boost_at_[hidden]> wrote:
>
> // I think this way it is much readable
> eswitch( get_file_extention( file_name ) ) >>
> case_( any_from( "cpp", "cc", "c++", "cxx", "C" ) ) >> [&]{...} >>
> case_( any_from( "h", "hpp", "hh", "h++", "hxx", "H" ) ) >> [&]{...};
>
> // OR
> // even this could be possible( but not yet implemented )
> eswitch( get_file_extention( file_name ) ) >>
> case_( "cpp", "cc", "c++", "cxx", "C" ) >> [&]{...} >>
> case_( "h", "hpp", "hh", "h++", "hxx", "H" ) >> [&]{...};
>
> -- Example_2 "Parse HTTP header with Regex":
>
> // O_o too much boilerplate
> if( std::regex_match( line, "^.+ 200 .+$"_r ) ){...}
> else if( std::regex_match( line, "^.+ 404 .+$"_r ) ){...}
> else if( std::regex_match( line, "^.+: .+$"_r ) ){...}
> else { terminate(); }
>
> // OR
>
> // I think this way your intention is clearer
> eswitch( line ) >>
> case_( "^.+ 200 .+$"_r ) >> []{...} // match for "HTTP/1.1 200 OK"
> case_( "^.+ 404 .+$"_r ) >> []{...} // match for "HTTP/1.1 404 Not Found"
> case_( "^.+: .+$"_r ) >> [&]{...} >> // match for "key: value"
> default_ >> []{ terminate(); };
>
>
> -- Example_2 "match in range of values":
>
> // Error prone approach
> if( value >= 1 && value <= 10 ){ Print("Value in a range[1,10]"); }
> if( value >= 11 && value <= 20 ){ Print("Value in a range[11,20]"); }
> else { Print('?'); };
>
> /// OR
>
> eswitch( value ) >>
> case_( _1.in( 1, 10 ) ) >> []{ Print("Value in a range[1,10]"); }
>>>
> case_( _1.in( 11, 20 ) ) >> []{ Print("Value in a range[11,20]"); }
>>>
> default_ >> []{ Print('?'); };
> // _1 it is lazy index mapping for the params in eswitch
>
>
> I my opinion "eswitch" looks more readable, more intuitive, less error prone
> and furthermore it is extensible.

I don't like this syntax, it is awkward and making the code less readable.

1) You are replacing a normal switch, which may be limited in its abilities but is well known even to beginners with something that has a rather unintuitive syntax. The shift operators are associated to streaming in C++ these days, but you use them differently here. Someone who reads this code who is not familiar with eswitch will not immediately understand this. Why did you chose operator>> instead of operator<< ? I think the choice is arbitrary and therefore hard to remember.

2) Your syntax requires one to type a lot of characters. I don't think one should replace the switch but if one did, the following syntax would be more economic and easier to remember, because it is less arbitrary

eswitch( value, case(match1), lambda1, case(match2), lambda2 , … , default(), lambdaN );

Best regards,
Hans


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