digitalmars.D.learn - Importing struct
- Andrey (31/31) Aug 13 2018 Hello,
- evilrat (10/41) Aug 13 2018 That's just name collision, because module is a valid symbol you
- evilrat (8/11) Aug 13 2018 this also should work
- Andrey (4/6) Aug 13 2018 Hmm, I thought that name of class should match name of file...
- evilrat (12/18) Aug 13 2018 Modules usually named after their purpose.
- Mike Parker (4/10) Aug 13 2018 The convention is to use lowercase for the module name:
Hello, This is my test project: source/app.d source/MyClass.d app.d: ------------------------------------------------ import std.stdio; import MyClass; void main(string[] args) { MyClass.MyClass.parse(args); // I want just MyClass.parse(args); } ------------------------------------------------ MyClass.d ------------------------------------------------ import std.stdio; struct MyClass { static void parse(string[] args) { } } ------------------------------------------------ In "main" function I need to write "MyClass.MyClass.parse(args)" bit I want just "MyClass.parse(args)". If I do so then I have an error: Error: undefined identifier parse in module MyClass. Of course function "parse" doesn't exist in this module. But it exists inside struct "MyClass"! What should I do to import my struct correctly?
Aug 13 2018
On Monday, 13 August 2018 at 12:34:25 UTC, Andrey wrote:Hello, This is my test project: source/app.d source/MyClass.d app.d: ------------------------------------------------ import std.stdio; import MyClass; void main(string[] args) { MyClass.MyClass.parse(args); // I want just MyClass.parse(args); } ------------------------------------------------ MyClass.d ------------------------------------------------ import std.stdio; struct MyClass { static void parse(string[] args) { } } ------------------------------------------------ In "main" function I need to write "MyClass.MyClass.parse(args)" bit I want just "MyClass.parse(args)". If I do so then I have an error: Error: undefined identifier parse in module MyClass. Of course function "parse" doesn't exist in this module. But it exists inside struct "MyClass"! What should I do to import my struct correctly?That's just name collision, because module is a valid symbol you need to be specific about what you are trying to access. Try selective import instead import MyClass : MyClass; Of course if there is more symbols needed you have to specify them as well after a comma. Another option to save up on typing is renamed imports import mc = MyClass; mc.MyClass.parse(...)
Aug 13 2018
On Monday, 13 August 2018 at 12:44:44 UTC, evilrat wrote:Another option to save up on typing is renamed imports import mc = MyClass; mc.MyClass.parse(...)this also should work import mc = MyClass; alias MyClass = mc.MyClass; // make synonym // now it is just MyClass MyClass.parse(...) however the best option is simply avoid naming anything with same name as module.
Aug 13 2018
On Monday, 13 August 2018 at 13:05:28 UTC, evilrat wrote:however the best option is simply avoid naming anything with same name as module.Hmm, I thought that name of class should match name of file... And how to name a file that contains only one class/struct? Like in my case. What usually true D coders do?)
Aug 13 2018
On Monday, 13 August 2018 at 13:09:24 UTC, Andrey wrote:On Monday, 13 August 2018 at 13:05:28 UTC, evilrat wrote:Modules usually named after their purpose. Like std.stdio for IO stuff, std.file for file utilities, and complex example is std.algorithm.search for various searching algorithms which is a part of more global std.algorithm package (importing it also brings in several other std.algorithm.* modules). Not sure about your use case, but seems like parser.d or args.d or argsparser.d (names best to keep lower case in case of OS or FS specific quirks, this is the convention) Also if you need ready to use arguments parser check this one https://dlang.org/phobos/std_getopt.htmlhowever the best option is simply avoid naming anything with same name as module.Hmm, I thought that name of class should match name of file... And how to name a file that contains only one class/struct? Like in my case. What usually true D coders do?)
Aug 13 2018
On Monday, 13 August 2018 at 13:09:24 UTC, Andrey wrote:On Monday, 13 August 2018 at 13:05:28 UTC, evilrat wrote:The convention is to use lowercase for the module name: module myclass; struct MyClass {}however the best option is simply avoid naming anything with same name as module.Hmm, I thought that name of class should match name of file... And how to name a file that contains only one class/struct? Like in my case. What usually true D coders do?)
Aug 13 2018
On Monday, 13 August 2018 at 14:16:47 UTC, Mike Parker wrote:On Monday, 13 August 2018 at 13:09:24 UTC, Andrey wrote:Using lowercase letters for module names (and their respective file names!!) also prevents weird errors when different OSes treat lower/capital case letters differently which can sometimes lead to modules not being found. So from my experience: always use lower-case letters for modules and the file names.On Monday, 13 August 2018 at 13:05:28 UTC, evilrat wrote:The convention is to use lowercase for the module name: module myclass; struct MyClass {}however the best option is simply avoid naming anything with same name as module.Hmm, I thought that name of class should match name of file... And how to name a file that contains only one class/struct? Like in my case. What usually true D coders do?)
Aug 13 2018
On Monday, 13 August 2018 at 14:30:36 UTC, Timoses wrote:On Monday, 13 August 2018 at 14:16:47 UTC, Mike Parker wrote:I understood, thank you!On Monday, 13 August 2018 at 13:09:24 UTC, Andrey wrote:Using lowercase letters for module names (and their respective file names!!) also prevents weird errors when different OSes treat lower/capital case letters differently which can sometimes lead to modules not being found. So from my experience: always use lower-case letters for modules and the file names.[...]The convention is to use lowercase for the module name: module myclass; struct MyClass {}
Aug 14 2018