Count template parameters of method
Hello, How to count a number of parameters in uninitialized template method? For example: struct Test { void abc(int a, bool status, string text)() {} { The method "Test.abc" has three template paramenters. I know that "TemplateArgsOf" exists but it is used only for INITIALIZED templates...
Re: Docs generation example
On Saturday, 10 October 2020 at 02:07:02 UTC, Виталий Фадеев wrote: Wanted! Docs generation example. I have dub project, sources/*.d. I want html-index with all classes/functions. Is exists simple, hi-level, one-line command line solution ? The more official way is: dub build --build=docs Alternatively to use ddox with: dub build --build=ddoc
Re: List of exceptions?
On Saturday, 10 October 2020 at 20:32:22 UTC, DMon wrote: On Saturday, 10 October 2020 at 19:55:44 UTC, Ali Çehreli wrote: On 10/10/20 12:51 PM, DMon wrote: Thank you for your and Imperatorns time. Even if it did go in circles and get stuck in the mud. No problem. We're doing it out of free will :)
Re: List of exceptions?
On Saturday, 10 October 2020 at 19:55:44 UTC, Ali Çehreli wrote: On 10/10/20 12:51 PM, DMon wrote: Thank you for your and Imperatorns time. Even if it did go in circles and get stuck in the mud.
Re: List of exceptions?
On 10/10/20 12:51 PM, DMon wrote: > I will copy that down. > > The idea for specific exceptions came from the online docs and > Programing in D, 39.2 The try-catch statemet > > try > { // the code block that is being executed, where an // exception may be > thrown > } > catch (an_exception_type) > { // expressions to execute if an exception of this // type is caught > } > catch (another_exception_type) > { // expressions to execute if an exception of this // other type is > caught // ... more catch blocks as appropriate ... > } > finally > { // expressions to execute regardless of whether an // exception is thrown > } I don't have time right now to write longer but I see how that can be confusing and apologize for the confusion. :( In general, forget all of that and just catch Exception. :) That's all you need. And 'finally' is almost never used in D. Ali
Re: List of exceptions?
On Saturday, 10 October 2020 at 18:16:45 UTC, Ali Çehreli wrote: On 10/10/20 9:16 AM, DMon wrote: > catch (Exception e) // implicit (any exception) > catch (ConvException f) // explicit (conversion only) > > Or is that not correct? I think in class hierarchies, "more general" and "more specific" are better terms. :) The answer is, catch by the most general under the Exception hierarchy that you care about. It depends on the program. In most of my programs, catching Exception in main is sufficient because I just print the error message. However, sometimes the error message does not make sense at that level: So, you can augment that error with another one: One cool thing about storing the 'actual' exception is, you can later debug it by catching the specific FooException and printing 'actual' as is, which contains the stack trace: But really, it all depends on your program. The simplest thing may to not catch at all. The default behavior is to dump the stack trace and it works for some programs. Ali I will copy that down. The idea for specific exceptions came from the online docs and Programing in D, 39.2 The try-catch statemet try { // the code block that is being executed, where an // exception may be thrown } catch (an_exception_type) { // expressions to execute if an exception of this // type is caught } catch (another_exception_type) { // expressions to execute if an exception of this // other type is caught // ... more catch blocks as appropriate ... } finally { // expressions to execute regardless of whether an // exception is thrown } This is where I'm at: import std.stdio; import std.conv; // StdioException // ConvException // StringException // ErrnoException // FormatException // UnicodeException // UTFException // FileMissingException // DataCorruptionException // FE_INEXACT // FE_UNDERFLOW // FE_OVERFLOW // int main() { string z1 = "+ "; string z2 = "++ "; bool a1 = false; bool a2 = true; int b1 = 0; int b2 = 1; uint c1 = 0; uint c2 = 1; float d1 = 1f; float d2 = 2f; char e1 = 'a'; char e2 = 'b'; int o1; int o2; //auto test; int[3] ar1; int[5] ar2; string st1 = "arg"; writeln("Control\n\n"); /* writeln("Testing"); try { writeln(z1 ~ e1 ~ st1); writeln(); } catch (Exception y1) { writeln("Something ", y1.msg, y1.info); } */ writeln("try...catch"); try { o1 = to!int("hello"); } catch (Exception i1) { writefln(z1 ~ "Message from exception i1: %s", i1.msg); writefln(z1 ~ "Info from exception i1: %s", i1.info); } writeln(); writeln("try...finally"); try // Will run as normal code. { o1 = a2 + b2; writeln(z1 ~ "", o1); } finally { writeln("Continues to run normally and no exceptions are displayed."); } writeln(); writeln("try...catch...finally"); try { to!int(z1); to!int(z2); } catch (ConvException j1) { writefln(z1 ~ "1st Exception msg: %s", j1.msg); writeln(); } catch (Exception k1) { writeln(z1 ~ "2nd Exception msg: %s", k1.msg); } finally { writeln(z1 ~ "There are two exceptions."); writeln(z2 ~ "The first exception is caught and that, immediately, exits the try clause."); } writeln("This still runs."); writeln(); /* writeln("Nesting"); try { */ return 0; }
Re: Why does sum not work in static arrays?
On Sunday, 6 December 2015 at 12:27:49 UTC, cym13 wrote: On Sunday, 6 December 2015 at 12:23:05 UTC, Tim K. wrote: Hi! I have the following code: int main(string[] argv) { import std.algorithm: sum; import std.stdio: writeln; uint[3] a1 = [1, 2, 3]; uint[] a2; for (int i = 1; i <= 3; ++i) a2 ~= i; writeln("a1: ", sum(a1)); writeln("a2: ", sum(a2)); return 0; } This throws the error: dummy.d(11): Error: template std.algorithm.iteration.sum cannot deduce function from argument types !()(uint[3]), candidates are: /usr/include/dmd/phobos/std/algorithm/iteration.d(3916): std.algorithm.iteration.sum(R)(R r) if (isInputRange!R && !isInfinite!R && is(typeof(r.front + r.front))) /usr/include/dmd/phobos/std/algorithm/iteration.d(3927): std.algorithm.iteration.sum(R, E)(R r, E seed) if (isInputRange!R && !isInfinite!R && is(typeof(seed = seed + r.front))) So a dynamic array works just fine. In fact, if I uncomment the line in question the program compiles and outputs the correct value (for a2). Why does "sum" not work on static arrays? Regards So that you do not shoot yourself in the foot too easily. A static array is a value type so it is passed by value to functions. If you pass a 1M array to a function... well, I guesse you don't want to do that. Can the template func `sum()` be made to take `ref` of a static array? The solution is to slice it: a1[].sum; That way you avoid the problem. While I understand the explanation of the current behavior, and the work-around, this inconstancy of making func calls means either the library, or the language leaves something to be desired: i.e dynamic array and static array cannot be used interchangeably: (sure I'm not talking about array decl / allocation, the user have to take different actions) I'm talking about a simple function call to calc the sum of the array. ``` sum(static_array[]); // v.s. sum(dynamic_array); ``` For example, if the user first decl a static array for fast prototyping, and later changed mind to use dynamic array, then s/he need to change the call all over the places. (I hope you are not telling me, every time people should use: ``` array_func(any_array[]); ``` is the correct D-idiom to use array in a func call)
Re: List of exceptions?
On 10/10/20 9:16 AM, DMon wrote: > catch (Exception e) // implicit (any exception) > catch (ConvException f) // explicit (conversion only) > > Or is that not correct? I think in class hierarchies, "more general" and "more specific" are better terms. :) The answer is, catch by the most general under the Exception hierarchy that you care about. It depends on the program. In most of my programs, catching Exception in main is sufficient because I just print the error message. However, sometimes the error message does not make sense at that level: void foo() { // The error thrown during this may not be meaningful to // the user of the program: // "Unexpected 'h' when converting from type string to type int" "hello".to!int; } So, you can augment that error with another one: import std.conv; import std.stdio; import std.format; class FooException : Exception { string msg; Exception actual; this (Exception e) { this.msg = format!"Failed to do foo: %s"(e.msg); super(this.msg); this.actual = e; // Store for more information later } } void foo() { try { "hello".to!int; } catch (Exception e) { // Convert to a more meanigful exception: throw new FooException(e); } } int main() { try { foo(); } catch (Exception e) { stderr.writefln!"ERROR: %s"(e.msg); return 1; } return 0; } One cool thing about storing the 'actual' exception is, you can later debug it by catching the specific FooException and printing 'actual' as is, which contains the stack trace: int main() { try { foo(); // Added for debugging: } catch (FooException e) { // Printing as is contains the stack trace: stderr.writeln(e.actual); return 1; } catch (Exception e) { stderr.writefln!"ERROR: %s"(e.msg); return 1; } return 0; } But really, it all depends on your program. The simplest thing may to not catch at all. The default behavior is to dump the stack trace and it works for some programs. Ali
Re: Docs generation example
On Saturday, 10 October 2020 at 02:07:02 UTC, Виталий Фадеев wrote: Wanted! Docs generation example. I have dub project, sources/*.d. I want html-index with all classes/functions. Is exists simple, hi-level, one-line command line solution ? Alternatively: 1. Publish the 'blablah' package on the DUB registry. 2. Navigate to the https://blablah.dpldocs.info/index.html URL
Re: vibe.d / experience / feedback
On Saturday, 10 October 2020 at 16:00:47 UTC, Alaindevos wrote: For a framework to be successful you need at least a minimum of sample code in public domain. For vibe it is none existent. I could not even find demo code doing a redirect which is the most basic stuff. The number of books on a framework is also an indication of the success after a few years. Compare for instance to Flask or rails. Also a framework must concentrate on one task. A web framework should not care about sql. Neither a GUI framework. For samples of redirect please see here https://github.com/vibe-d/vibe.d/search?q=redirect&type= But yes, I got your point. Vibe.d and a lot other projects in the D ecosystem are currently at the point where each community member could help to make a little improvement here and there (including documentation/ sample coding / blog posts ,...). Kind regards Andre
Re: List of exceptions?
On Saturday, 10 October 2020 at 16:37:23 UTC, Imperatorn wrote: On Saturday, 10 October 2020 at 12:12:35 UTC, DMon wrote: To clarify, do you want a list of *possible* exceptions, like in Java? Please. I've been looking and thinking that I'm over complicating it for myself so it may not be necessary. Instead use multiples of catch(Exception var) and writefln(var.some_method, var.another_method) for multiple exceptions.
Re: List of exceptions?
On Saturday, 10 October 2020 at 12:12:35 UTC, DMon wrote: Is there a list of a list of the exceptions or what can be used with catch? I'm thinking that I missed it and there is something easier than breaking old code, scouring the site, or hypnotic regression. To clarify, do you want a list of *possible* exceptions, like in Java?
Re: vibe.d / experience / feedback
On Thursday, 1 October 2020 at 06:32:23 UTC, Robert M. Münch wrote: Hi, we are currently using vibe.d for a prototype and I want to post some experiences. I know one shouldn't only address the problems but provide some solutions. [...] Have you tried Diamond? https://code.dlang.org/packages/diamond
Re: List of exceptions?
On Saturday, 10 October 2020 at 16:00:26 UTC, Ali Çehreli wrote: On 10/10/20 8:46 AM, DMon wrote: On Saturday, 10 October 2020 at 14:56:31 UTC, Ali Çehreli wrote: On 10/10/20 5:12 AM, DMon wrote: Is there a list of a list of the exceptions or what can be used with catch? Only Throwable and classes that are derived from it can be thrown and caught. Ali Is explicite cathing the best practice or is implicite how its done? I don't know implicit catching. I would like to learn from others. Ali Maybe those are the wrong words. catch (Exception e) // implicit (any exception) catch (ConvException f) // explicit (conversion only) Or is that not correct? You wrote a book and I'm typing snippets.
Re: vibe.d / experience / feedback
For a framework to be successful you need at least a minimum of sample code in public domain. For vibe it is none existent. I could not even find demo code doing a redirect which is the most basic stuff. The number of books on a framework is also an indication of the success after a few years. Compare for instance to Flask or rails. Also a framework must concentrate on one task. A web framework should not care about sql. Neither a GUI framework.
Re: List of exceptions?
On 10/10/20 8:46 AM, DMon wrote: On Saturday, 10 October 2020 at 14:56:31 UTC, Ali Çehreli wrote: On 10/10/20 5:12 AM, DMon wrote: Is there a list of a list of the exceptions or what can be used with catch? Only Throwable and classes that are derived from it can be thrown and caught. Ali Thanks for the reply. I am looking to catch exceptions explicitly and get that it does not have to be. Is explicite cathing the best practice or is implicite how its done? I don't know implicit catching. I would like to learn from others. I think the common rules are: - Don't catch anything - Unless you can do something about it (e.g. ask the user something to retry, augment it, etc.) I almost always catch in main() (or a thread's main function) and only to print a clean error message: void main() { try { // ... } catch (Exception e) { stderr.writefln!"ERROR: %s"(e.msg); } } That's it for me. :) Ali
Re: List of exceptions?
On Saturday, 10 October 2020 at 14:56:31 UTC, Ali Çehreli wrote: On 10/10/20 5:12 AM, DMon wrote: Is there a list of a list of the exceptions or what can be used with catch? Only Throwable and classes that are derived from it can be thrown and caught. Ali Thanks for the reply. I am looking to catch exceptions explicitly and get that it does not have to be. Is explicite cathing the best practice or is implicite how its done?
Re: List of exceptions?
On 10/10/20 5:12 AM, DMon wrote: Is there a list of a list of the exceptions or what can be used with catch? I'm thinking that I missed it and there is something easier than breaking old code, scouring the site, or hypnotic regression. Only Throwable and classes that are derived from it can be thrown and caught. It has two decendants: Throwable / \ Error Exception Throwable and Error are caught very rarely in special situations. For example, a thread may catch all types of exceptions to report the reason why it's about to die. So the only type that is and should be used in most programs is Exception: void main() { try { foo(); } catch (Exception e) { writefln!"Something bad happened: %s"(e.msg); // You can continue if it makes sense } } So, programs base their exceptions on Exception: class MyException : Exception { this(int i) { import std.format; super(format!"Bad int happened: %s"(i)); } } void foo() { throw new MyException(42); } void main() { foo(); } There are helpers in std.exception e.g. class MyException : Exception { import std.exception; mixin basicExceptionCtors; } void foo() { throw new MyException("a problem happened"); } void main() { foo(); } I have some basic information here: http://ddili.org/ders/d.en/exceptions.html Ali
Re: Win32Api GetDlgItemText How make buffer with no fixed size?
On Saturday, 10 October 2020 at 10:15:03 UTC, Marcone wrote: wchar[100] buffer; // I don't want fixed size :( wchar[] buffer; // no fixed size buffer.length = GetWindowTextLength(hwn); // set it to the text length of the window // now get the text GetDlgItemText(hwn, widget, buffer.ptr, buffer.length); Use buffer.length instead of buffer.sizeof in D.
List of exceptions?
Is there a list of a list of the exceptions or what can be used with catch? I'm thinking that I missed it and there is something easier than breaking old code, scouring the site, or hypnotic regression.
Win32Api GetDlgItemText How make buffer with no fixed size?
wchar[100] buffer; // I don't want fixed size :( GetDlgItemText(hwn, widget, buffer.ptr, buffer.sizeof);
Re: Docs generation example
On Saturday, 10 October 2020 at 05:04:54 UTC, Anonymouse wrote: On Saturday, 10 October 2020 at 02:07:02 UTC, Виталий Фадеев wrote: Wanted! Docs generation example. I have dub project, sources/*.d. I want html-index with all classes/functions. Is exists simple, hi-level, one-line command line solution ? dub run adrdox -- -i sources Files will be in generated-docs/ Nice ! https://github.com/vitalfadeev/dlang-doc-example/raw/main/docs.png Thank a lot !