www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - First D project, problem with error messages

reply Daniel <d gmail.com> writes:
         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
next sibling parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
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
next sibling parent Daniel <d gmail.com> writes:
On Thursday, 9 February 2023 at 19:42:23 UTC, Steven 
Schveighoffer wrote:
 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. ====== -Steve
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 !!!
Feb 09 2023
prev sibling parent reply Walter Bright <newshound2 digitalmars.com> writes:
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
next sibling parent evilrat <evilrat666 gmail.com> writes:
On Saturday, 11 February 2023 at 05:28:11 UTC, Walter Bright 
wrote:
 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.
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.
Feb 10 2023
prev sibling parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 2/11/23 12:28 AM, Walter Bright wrote:
 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.
Yes, there are some advantages for functions, because functions don't require explicit instantiation. -Steve
Feb 11 2023
prev sibling next sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 2/9/23 11:37, Daniel wrote:

 problems with error messages
I 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
prev sibling parent reply WebFreak001 <d.forum webfreak.org> writes:
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
parent WebFreak001 <d.forum webfreak.org> writes:
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