digitalmars.D - First D project, problem with error messages
- Daniel (53/53) Feb 09 2023 So I finally decided to start my first project using the
- Steven Schveighoffer (16/17) Feb 09 2023 You have made Lexer take compile-time parameters by adding the
- Daniel (6/13) Feb 09 2023 Fair enough, my intention was not to declare a template but a
- Walter Bright (3/6) Feb 10 2023 Adding () to functions turns them into templates, which can be be advant...
- evilrat (6/13) Feb 10 2023 It also can be used in header-only kind of libraries (a bindings
- Steven Schveighoffer (4/13) Feb 11 2023 Yes, there are some advantages for functions, because functions don't
- =?UTF-8?Q?Ali_=c3=87ehreli?= (4/5) Feb 09 2023 I would like to remind everyone that we have a Learn forum as well,
- WebFreak001 (18/19) Feb 09 2023 Thanks for showing the weird error that would be on module.
- WebFreak001 (8/11) Feb 09 2023 fixed, tagged new libdparse release:
So I finally decided to start my first project using the D language. So far it feels very clean, but I'm having problems with error messages that don't seem to point to the problematic place and syntax element. My setup is DMD 2.102 and VSCode with Code-d plugin. For example: ```d module lexer; import std.stdio; import token; class Lexer() { string sourceText; char currentChar; int charIndex; int line; int column; this(string sourceText) { this.sourceText = sourceText; this.currentChar = ''; this.charIndex = 0; this.line = 1; this.column = 1; } ``` Here, the word module is underlined in red and the message I get is "Primary expression expected (DScanner)". The solution is to write a character between the single quotes in the assignment to this.currentChar. (Saving and previewing this post shows an error at the place of the single quotes, but this is not the case in VSCode). I have already fixed a couple of similar errors with comparably puzzling messages. Now I am stuck with this: ```d module dpl0c; import std.stdio; import lexer; enum VERSION = "0.0.1"; void main(string[] args) { writefln("\nThe D PL0 Compiler %s", VERSION); string sourceText = "Hello, World!"; Lexer lex = new Lexer(sourceText); lex.sayHello(); } ``` > dmd dpl0c.d lexer.d token.d dpl0c.d(12): Error: template class `lexer.Lexer()` is used as a type without instantiation; to instantiate it use `Lexer!(arguments)` Of course new Lexer!(sourceText); doesn't solve the problem. Googling the error messages has been of little to no use so far. So I'm having a hard time getting used to the language features. However puzzling, it feels better than C/C++ anyway. Daniel
Feb 09 2023
On 2/9/23 2:37 PM, Daniel wrote:class Lexer() {You have made Lexer take compile-time parameters by adding the parentheses. And there are no parameters, so the use of this is quite narrow. In essence, your `Lexer` class is a *template*, which means it doesn't exist until it's instantiated with appropriate parameters. The appropriate parameters can only be `Lexer!()`, since it takes none. So your two options here are to do that (use `Lexer!()` instead of `Lexer`), or remove the parentheses, changing it to a regular class instead of a template. or Java or many others), then I would say just remove the template parameters. Having no parameters has limited use, mostly to allow it to be a mixin template, or to prevent it from being included in the binary unless it's used. -Steve
Feb 09 2023
On Thursday, 9 February 2023 at 19:42:23 UTC, Steven Schveighoffer wrote:On 2/9/23 2:37 PM, Daniel wrote:Fair enough, my intention was not to declare a template but a simple class, and I'm used to the Java syntax, so those innocent parentheses went unperceived. Thank you !!!class Lexer() {You have made Lexer take compile-time parameters by adding the parentheses. And there are no parameters, so the use of this is quite narrow. ====== -Steve
Feb 09 2023
On 2/9/2023 11:42 AM, Steven Schveighoffer wrote:Having no parameters has limited use, mostly to allow it to be a mixin template, or to prevent it from being included in the binary unless it's used.Adding () to functions turns them into templates, which can be be advantageous if you want the compiler to infer the return/scope attributes automatically.
Feb 10 2023
On Saturday, 11 February 2023 at 05:28:11 UTC, Walter Bright wrote:On 2/9/2023 11:42 AM, Steven Schveighoffer wrote:It also can be used in header-only kind of libraries (a bindings for example) for a helper functions, that otherwise will require linking that library in addition to the original library, which can be very annoying.Having no parameters has limited use, mostly to allow it to be a mixin template, or to prevent it from being included in the binary unless it's used.Adding () to functions turns them into templates, which can be be advantageous if you want the compiler to infer the return/scope attributes automatically.
Feb 10 2023
On 2/11/23 12:28 AM, Walter Bright wrote:On 2/9/2023 11:42 AM, Steven Schveighoffer wrote:Yes, there are some advantages for functions, because functions don't require explicit instantiation. -SteveHaving no parameters has limited use, mostly to allow it to be a mixin template, or to prevent it from being included in the binary unless it's used.Adding () to functions turns them into templates, which can be be advantageous if you want the compiler to infer the return/scope attributes automatically.
Feb 11 2023
On 2/9/23 11:37, Daniel wrote:problems with error messagesI would like to remind everyone that we have a Learn forum as well, where answers to such questions likely be more helpful in that forum. :) Ali
Feb 09 2023
On Thursday, 9 February 2023 at 19:37:44 UTC, Daniel wrote:[...]Thanks for showing the weird error that would be on module. The reason: libdparse (the D parser code-d / serve-d and all the other tools underlying it use) parses the code and the `''` is invalid according to D grammar, because inside `'x'` you always need to have exactly one character. This is emitted as invalid token directly at the lexing stage, the parser doesn't even know what it originally said in the code. However this error token didn't include location information, so it defaulted to 0 location. Now the parser emitted an error at the token location of the error token, because it expected a primary expression, but didn't get any valid token for one. The emitted error is now processed in code-d, it thinks the byte offset is 0 (is a valid offset), doesn't know a length, so it guesses by picking the keyword that's at location 0. I'm opening a PR to fix the location so at least this shouldn't be confusing anymore.
Feb 09 2023
On Thursday, 9 February 2023 at 21:32:03 UTC, WebFreak001 wrote:[...] I'm opening a PR to fix the location so at least this shouldn't be confusing anymore.fixed, tagged new libdparse release: https://github.com/dlang-community/libdparse/releases/tag/v0.21.3 serve-d has the fix included now, will appear in the next nightly release in ~4 hours: https://github.com/Pure-D/serve-d/ so now `module` won't be red underlined anymore with broken syntax like `x = '';` (I can't believe nobody spotted this earlier)
Feb 09 2023