Thread overview | ||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
March 05, 2014 static switch | ||||
---|---|---|---|---|
| ||||
I wonder why there is no static switch (analogue to static if)? Because especially if you overload a lot of operators (which is done e.g. in the definition of complex and bigint) I would think something like T opOpAssign(string op, T)(T x) if(op=="+" || op=="-" || op=="*" || op=="/" || op=="%" || op=="^^" || op=="&" || op=="|" || op=="^" || op=="<<" || op==">>" || op==">>>") { static switch(op) { case "+": case "-": ... break; case "*": ... } } would look much better than { static if(op=="+" || op == "-") { ... } else static if(op == "*") { ... } else { ... } } |
March 05, 2014 Re: static switch | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dominikus Dittes Scherkl | Well, as long as we're talking about language design, couldn't the first be improved more by allowing something like: T opOpAssign(string op : in("+", "-", "*", "/", "%", "^^", "&", "|", "^", "<<", ">>", ">>>"), T)(T x) The `in` there is my attempt to disambiguate between this type of constraint and what I view as the most likely syntax for built-in tuples. On 3/5/14, Dominikus Dittes Scherkl <Dominikus.Scherkl@continental-corporation.com> wrote: > I wonder why there is no static switch (analogue to static if)? > Because especially if you overload a lot of operators (which is > done e.g. > in the definition of complex and bigint) I would think something > like > > T opOpAssign(string op, T)(T x) if(op=="+" || op=="-" || op=="*" > || op=="/" || op=="%" || op=="^^" || op=="&" || op=="|" || > op=="^" || op=="<<" || op==">>" || op==">>>") > { > static switch(op) > { > case "+": > case "-": > ... > break; > case "*": > ... > } > } > > would look much better than > > { > static if(op=="+" || op == "-") > { > ... > } > else static if(op == "*") > { > ... > } > else > { > ... > } > } > |
March 05, 2014 Re: static switch | ||||
---|---|---|---|---|
| ||||
Posted in reply to Orvid King | Orvid King wrote:
> couldn't the first be improved more by allowing something like:
>
> T opOpAssign(string op : in("+", "-", "*", "/", "%", "^^", "&", "|", "^", "<<", ">>", ">>>"), T)(T x)
>
> The `in` there is my attempt to disambiguate between this type of constraint and what I view as the most likely syntax for built-in tuples.
While a static switch could be a little handy (I have desired it few times, despite a static foreach is much more needed), there's no strong need for that "in", You can use something like (untested):
T opOpAssign(string op, T)(T x)
if ("+ - * / % ^^ & | ^ << >> >>>".split.canFind(op))
Bye,
bearophile
|
March 05, 2014 Re: static switch | ||||
---|---|---|---|---|
| ||||
Posted in reply to Orvid King | On Wednesday, 5 March 2014 at 13:34:15 UTC, Orvid King wrote:
> Well, as long as we're talking about language design, couldn't the first be improved more by allowing something like:
>
> T opOpAssign(string op : in("+", "-", "*", "/", "%", "^^", "&", "|", "^", "<<", ">>", ">>>"), T)(T x)
>
> The `in` there is my attempt to disambiguate between this type of constraint and what I view as the most likely syntax for built-in tuples.
Also a nice idea. But a wholy new concept. "static switch" would be analogue to "static if", just syntactic suggar - I considered it only because I stumbled over the ugly "else static if()" chains in phobos.
|
March 05, 2014 Re: static switch | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dominikus Dittes Scherkl | On Wednesday, 5 March 2014 at 12:51:23 UTC, Dominikus Dittes
Scherkl wrote:
> I wonder why there is no static switch (analogue to static if)?
> Because especially if you overload a lot of operators (which is done e.g.
> in the definition of complex and bigint) I would think something like
>
> T opOpAssign(string op, T)(T x) if(op=="+" || op=="-" || op=="*" || op=="/" || op=="%" || op=="^^" || op=="&" || op=="|" || op=="^" || op=="<<" || op==">>" || op==">>>")
> {
> static switch(op)
> {
> case "+":
> case "-":
> ...
> break;
> case "*":
> ...
> }
> }
>
> would look much better than
>
> {
> static if(op=="+" || op == "-")
> {
> ...
> }
> else static if(op == "*")
> {
> ...
> }
> else
> {
> ...
> }
> }
use:
bool In(Args...)(string str, Args args){
foreach(a; args)
if(str == a)return true;
return false;
}
class Foo{
T opOpAssign(string op, T)(T x) if(op.In("+", "-", "*", "/",
"%", "^^", "&", "|", "^", "<<", ">>", ">>>")){
return x;
}
}
|
March 05, 2014 Re: static switch | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dominikus Dittes Scherkl | On 3/5/14, 4:51 AM, Dominikus Dittes Scherkl wrote:
> I wonder why there is no static switch (analogue to static if)?
Doesn't enable anything. There'd be a ton more juice in a static foreach; it would enable a lot of great idioms. We should pursue that instead.
Andrei
|
March 05, 2014 Re: static switch | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On Wednesday, 5 March 2014 at 18:39:08 UTC, Andrei Alexandrescu wrote:
> Doesn't enable anything. There'd be a ton more juice in a static foreach; it would enable a lot of great idioms. We should pursue that instead.
>
> Andrei
Btw, are there any unexpected design difficulties with static foreach? Or it is just waiting for someone to do the pull request?
|
March 05, 2014 Re: static switch | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dicebot | On 3/5/14, 10:45 AM, Dicebot wrote:
> On Wednesday, 5 March 2014 at 18:39:08 UTC, Andrei Alexandrescu wrote:
>> Doesn't enable anything. There'd be a ton more juice in a static
>> foreach; it would enable a lot of great idioms. We should pursue that
>> instead.
>>
>> Andrei
>
> Btw, are there any unexpected design difficulties with static foreach?
> Or it is just waiting for someone to do the pull request?
The one difficulty is figuring how to allow for all iterations to stay in the same scope, yet not have duplicate definitions of the iteration symbol. Probably worth a DIP. Other than that, we're a go.
Andrei
|
March 05, 2014 Re: static switch | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dominikus Dittes Scherkl | On Wednesday, 5 March 2014 at 12:51:23 UTC, Dominikus Dittes
Scherkl wrote:
> I wonder why there is no static switch (analogue to static if)?
> Because especially if you overload a lot of operators (which is done e.g.
> in the definition of complex and bigint) I would think something like
>
> T opOpAssign(string op, T)(T x) if(op=="+" || op=="-" || op=="*" || op=="/" || op=="%" || op=="^^" || op=="&" || op=="|" || op=="^" || op=="<<" || op==">>" || op==">>>")
> {
> static switch(op)
> {
> case "+":
> case "-":
> ...
> break;
> case "*":
> ...
> }
> }
>
> would look much better than
>
> {
> static if(op=="+" || op == "-")
> {
> ...
> }
> else static if(op == "*")
> {
> ...
> }
> else
> {
> ...
> }
> }
Constant folding will generate code like that. What does the
static buys you ?
|
March 05, 2014 Re: static switch | ||||
---|---|---|---|---|
| ||||
Posted in reply to deadalnix | On 3/5/14, 4:07 PM, deadalnix wrote:
> On Wednesday, 5 March 2014 at 12:51:23 UTC, Dominikus Dittes
> Scherkl wrote:
>> I wonder why there is no static switch (analogue to static if)?
>> Because especially if you overload a lot of operators (which is done e.g.
>> in the definition of complex and bigint) I would think something like
>>
>> T opOpAssign(string op, T)(T x) if(op=="+" || op=="-" || op=="*" ||
>> op=="/" || op=="%" || op=="^^" || op=="&" || op=="|" || op=="^" ||
>> op=="<<" || op==">>" || op==">>>")
>> {
>> static switch(op)
>> {
>> case "+":
>> case "-":
>> ...
>> break;
>> case "*":
>> ...
>> }
>> }
>>
>> would look much better than
>>
>> {
>> static if(op=="+" || op == "-")
>> {
>> ...
>> }
>> else static if(op == "*")
>> {
>> ...
>> }
>> else
>> {
>> ...
>> }
>> }
>
> Constant folding will generate code like that. What does the
> static buys you ?
Readability.
|
Copyright © 1999-2021 by the D Language Foundation