digitalmars.D - static try?
- Mehrdad (19/19) Oct 30 2011 I've written this piece of code a fair number of times:
- deadalnix (2/21) Oct 31 2011 That sound dangerous. You can get some compile error and not notice it.
- Mehrdad (3/31) Oct 31 2011 Huh? How is that any worse than is(typeof({ some random code })), which
- deadalnix (8/40) Nov 02 2011 At least, this if(is(typeof())) isn't executed and isn't supposed to be
- Robert Jacques (2/21) Nov 01 2011 Vote++
- dennis luehring (18/37) Nov 01 2011 1. sorry but look like static exception based flow control - because of
- Vladimir Panteleev (6/7) Nov 01 2011 "debug", "version", "static if" don't create scopes.
- dennis luehring (18/22) Nov 01 2011 i know - wrong name for the block between the try{...}
- kenji hara (14/33) Nov 01 2011 --1;
- bcs (6/46) Nov 01 2011 Please no! I've thought for years that string mixins are seriously
- Don (6/12) Nov 01 2011 IMHO the only problem is the is(typeof()) syntax.
I've written this piece of code a fair number of times: static if (is(typeof(foo()))) { foo(); } else { bar(); } When the expression inside the condition (i.e. the call to foo()) gets complicated, you get lots of code duplication and things become harder to read. So I'm thinking, why not just introduce a 'static try'? Something like: static try { foo(); } catch // (string ex) // perhaps let them know what the error is? { bar(); } It's a clean and immensely readable improvement IMO, and it doesn't introduce any new keywords or any breaking changes to anything. How's the idea?
Oct 30 2011
Le 31/10/2011 02:21, Mehrdad a écrit :I've written this piece of code a fair number of times: static if (is(typeof(foo()))) { foo(); } else { bar(); } When the expression inside the condition (i.e. the call to foo()) gets complicated, you get lots of code duplication and things become harder to read. So I'm thinking, why not just introduce a 'static try'? Something like: static try { foo(); } catch // (string ex) // perhaps let them know what the error is? { bar(); } It's a clean and immensely readable improvement IMO, and it doesn't introduce any new keywords or any breaking changes to anything. How's the idea?That sound dangerous. You can get some compile error and not notice it.
Oct 31 2011
On 10/31/2011 4:16 AM, deadalnix wrote:Le 31/10/2011 02:21, Mehrdad a écrit :Huh? How is that any worse than is(typeof({ some random code })), which we're already doing all over the place?I've written this piece of code a fair number of times: static if (is(typeof(foo()))) { foo(); } else { bar(); } When the expression inside the condition (i.e. the call to foo()) gets complicated, you get lots of code duplication and things become harder to read. So I'm thinking, why not just introduce a 'static try'? Something like: static try { foo(); } catch // (string ex) // perhaps let them know what the error is? { bar(); } It's a clean and immensely readable improvement IMO, and it doesn't introduce any new keywords or any breaking changes to anything. How's the idea?That sound dangerous. You can get some compile error and not notice it.
Oct 31 2011
Le 31/10/2011 23:29, Mehrdad a écrit :On 10/31/2011 4:16 AM, deadalnix wrote:At least, this if(is(typeof())) isn't executed and isn't supposed to be executed. Here you just created a piece of code that you don't know if it will be executed and that will silently fail with no error message. It remind me INTERCAL and it's styupid comment mecanism (at least, it was intentionnaly stupid with INTERCAL) : http://en.wikipedia.org/wiki/INTERCALLe 31/10/2011 02:21, Mehrdad a écrit :Huh? How is that any worse than is(typeof({ some random code })), which we're already doing all over the place?I've written this piece of code a fair number of times: static if (is(typeof(foo()))) { foo(); } else { bar(); } When the expression inside the condition (i.e. the call to foo()) gets complicated, you get lots of code duplication and things become harder to read. So I'm thinking, why not just introduce a 'static try'? Something like: static try { foo(); } catch // (string ex) // perhaps let them know what the error is? { bar(); } It's a clean and immensely readable improvement IMO, and it doesn't introduce any new keywords or any breaking changes to anything. How's the idea?That sound dangerous. You can get some compile error and not notice it.
Nov 02 2011
On Sun, 30 Oct 2011 21:21:58 -0400, Mehrdad <wfunction hotmail.com> wrote:I've written this piece of code a fair number of times: static if (is(typeof(foo()))) { foo(); } else { bar(); } When the expression inside the condition (i.e. the call to foo()) gets complicated, you get lots of code duplication and things become harder to read. So I'm thinking, why not just introduce a 'static try'? Something like: static try { foo(); } catch // (string ex) // perhaps let them know what the error is? { bar(); } It's a clean and immensely readable improvement IMO, and it doesn't introduce any new keywords or any breaking changes to anything. How's the idea?Vote++
Nov 01 2011
Am 31.10.2011 02:21, schrieb Mehrdad:I've written this piece of code a fair number of times: static if (is(typeof(foo()))) { foo(); } else { bar(); } When the expression inside the condition (i.e. the call to foo()) gets complicated, you get lots of code duplication and things become harder to read. So I'm thinking, why not just introduce a 'static try'? Something like: static try { foo(); } catch // (string ex) // perhaps let them know what the error is? { bar(); } It's a clean and immensely readable improvement IMO, and it doesn't introduce any new keywords or any breaking changes to anything. How's the idea?1. sorry but look like static exception based flow control - because of the "try/catch" - and that is evil 2. static try { } introduces an compiletime scope - what would the following code mean? static try { int x = 10; foo(); int y = 20; } catch { bar(); }
Nov 01 2011
On Tue, 01 Nov 2011 18:33:56 +0200, dennis luehring <dl.soluz gmx.net> wrote:introduces an compiletime scope - what would the following code mean?"debug", "version", "static if" don't create scopes. -- Best regards, Vladimir mailto:vladimir thecybershadow.net
Nov 01 2011
Am 01.11.2011 17:46, schrieb Vladimir Panteleev:On Tue, 01 Nov 2011 18:33:56 +0200, dennis luehring<dl.soluz gmx.net> wrote:i know - wrong name for the block between the try{...} static try { ------------------ foo1(); foo2(); foo3(); ------------------ } catch { bar(); } im missing a description of what should happen if any of the foos fails to compile? is then the catch part used in compilation? the idea is too near to the syntax of the exceptionhandling and have total different semantic... is that good?introduces an compiletime scope - what would the following code mean?"debug", "version", "static if" don't create scopes.
Nov 01 2011
--1; It you want to reduce duplication of long code, you can use string mixin. enum code =3D q{ ...long code... }; static if (is(typeof({ mixin(code); }))) { mixin(code); } else { ... } Kenji Hara 2011/10/31 Mehrdad <wfunction hotmail.com>:I've written this piece of code a fair number of times: =A0 =A0 static if (is(typeof(foo()))) { foo(); } =A0 =A0 else { bar(); } When the expression inside the condition (i.e. the call to foo()) gets complicated, you get lots of code duplication and things become harder to read. So I'm thinking, why not just introduce a 'static try'? Something like: =A0 =A0static try =A0 =A0{ =A0 =A0 =A0 =A0foo(); =A0 =A0} =A0 =A0catch =A0// (string ex) =A0// perhaps let them know what the error=is?=A0 =A0{ =A0 =A0 =A0 =A0bar(); =A0 =A0} It's a clean and immensely readable improvement IMO, and it doesn't introduce any new keywords or any breaking changes to anything. How's the idea?
Nov 01 2011
On 11/01/2011 10:04 AM, kenji hara wrote:--1; It you want to reduce duplication of long code, you can use string mixin. enum code = q{ ...long code... }; static if (is(typeof({ mixin(code); }))) { mixin(code); } else { ... }Please no! I've thought for years that string mixins are seriously overused in D. IMnsHO they should only be used as a method of last resort. For every case I've seen where there is a alternative to a string mixin, the alternative was cleaner. If people don't like the static try/catch, how about: static try/else?Kenji Hara 2011/10/31 Mehrdad<wfunction hotmail.com>:I've written this piece of code a fair number of times: static if (is(typeof(foo()))) { foo(); } else { bar(); } When the expression inside the condition (i.e. the call to foo()) gets complicated, you get lots of code duplication and things become harder to read. So I'm thinking, why not just introduce a 'static try'? Something like: static try { foo(); } catch // (string ex) // perhaps let them know what the error is? { bar(); } It's a clean and immensely readable improvement IMO, and it doesn't introduce any new keywords or any breaking changes to anything. How's the idea?
Nov 01 2011
On 02.11.2011 03:57, bcs wrote:On 11/01/2011 10:04 AM, kenji hara wrote:Probably worth noting that in the latest release, you can instantiate a template from inside an is(typeof()), and if it fails to compile, the template instantiation will be discarded. (Prior to 2.056, if the template instantiation failed, compilation would fail). This means you can now factor out "does it compile?" tests into a template, to reduce code duplication.--1; It you want to reduce duplication of long code, you can use string mixin. enum code = q{ ...long code... }; static if (is(typeof({ mixin(code); }))) { mixin(code); } else { ... }Please no! I've thought for years that string mixins are seriously overused in D. IMnsHO they should only be used as a method of last resort. For every case I've seen where there is a alternative to a string mixin, the alternative was cleaner. If people don't like the static try/catch, how about: static try/else?
Nov 02 2011
Le 02/11/2011 08:24, Don a écrit :On 02.11.2011 03:57, bcs wrote:This look like a cleaner solution.On 11/01/2011 10:04 AM, kenji hara wrote:Probably worth noting that in the latest release, you can instantiate a template from inside an is(typeof()), and if it fails to compile, the template instantiation will be discarded. (Prior to 2.056, if the template instantiation failed, compilation would fail). This means you can now factor out "does it compile?" tests into a template, to reduce code duplication.--1; It you want to reduce duplication of long code, you can use string mixin. enum code = q{ ...long code... }; static if (is(typeof({ mixin(code); }))) { mixin(code); } else { ... }Please no! I've thought for years that string mixins are seriously overused in D. IMnsHO they should only be used as a method of last resort. For every case I've seen where there is a alternative to a string mixin, the alternative was cleaner. If people don't like the static try/catch, how about: static try/else?
Nov 02 2011
On 11/02/2011 12:24 AM, Don wrote:On 02.11.2011 03:57, bcs wrote:That still has the (potential) issue of putting the code out-of-line.On 11/01/2011 10:04 AM, kenji hara wrote:Probably worth noting that in the latest release, you can instantiate a template from inside an is(typeof()), and if it fails to compile, the template instantiation will be discarded. (Prior to 2.056, if the template instantiation failed, compilation would fail). This means you can now factor out "does it compile?" tests into a template, to reduce code duplication.--1; It you want to reduce duplication of long code, you can use string mixin. enum code = q{ ...long code... }; static if (is(typeof({ mixin(code); }))) { mixin(code); } else { ... }Please no! I've thought for years that string mixins are seriously overused in D. IMnsHO they should only be used as a method of last resort. For every case I've seen where there is a alternative to a string mixin, the alternative was cleaner. If people don't like the static try/catch, how about: static try/else?
Nov 02 2011
On 31.10.2011 02:21, Mehrdad wrote:I've written this piece of code a fair number of times: static if (is(typeof(foo()))) { foo(); } else { bar(); } When the expression inside the condition (i.e. the call to foo()) gets complicated, you get lots of code duplication and things become harder to read.IMHO the only problem is the is(typeof()) syntax. Otherwise I don't see how this has any more code duplication than: if( foo(lots_of_parameters) ) foo(lots_of_parameters); else bar();
Nov 01 2011
On 11/1/2011 11:55 PM, Don wrote:On 31.10.2011 02:21, Mehrdad wrote:Huh? sorry but I'm confused as how that's related to static try/catch...I've written this piece of code a fair number of times: static if (is(typeof(foo()))) { foo(); } else { bar(); } When the expression inside the condition (i.e. the call to foo()) gets complicated, you get lots of code duplication and things become harder to read.IMHO the only problem is the is(typeof()) syntax. Otherwise I don't see how this has any more code duplication than: if( foo(lots_of_parameters) ) foo(lots_of_parameters); else bar();
Nov 02 2011
On 02.11.2011 09:38, Mehrdad wrote:On 11/1/2011 11:55 PM, Don wrote:The only benefit you'd get from static try/catch is a small reduction in code duplication, in one specific idiom. I'm arguing that the code duplication is no worse in this case than in anything other case in the language. I'm not seeing why that particular idiom is so phenomenally important that it deserves its own syntax to obtain such a tiny benefit.On 31.10.2011 02:21, Mehrdad wrote:Huh? sorry but I'm confused as how that's related to static try/catch...I've written this piece of code a fair number of times: static if (is(typeof(foo()))) { foo(); } else { bar(); } When the expression inside the condition (i.e. the call to foo()) gets complicated, you get lots of code duplication and things become harder to read.IMHO the only problem is the is(typeof()) syntax. Otherwise I don't see how this has any more code duplication than: if( foo(lots_of_parameters) ) foo(lots_of_parameters); else bar();
Nov 02 2011