www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Importing struct

reply Andrey <saasecondbox yandex.ru> writes:
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
parent reply evilrat <evilrat666 gmail.com> writes:
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
parent reply evilrat <evilrat666 gmail.com> writes:
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
parent reply Andrey <saasecondbox yandex.ru> writes:
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
next sibling parent evilrat <evilrat666 gmail.com> writes:
On Monday, 13 August 2018 at 13:09:24 UTC, Andrey wrote:
 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?)
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.html
Aug 13 2018
prev sibling parent reply Mike Parker <aldacron gmail.com> writes:
On Monday, 13 August 2018 at 13:09:24 UTC, Andrey wrote:
 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?)
The convention is to use lowercase for the module name: module myclass; struct MyClass {}
Aug 13 2018
parent reply Timoses <timosesu gmail.com> writes:
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:
 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?)
The convention is to use lowercase for the module name: module myclass; struct MyClass {}
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.
Aug 13 2018
parent Andrey <saasecondbox yandex.ru> writes:
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:
 On Monday, 13 August 2018 at 13:09:24 UTC, Andrey wrote:
 [...]
The convention is to use lowercase for the module name: module myclass; struct MyClass {}
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.
I understood, thank you!
Aug 14 2018