Re: Copying and moving directories
V Thu, 16 Feb 2017 16:38:51 + Chris via Digitalmars-d-learn napsáno: > In `std.file`, I haven't found a function that allows me to move > or at least copy directories, as in `mv dir /toDir`. Do I have to > go the awkward way over `rename` or something? http://forum.dlang.org/post/uzoxwrxsgeazugqoh...@forum.dlang.org
Re: User imput string int and float[DOUBT]
On Thursday, 16 February 2017 at 22:44:58 UTC, Ali Çehreli wrote: On 02/16/2017 02:05 PM, Jean Cesar wrote: > So I used get methods and sets only as initial pattern to netender the > functioning of the language in relation to some concepts of the same Makes sense... > how to leave a very small code with the largest number of > Possible functionality type I think D is very suitable for that. > I still do not know very well or use > constructors in C ++ Understandable: Many coding guidelines eschew doing non-trivial work in constructors. They require a member function like obj.initialize(/* ... */) to be called in order to get a functioning object. > but I have very high potential in a code with > multiple inheritance That's going to be a problem because D does not allow multiple inheritance. > I think of compilers in the case of the code that > favors me in reading so I would do something like: > > void main () > { >minhaclasse c = new minhaclasse(string text); >minhaclasse d = new minhaclasse(int number); > > write("Enter your name: ") > c.set(); So, your minhaclasse is basically ValorLegível (ReadableValue), which would not scale because likely it's also writable and movable, etc. And that explains why you're looking for multiple inheritance. :) // NOT valid D (and no, I don't speak Portuguese) class MinhaValor : ValorLegível, ValorEscrita, ValorMóvel /*, ... */ { // ... } > void main () > { > string txt; >Int num; > write("Enter your name: ") > minhaclasse(text).set(); > > write("Enter your age: ") > minhaclasse(num).set(); > > writeln >( > "\n\tString:", minhaclasse(text).print() ;, > "\n\tInt:", minhaclasse(num).print(); >); > } > > I think of object orientation this way to avoid getting rewritten many > things so I would only define what the set or get would return by > initializing the constructor only but I have no idea how to do that .. You make it sound as if OOP is for code reuse or for reducing code repetition. I think regular functions provide that already. Unless polymorphism is really beneficial, functional style is preferable. Additionally, D has this very useful universal function call syntax (UFCS), which makes your use case easy to implement, and which my earlier code could have benefited from as well. import std.stdio; import std.traits; auto read(T)(ref T t, string message) if (!isSomeString!T) { writef("%s: ", message); readf(" %s", &t); return t; } auto read(S)(ref S s, string message) if (isSomeString!S) { import std.string : strip; writef("%s: ", message); s = readln().strip(); return s; } class person { private: string name, address; int age; float height; public: static person fromConsole() { auto p = new person(); /* UFCS in action: Note how these are not written as *read(p.name, /* ... */) */ p.name.read("Enter Your Name"); p.age.read("Enter Your Age"); p.height.read("Enter Your Height"); return p; } float getHeight() { return height; } int getIty() { return age; } string getNome() { return name; } } void main () { person p = person.fromConsole(); writeln(p.getNome()); writeln(p.getIty()); writeln(p.getHeight()); } > My goal in learning to use languages like Java, C ++, D is with the > intention of learning the best way to reuse code and orienation to > objects and also development cross-platform codes that will run in > standard ansi for, Unix, Linux, Windows, android etc. .. Ali I tried to define a method to read vectors of chars but this is giving error auto read(C)(ref C c, char[] message) if (!isSomeChar!C) { writef("\n\t%s: ", message); read(" %s", &c); return c; }
Flat file CMS in D?
I was wondering, what would be the D equivalent of a flat file (as opposed to database driven) content management system?
Re: isNumeric bugfix or 2.072 regression?
On Thursday, 16 February 2017 at 23:15:09 UTC, Johan Engelen wrote: Hi all, `isNumeric!string` no longer works like it did in 2.071 when both std.string and std.traits are imported. https://issues.dlang.org/show_bug.cgi?id=17190
isNumeric bugfix or 2.072 regression?
Hi all, `isNumeric!string` no longer works like it did in 2.071 when both std.string and std.traits are imported. This code no longer compiles with 2.072: ```d // RUN: dmd -c test.d import std.string; import std.traits; void foo() { static if (isNumeric!string) {} } ``` The error is (dlang 2.072 and 2.073): isnum.d(6): Error: std.traits.isNumeric(T) at /Library/D/dmd/src/phobos/std/traits.d(5350) conflicts with std.string.isNumeric(S)(S s, bool bAllowSep = false) if (isSomeString!S || isRandomAccessRange!S && hasSlicing!S && isSomeChar!(ElementType!S) && !isInfinite!S) at /Library/D/dmd/src/phobos/std/string.d(5844) dlang 2.070 and 2.071 compile it fine. Is this part of fixing import issues and the above code is just broken, or is it a bug? - Johan
Re: User imput string int and float[DOUBT]
On 02/16/2017 02:05 PM, Jean Cesar wrote: > So I used get methods and sets only as initial pattern to netender the > functioning of the language in relation to some concepts of the same Makes sense... > how to leave a very small code with the largest number of > Possible functionality type I think D is very suitable for that. > I still do not know very well or use > constructors in C ++ Understandable: Many coding guidelines eschew doing non-trivial work in constructors. They require a member function like obj.initialize(/* ... */) to be called in order to get a functioning object. > but I have very high potential in a code with > multiple inheritance That's going to be a problem because D does not allow multiple inheritance. > I think of compilers in the case of the code that > favors me in reading so I would do something like: > > void main () > { >minhaclasse c = new minhaclasse(string text); >minhaclasse d = new minhaclasse(int number); > > write("Enter your name: ") > c.set(); So, your minhaclasse is basically ValorLegível (ReadableValue), which would not scale because likely it's also writable and movable, etc. And that explains why you're looking for multiple inheritance. :) // NOT valid D (and no, I don't speak Portuguese) class MinhaValor : ValorLegível, ValorEscrita, ValorMóvel /*, ... */ { // ... } > void main () > { > string txt; >Int num; > write("Enter your name: ") > minhaclasse(text).set(); > > write("Enter your age: ") > minhaclasse(num).set(); > > writeln >( > "\n\tString:", minhaclasse(text).print() ;, > "\n\tInt:", minhaclasse(num).print(); >); > } > > I think of object orientation this way to avoid getting rewritten many > things so I would only define what the set or get would return by > initializing the constructor only but I have no idea how to do that .. You make it sound as if OOP is for code reuse or for reducing code repetition. I think regular functions provide that already. Unless polymorphism is really beneficial, functional style is preferable. Additionally, D has this very useful universal function call syntax (UFCS), which makes your use case easy to implement, and which my earlier code could have benefited from as well. import std.stdio; import std.traits; auto read(T)(ref T t, string message) if (!isSomeString!T) { writef("%s: ", message); readf(" %s", &t); return t; } auto read(S)(ref S s, string message) if (isSomeString!S) { import std.string : strip; writef("%s: ", message); s = readln().strip(); return s; } class person { private: string name, address; int age; float height; public: static person fromConsole() { auto p = new person(); /* UFCS in action: Note how these are not written as *read(p.name, /* ... */) */ p.name.read("Enter Your Name"); p.age.read("Enter Your Age"); p.height.read("Enter Your Height"); return p; } float getHeight() { return height; } int getIty() { return age; } string getNome() { return name; } } void main () { person p = person.fromConsole(); writeln(p.getNome()); writeln(p.getIty()); writeln(p.getHeight()); } > My goal in learning to use languages like Java, C ++, D is with the > intention of learning the best way to reuse code and orienation to > objects and also development cross-platform codes that will run in > standard ansi for, Unix, Linux, Windows, android etc. .. Ali
Re: User imput string int and float[DOUBT]
On Thursday, 16 February 2017 at 02:17:49 UTC, Ali Çehreli wrote: On 02/15/2017 05:49 PM, Jean Cesar wrote: > So I'm a beginner in this language and have very little time I started > I'm interested in apprehending concepts of object orientation > polymorphism inheritance, multiple inheritance as in c ++ D is similar to C++ but also very different. > but I did not > understand how to use constructor in it > Because I simply did. > > Class person > { >person(){} >~ Person () {} > } > > And error ... In D, constructor is always called this(): class Person { this(){} ~this() {} } void main() { auto p = new Person(); } Ali So I used get methods and sets only as initial pattern to netender the functioning of the language in relation to some concepts of the same I intend to learn it not because it is a new language, but I want to understand how to leave a very small code with the largest number of Possible functionality type I still do not know very well or use constructors in C ++ but I have very high potential in a code with multiple inheritance, I think of compilers in the case of the code that favors me in reading so I would do something like: void main () { minhaclasse c = new minhaclasse(string text); minhaclasse d = new minhaclasse(int number); write("Enter your name: ") c.set(); write("Enter your age: ") d.set(); /* the set method would already fetch user i imput by mistake for the information automatically */ Writeln ( "\n\tString:", c.get (), "\n\tInt:", d.get () ); } Or something like: void main () { string txt; Int num; write("Enter your name: ") minhaclasse(text).set(); write("Enter your age: ") minhaclasse(num).set(); writeln ( "\n\tString:", minhaclasse(text).print() ;, "\n\tInt:", minhaclasse(num).print(); ); } I think of object orientation this way to avoid getting rewritten many things so I would only define what the set or get would return by initializing the constructor only but I have no idea how to do that .. My goal in learning to use languages like Java, C ++, D is with the intention of learning the best way to reuse code and orienation to objects and also development cross-platform codes that will run in standard ansi for, Unix, Linux, Windows, android etc. ..
Re: How to use D-language polymorphism?
On 02/16/2017 11:09 AM, Jean Cesar wrote: > I have the following code in c++ using polymorphism how would I convert > D-language polymorphism? import std.stdio; class Mamifero { int idade; void somMamifero() { writeln("\n\tSom de mamifero."); } } class Boi: Mamifero { override void somMamifero() const { writeln("\n\tMuu ..! Muu..!!"); } } class Gato: Mamifero { override void somMamifero() const { writeln("\n\tMiAu ..! MiAu..!!"); } } class Porco: Mamifero { override void somMamifero() const { writeln("\n\tOinc ..! Oinc..!!"); } } class Cachorro: Mamifero { override void somMamifero() const { writeln("\n\tAu ..! Au..!!"); } } void main() { Mamifero mamPtr; int op; while(op != 5) { write("\n\t(1) Boi" ~ "\n\t(2) Gato" ~ "\n\t(3) Porco" ~ "\n\t(4) Cachorro" ~ "\n\t(5) Sair" ~ "\n\tDigite: "); readf(" %s", &op); switch(op) { case 1:{ mamPtr = new Boi(); mamPtr.somMamifero(); break; } case 2:{ mamPtr = new Gato(); mamPtr.somMamifero(); break; } case 3:{ mamPtr = new Porco(); mamPtr.somMamifero(); break; } case 4:{ mamPtr = new Cachorro(); mamPtr.somMamifero(); break; } case 5:{ writeln("\n\tGood Bye"); return; } default: writeln("\n\tOpção Inválida ..!!!"); } } } In D you can have interfaces as well: interface Mamifero { void somMamifero(); } Ali
How to use D-language polymorphism?
I have the following code in c++ using polymorphism how would I convert D-language polymorphism? #include #include class Mamifero { protected: int idade; public: Mamifero(){} ~Mamifero(){} virtual void somMamifero() const { std::cout<<"\n\tSom de mamifero.\n"; } }; class Boi: public Mamifero { public: void somMamifero() const { std::cout<<"\n\tMuu ..! Muu..!!\n"; } }; class Gato: public Mamifero { public: void somMamifero() const { std::cout<<"\n\tMiAu ..! MiAu..!!\n"; } }; class Porco: public Mamifero { public: void somMamifero() const { std::cout<<"\n\tOinc ..! Oinc..!!\n"; } }; class Cachorro: public Mamifero { public: void somMamifero() const { std::cout<<"\n\tAu ..! Au..!!\n"; } }; int main() { Mamifero* mamPtr; int op; while(op != 5) { std::cout<<"\n\t(1) Boi" <<"\n\t(2) Gato" <<"\n\t(3) Porco" <<"\n\t(4) Cachorro" <<"\n\t(5) Sair" <<"\n\tDigite: "; std::cin>>op; switch(op) { case 1:{ mamPtr = new Boi(); mamPtr->somMamifero(); break; } case 2:{ mamPtr = new Gato(); mamPtr->somMamifero(); break; } case 3:{ mamPtr = new Porco(); mamPtr->somMamifero(); break; } case 4:{ mamPtr = new Cachorro(); mamPtr->somMamifero(); break; } case 5:{ std::cout<<"\n\tGood Bye\n\n"; exit(0); break; } default: std::cout<<"\n\tOpção Inválida ..!!!\n"; } } }
Re: Copying and moving directories
On Thursday, February 16, 2017 16:47:05 Chris via Digitalmars-d-learn wrote: > On Thursday, 16 February 2017 at 16:41:48 UTC, Adam D. Ruppe > > wrote: > > On Thursday, 16 February 2017 at 16:38:51 UTC, Chris wrote: > >> In `std.file`, I haven't found a function that allows me to > >> move or at least copy directories, as in `mv dir /toDir`. Do I > >> have to go the awkward way over `rename` or something? > > > > Have you already tried just renaming the directory? > > Yes, but that's a bit awkward. It'd be handier to have a function > like `mv(dir, toDir)`. Well, there's zero difference between renaming the file or directory and moving it. It's simply a difference in name. rename actually comes from POSIX, where rename is used in C code, and mv is used in the shell. So, I guess that you can blame POSIX. But there really isn't any reason to have a mv or move function in addition to rename. If you want mv instead, just alias rename to mv. However, I would point out that rename has the problem (at least on *nix - not sure about Windows) that it can't move across filesystem boundaries. I think that at some point, an alternative which did work across filesystem boundaries was proposed, and that may have been called move. It's not currently in Phobos though. - Jonathan M Davis
Re: Copying and moving directories
On Thursday, 16 February 2017 at 16:41:48 UTC, Adam D. Ruppe wrote: On Thursday, 16 February 2017 at 16:38:51 UTC, Chris wrote: In `std.file`, I haven't found a function that allows me to move or at least copy directories, as in `mv dir /toDir`. Do I have to go the awkward way over `rename` or something? Have you already tried just renaming the directory? Yes, but that's a bit awkward. It'd be handier to have a function like `mv(dir, toDir)`.
Re: Copying and moving directories
On Thursday, 16 February 2017 at 16:38:51 UTC, Chris wrote: In `std.file`, I haven't found a function that allows me to move or at least copy directories, as in `mv dir /toDir`. Do I have to go the awkward way over `rename` or something? Have you already tried just renaming the directory?
Copying and moving directories
In `std.file`, I haven't found a function that allows me to move or at least copy directories, as in `mv dir /toDir`. Do I have to go the awkward way over `rename` or something?
Re: opApply with Type Inference and Templates?
On 02/15/2017 07:20 PM, Jerry wrote: I am trying to do opApply to work when the delegate passed when it is and isn't nogc/nothrow. As soon as you involve a template though, type inference goes out the door. I want to be able to use opApply with templates (to get the auto @nogc/nothrow deducation passed on the delegate passed) but still be able to use type inference. Is there any way to do this? Related: http://forum.dlang.org/post/ovitindvwuxkmbxuf...@forum.dlang.org Ali
Re: opApply with Type Inference and Templates?
On Thursday, 16 February 2017 at 04:09:18 UTC, Basile B. wrote: No, by any chance do you ask this for the tuple unpacking PR ? If so I've also tried and failed. Was just trying to use a container I created in functions with and without @nogc/nothrow.
Re: Can't send messages to tid spawned in a Windows DLL. Bug?
On Thursday, 16 February 2017 at 15:14:25 UTC, kinke wrote: On Thursday, 16 February 2017 at 12:07:40 UTC, Atila Neves wrote: This fails for me in a DLL: auto tid = spawn(&func); assert(tid != Tid.init); If I print out the tid, I find that its message box is null. This is odd, since according the code in std.concurrency there's nothing weird about how it gets a message box, it's just `auto spawnTid = Tid(new MessageBox);`. So... `new` is returning null??? The really weird thing is that a thread is spawned and func starts executing. I just can't send it any messages without crashing. Atila If you suspect `new` of returning null, a GC issue seems likely. Is your DLL linked statically against druntime, thus having its own GC? Or are you using a shared druntime (and thus GC) across multiple binaries? Whatever's default on Windows 32-bit. The thing is, all other uses of GC allocations in the same DLL work as expected. Atila
Re: Can't send messages to tid spawned in a Windows DLL. Bug?
On Thursday, 16 February 2017 at 12:07:40 UTC, Atila Neves wrote: This fails for me in a DLL: auto tid = spawn(&func); assert(tid != Tid.init); If I print out the tid, I find that its message box is null. This is odd, since according the code in std.concurrency there's nothing weird about how it gets a message box, it's just `auto spawnTid = Tid(new MessageBox);`. So... `new` is returning null??? The really weird thing is that a thread is spawned and func starts executing. I just can't send it any messages without crashing. Atila If you suspect `new` of returning null, a GC issue seems likely. Is your DLL linked statically against druntime, thus having its own GC? Or are you using a shared druntime (and thus GC) across multiple binaries?
Re: Getting a segfault here, why?
On Thursday, 16 February 2017 at 09:18:16 UTC, aberba wrote: On linux it is pretty easy. Just compile with `-g` to dmd and run the program in gdb. Run till it crashes and it should tell you the file and line of where. Thanks, will try and see. Also don't forget to do this: http://vibed.org/docs#handling-segmentation-faults
Can't send messages to tid spawned in a Windows DLL. Bug?
This fails for me in a DLL: auto tid = spawn(&func); assert(tid != Tid.init); If I print out the tid, I find that its message box is null. This is odd, since according the code in std.concurrency there's nothing weird about how it gets a message box, it's just `auto spawnTid = Tid(new MessageBox);`. So... `new` is returning null??? The really weird thing is that a thread is spawned and func starts executing. I just can't send it any messages without crashing. Atila
Re: Get the address of an object, within the object itself
On 2017-02-15 22:42, Andrew Chapman wrote: Thanks Jonathan. Good point about the reference address. I can work around this quite easily, but I was curious. I will try the void* cast and see what happens. If it's only for printing you can use the C "printf" without any casting: import core.stdc.stdio; class Node { this() { printf("%p\n", this); } } void main() { new Node; } -- /Jacob Carlborg
Re: Getting a segfault here, why?
On Wednesday, 15 February 2017 at 18:22:53 UTC, Adam D. Ruppe wrote: On Wednesday, 15 February 2017 at 18:19:18 UTC, aberba wrote: Trying to find it but working with a debugger in D is not straight forward. Not yo talk of interpretating the debugger output. On linux it is pretty easy. Just compile with `-g` to dmd and run the program in gdb. Run till it crashes and it should tell you the file and line of where. Thanks, will try and see.