digitalmars.D.learn - Problem: handling a function dependent on release mode.
- Damian (12/12) Jul 20 2012 Trying to convert the below code to D, for the life of me I
- Namespace (10/22) Jul 20 2012 Maybe something like that?
- Damian (2/31) Jul 20 2012 That works perfect, you sir are a genius!
- Jacob Carlborg (8/16) Jul 21 2012 If "filename" and "line" is template parameters they will get the
- Namespace (2/18) Jul 21 2012 Equally when they are passed as normal parameters.
- Jacob Carlborg (4/5) Jul 22 2012 Really? I didn't know that.
- Namespace (2/5) Jul 22 2012 Really. Try it. ;)
- Jonathan M Davis (4/11) Jul 22 2012 Yeah. Don't have them be template parameters unless you need to, otherwi...
- Andrej Mitrovic (12/16) Jul 22 2012 I've just noticed something:
- Jonathan M Davis (13/32) Jul 20 2012 It is impossible to have a function which relies on -release unless you ...
- Jonathan M Davis (20/40) Jul 22 2012 No. The error is correct. A property can't take all of those arguments, ...
Trying to convert the below code to D, for the life of me I cannot do it :( I'm guess I need to use a template for this but I just cannot get my head around it. Any help please? #ifdef SFML_DEBUG // If in debug mode, perform a test on every call #define alCheck(Func) ((Func), priv::alCheckError(__FILE__, __LINE__)) #else // Else, we don't add any overhead #define alCheck(Func) (Func) #endif void alCheckError(const std::string& file, unsigned int line);
Jul 20 2012
On Friday, 20 July 2012 at 21:36:59 UTC, Damian wrote:Trying to convert the below code to D, for the life of me I cannot do it :( I'm guess I need to use a template for this but I just cannot get my head around it. Any help please? #ifdef SFML_DEBUG // If in debug mode, perform a test on every call #define alCheck(Func) ((Func), priv::alCheckError(__FILE__, __LINE__)) #else // Else, we don't add any overhead #define alCheck(Func) (Func) #endif void alCheckError(const std::string& file, unsigned int line);Maybe something like that? void alCheck(lazy void Func, string filename = __FILE__, uint line = __LINE__) { Func(); debug { alCheckError(filename, line); } }
Jul 20 2012
On Friday, 20 July 2012 at 21:47:55 UTC, Namespace wrote:On Friday, 20 July 2012 at 21:36:59 UTC, Damian wrote:That works perfect, you sir are a genius!Trying to convert the below code to D, for the life of me I cannot do it :( I'm guess I need to use a template for this but I just cannot get my head around it. Any help please? #ifdef SFML_DEBUG // If in debug mode, perform a test on every call #define alCheck(Func) ((Func), priv::alCheckError(__FILE__, __LINE__)) #else // Else, we don't add any overhead #define alCheck(Func) (Func) #endif void alCheckError(const std::string& file, unsigned int line);Maybe something like that? void alCheck(lazy void Func, string filename = __FILE__, uint line = __LINE__) { Func(); debug { alCheckError(filename, line); } }
Jul 20 2012
On 2012-07-20 23:47, Namespace wrote:Maybe something like that? void alCheck(lazy void Func, string filename = __FILE__, uint line = __LINE__) { Func(); debug { alCheckError(filename, line); } }If "filename" and "line" is template parameters they will get the default values from the call site. void alCheck (string filename = __FILE__, uint line = __LINE__) (lazy void Func) {} -- /Jacob Carlborg
Jul 21 2012
On Saturday, 21 July 2012 at 16:05:24 UTC, Jacob Carlborg wrote:On 2012-07-20 23:47, Namespace wrote:Equally when they are passed as normal parameters.Maybe something like that? void alCheck(lazy void Func, string filename = __FILE__, uint line = __LINE__) { Func(); debug { alCheckError(filename, line); } }If "filename" and "line" is template parameters they will get the default values from the call site. void alCheck (string filename = __FILE__, uint line = __LINE__) (lazy void Func) {}
Jul 21 2012
On 2012-07-21 19:11, Namespace wrote:Equally when they are passed as normal parameters.Really? I didn't know that. -- /Jacob Carlborg
Jul 22 2012
On Sunday, 22 July 2012 at 16:19:29 UTC, Jacob Carlborg wrote:On 2012-07-21 19:11, Namespace wrote:Really. Try it. ;)Equally when they are passed as normal parameters.Really? I didn't know that.
Jul 22 2012
On Sunday, July 22, 2012 18:25:58 Namespace wrote:On Sunday, 22 July 2012 at 16:19:29 UTC, Jacob Carlborg wrote:Yeah. Don't have them be template parameters unless you need to, otherwise you get a different template instantiation _every_ time that you call the function. - Jonathan M DavisOn 2012-07-21 19:11, Namespace wrote:Really. Try it. ;)Equally when they are passed as normal parameters.Really? I didn't know that.
Jul 22 2012
On 7/22/12, Jonathan M Davis <jmdavisProg gmx.com> wrote:Yeah. Don't have them be template parameters unless you need to, otherwise you get a different template instantiation _every_ time that you call the function.I've just noticed something: property front(T)(T arr, string file = __FILE__, size_t line = __LINE__) if (isArray!T) { enforce(arr.length, safeFmt("%s(%s): Cannot call front on empty array.", file, line)); return std.array.front(arr); } This errors at compilation: Error: properties can only have zero, one, or two parameter Do you think I should file this?
Jul 22 2012
On Friday, July 20, 2012 23:36:58 Damian wrote:Trying to convert the below code to D, for the life of me I cannot do it :( I'm guess I need to use a template for this but I just cannot get my head around it. Any help please? #ifdef SFML_DEBUG // If in debug mode, perform a test on every call #define alCheck(Func) ((Func), priv::alCheckError(__FILE__, __LINE__)) #else // Else, we don't add any overhead #define alCheck(Func) (Func) #endif void alCheckError(const std::string& file, unsigned int line);It is impossible to have a function which relies on -release unless you put all calls to it in an assertion. There is _no_ way to check whether -release is enabled or not. -debug enables debug blocks: debug { //my code that runs with -debug only } but that's only if -debug is used. It's perfectly possible to compile with neither -release nor -debug. The result is that talking about "debug mode" in D is really confusing and inaccurate. - Jonathan M Davis
Jul 20 2012
On Sunday, July 22, 2012 23:40:16 Andrej Mitrovic wrote:On 7/22/12, Jonathan M Davis <jmdavisProg gmx.com> wrote:No. The error is correct. A property can't take all of those arguments, and the fact that you gave it default arguments has no effect on the type. Granted, that could be a bit annoying in this case, since then you'd be forced to using __FILE__ and __LINE__ as template arguments, but given how default arguments work, it makes no sense to allow what you're trying to do. On the bright side though, how often do you really need this? In my experience, the main advantages of having __FILE__ and __LINE__ as default arguments is to give exceptions the proper file and line number and to help with unit testing helper functions which need to give the file and line number of their call point on test failure rather than the line number inside of them where the test failed. By the way, I believe that the agreed upon way to handle the check that you're doing is to use an assertion, not an exception - at least as far as Phobos goes. Using enforce there is probably going to kill your performance given the fact that it can't be inlined and front is probably used often, and it makes using ranges in a nothrow context really annoying. The caller needs to check for empty first if there's any question rather than relying on front or popFront to handle it. - Jonathan M DavisYeah. Don't have them be template parameters unless you need to, otherwise you get a different template instantiation _every_ time that you call the function.I've just noticed something: property front(T)(T arr, string file = __FILE__, size_t line = __LINE__) if (isArray!T) { enforce(arr.length, safeFmt("%s(%s): Cannot call front on empty array.", file, line)); return std.array.front(arr); } This errors at compilation: Error: properties can only have zero, one, or two parameter Do you think I should file this?
Jul 22 2012