digitalmars.D.learn - string object won't compile
- askjfbd (23/23) Mar 05 2018 Someone please tell me how, for I am a newbie and don't know any
- Adam D. Ruppe (15/16) Mar 05 2018 The problem is you named the file string.d and didn't give a
- askjfbd (6/23) Mar 05 2018 Thanks for the precise advice. How foolish I was and how quickly
- psychoticRabbit (19/25) Mar 05 2018 This is mistake I made too, when i began using D.
- askjfbd (5/30) Mar 05 2018 Thank you. I will learn about modules from now on. I thought I
Someone please tell me how, for I am a newbie and don't know any solutions even to this very simple problem. As I learned dlang using the Dlang tour page, I stuck at the alias & Strings page. I have tried to compile the following simple code many times but still get the same error both from dmd and gdc. They both say that <---------------------------------------------------- string.d:9:9: error: module string is used as a type string str = "This is a test."; ----------------------------------------------------> whenever I tried compiling this code below <------------------------------------------ import std.stdio; import std.string; void main() { //alias string = immutable(char)[]; string str = "This is a test."; } -------------------------------------------> However, when I uncomment the alias~ line above, both dmd and gdc can compile the code. I thought that the string object is already defined in the package or somewhere beforehand, but am I wrong?
Mar 05 2018
On Monday, 5 March 2018 at 23:34:50 UTC, askjfbd wrote:string.dThe problem is you named the file string.d and didn't give a `module xxxxx;` statement in the code, so the compiler assumed the module is named after the file.... and thus introduced a local name `string` referring to the module, overriding the built in one. You can still refer to the built in one by saying `.string foo = "bar";` - yes, the leading dot, which tells it to use the global instead of local name. Or by putting `module mytest.string;` at the top, so the module has a two-part name (I recommend this in all cases anyway btw, it is the best way to avoid conflicts as you import more modules). Or just renaming the file from string.d to almost_anything_else.d, which will also avoid the local name override.
Mar 05 2018
On Monday, 5 March 2018 at 23:42:59 UTC, Adam D. Ruppe wrote:On Monday, 5 March 2018 at 23:34:50 UTC, askjfbd wrote:Thanks for the precise advice. How foolish I was and how quickly you gave me the answer! I'll have to learn a lot more about D and more about modules. Since C and common lisp, which I have learned so far, have almost nothing to do with name conflicts, I couldn't imagine that I was still wrong. :) Thank you.string.dThe problem is you named the file string.d and didn't give a `module xxxxx;` statement in the code, so the compiler assumed the module is named after the file.... and thus introduced a local name `string` referring to the module, overriding the built in one. You can still refer to the built in one by saying `.string foo = "bar";` - yes, the leading dot, which tells it to use the global instead of local name. Or by putting `module mytest.string;` at the top, so the module has a two-part name (I recommend this in all cases anyway btw, it is the best way to avoid conflicts as you import more modules). Or just renaming the file from string.d to almost_anything_else.d, which will also avoid the local name override.
Mar 05 2018
On Monday, 5 March 2018 at 23:34:50 UTC, askjfbd wrote:Someone please tell me how, for I am a newbie and don't know any solutions even to this very simple problem. As I learned dlang using the Dlang tour page, I stuck at the alias & Strings page. I have tried to compile the following simple code many times but still get the same error both from dmd and gdc. They both say that...This is mistake I made too, when i began using D. I quickly got into the habit of putting module test; at the beginning of every file I create, unless I determine a better module name. Actually, I wrote my own IDE, so it does that for me whenever I select a new D file. Personally, I think that is one of the first things newcomers need to learn about - modules. ------------- "Modules have a one-to-one correspondence with source files. The module name is, by default, the file name with the path and extension stripped off, and can be set explicitly with the module declaration." "Modules automatically provide a namespace scope for their contents." https://dlang.org/spec/module.html ------------
Mar 05 2018
On Tuesday, 6 March 2018 at 00:18:14 UTC, psychoticRabbit wrote:On Monday, 5 March 2018 at 23:34:50 UTC, askjfbd wrote:Thank you. I will learn about modules from now on. I thought I was right until you two replied to me. ;) Can you make your own IDE? That's great! If it were OpenSource, I would gladly download it for myself to avoid this sort of annoying experience. Thanks.Someone please tell me how, for I am a newbie and don't know any solutions even to this very simple problem. As I learned dlang using the Dlang tour page, I stuck at the alias & Strings page. I have tried to compile the following simple code many times but still get the same error both from dmd and gdc. They both say that...This is mistake I made too, when i began using D. I quickly got into the habit of putting module test; at the beginning of every file I create, unless I determine a better module name. Actually, I wrote my own IDE, so it does that for me whenever I select a new D file. Personally, I think that is one of the first things newcomers need to learn about - modules. ------------- "Modules have a one-to-one correspondence with source files. The module name is, by default, the file name with the path and extension stripped off, and can be set explicitly with the module declaration." "Modules automatically provide a namespace scope for their contents." https://dlang.org/spec/module.html ------------
Mar 05 2018