Re: question about call cpp class constructer without new , and define cpp delegate
On Thursday, 27 June 2019 at 17:00:01 UTC, Rémy Mouëza wrote: I though support for C++ allocation had improved. In a recent release, there was the addition of core.stdcpp.new, but I didn't try it out: - http://dpldocs.info/experimental-docs/core.stdcpp.new_.html - https://en.cppreference.com/w/cpp/memory/new/operator_new It seems at this moment these operators only supported on Windows. Are there still pitfalls to be wary of? Not possible: - new/delete (not entirely impossible, but requires to dig in compiler/library internals, generally fragile and not portable, not even mention that deciphering STL code is just meh...), though many libraries provides their own allocator/deallocator functions and some even provides pluggable hooks to use. - Member functions pointers - Multiple inheritance (tbh this is considered a bad practice anyway and chances to encounter it relatively small) - lambdas? - Exceptions? (except maybe on Windows, because of SEH) - Template instantiations code gen(this basically requires to implement entire C++ compiler), what this means is that if the concrete template instance isn't used in a library you use you have to make dummy C++ function that forces compiler to emit the code for it. Possible, but annoying: - Functions with ref parameters - Functions with const pointers to mutable data parameters ex: float calcStuff(float * const arr, size_t len); It doesn't make sense in D, and I doubt there is much sense in C++ as well (except maybe to convey the meaning that this function won't try to free data). So the workaround of course is to slap pragma mangle, which of course requires manually getting the mangled name for the function... On Windows using MS compiler for the example above the mangled name will look like float calc(float * const arr); // ?calc@@YAMQEAM@Z and for non const float calc(float * arr); // ?calc@@YAMPEAM@Z so it is possible to do something like this pragma(mangle, calc.mangleof.replace("QEA","PEA")) float calc(float * arr); - Some operator overloads also needs pragma mangle treatment. - Incomplete/buggy mangling support, especially annoying with templates. Same treatment. Maybe I've missed something else though, but anything not on the list usually works without surprises. And thanks to string namespaces it is now such a relief to use.
Re: dll
For somebody who isn't familiar - what's the issue exactly?
Re: dll
On Thursday, 27 June 2019 at 16:56:00 UTC, fred wrote: https://forum.dlang.org/thread/osnema$d5s$1...@digitalmars.com dll support is it ok now? i cant find the docs on thatm Not much better.
Re: Setting default values for Main function's args Array
On Thursday, 27 June 2019 at 17:22:36 UTC, Paul Backus wrote: On Thursday, 27 June 2019 at 17:20:37 UTC, Paul Backus wrote: void main(string[] args) { string[] defaultArgs = ["my", "default", "arguments"]; if (args.length == 0) { args = defaultArgs; } // Process args... } Correction: you should check for `args.length == 1`, since (as Adam points out) the name of the program will be passed as args[0]. I'm feeling, that overwriting the zero argument that is containing the program's path is mostly never a good idea. Here, zero argument will not be overwritten. Program.d import std.stdio : writeln; void main(string[] args) { string[] defaultArgs = [args[0], "default", "arguments"]; if (args.length == 1) { args = defaultArgs; } // Process args... writeln("", args); } Output: vaidas@vaidas-SATELLITE-L855:~/Desktop$ rdmd program.d ["/tmp/.rdmd-1000/rdmd-program.d-7E2D9881B29D67DB2D97D001FFD2817D/program", "default", "arguments"]
Re: Setting default values for Main function's args Array
On Thursday, 27 June 2019 at 17:05:05 UTC, Vaidas wrote: Is it possible to set the default values for the Main function's arguments? It seems that I'm getting Range error. import std.stdio : writeln; void main(string[] args = ["asdsfasdf", "asdklfajsdk", "asdfasdfasd"]){ writeln("", args[1]); } Output: vaidas@vaidas-SATELLITE-L855:~/Desktop$ rdmd newfile.d core.exception.RangeError@newfile.d(4): Range violation ??:? _d_arrayboundsp [0x555f5b79f8e9] ??:? _Dmain [0x555f5b79e7ee] Your main function is receiving an empty array as an argument, which overrides the default argument. The correct way to do what you want to do is this: void main(string[] args) { string[] defaultArgs = ["my", "default", "arguments"]; if (args.length == 0) { args = defaultArgs; } // Process args... }
Re: Setting default values for Main function's args Array
On Thursday, 27 June 2019 at 17:20:37 UTC, Paul Backus wrote: void main(string[] args) { string[] defaultArgs = ["my", "default", "arguments"]; if (args.length == 0) { args = defaultArgs; } // Process args... } Correction: you should check for `args.length == 1`, since (as Adam points out) the name of the program will be passed as args[0].
Re: Setting default values for Main function's args Array
On Thursday, 27 June 2019 at 17:05:05 UTC, Vaidas wrote: Is it possible to set the default values for the Main function's arguments? It seems that I'm getting Range error. import std.stdio : writeln; void main(string[] args = ["asdsfasdf", "asdklfajsdk", "asdfasdfasd"]){ writeln("", args[1]); } Output: vaidas@vaidas-SATELLITE-L855:~/Desktop$ rdmd newfile.d core.exception.RangeError@newfile.d(4): Range violation ??:? _d_arrayboundsp [0x555f5b79f8e9] ??:? _Dmain [0x555f5b79e7ee] consider this: module d-program; void main(string[] args) { import std.stdio; writeln("args = ", args); } --- ~/> ./d-program abc def Output: args = ["~/d-program", "abc", "def"] --- ~/> ./d-program Output: args = ["~/d-program"]
Re: Setting default values for Main function's args Array
On Thursday, 27 June 2019 at 17:05:05 UTC, Vaidas wrote: Is it possible to set the default values for the Main function's arguments? No, as far as the language is concerned, a value is always being passed from the operating system, so those default values would never trigger. What you could do though is just check inside main: void main(string[] args) { if(args.length <= 1) args = ["defaults", "here"]; } Keep in mind that args[0] is almost always set to the name of the executable, so length == 0 is liekly never going to happen.
Setting default values for Main function's args Array
Is it possible to set the default values for the Main function's arguments? It seems that I'm getting Range error. import std.stdio : writeln; void main(string[] args = ["asdsfasdf", "asdklfajsdk", "asdfasdfasd"]){ writeln("", args[1]); } Output: vaidas@vaidas-SATELLITE-L855:~/Desktop$ rdmd newfile.d core.exception.RangeError@newfile.d(4): Range violation ??:? _d_arrayboundsp [0x555f5b79f8e9] ??:? _Dmain [0x555f5b79e7ee]
Re: question about call cpp class constructer without new , and define cpp delegate
On Thursday, 27 June 2019 at 05:57:49 UTC, evilrat wrote: On Thursday, 27 June 2019 at 05:37:08 UTC, ChangLoong wrote: If I want call cpp class constructer without new method, is there a way to do that ? If what you really want is to actually allocate using C++ new operator from D, then that is very problematic and not portable even across compilers on same OS. If C++ side has poor design around this specific issue and expects passed object to be delete'd (using the C++ delete operator) later then you are in trouble. In that case you have to make simple wrapper on C++ side to be able to call new/delete from D. I though support for C++ allocation had improved. In a recent release, there was the addition of core.stdcpp.new, but I didn't try it out: - http://dpldocs.info/experimental-docs/core.stdcpp.new_.html - https://en.cppreference.com/w/cpp/memory/new/operator_new Are there still pitfalls to be wary of?
dll
https://forum.dlang.org/thread/osnema$d5s$1...@digitalmars.com dll support is it ok now? i cant find the docs on thatm
Re: Reading formatted file
On Thursday, 27 June 2019 at 13:52:43 UTC, a11e99z wrote: On Thursday, 27 June 2019 at 13:31:23 UTC, Tabamon wrote: 1) try to google "dlang read text file", most probably 1,2,3 link will help to u. 2) https://dlang.org/library/std/file/read_text.html string content = readText( fileName ); 3) for matrix reading better to use byLine() https://dlang.org/library/std/stdio/file.by_line.html (see the samples below)
Re: Reading formatted file
On Thursday, 27 June 2019 at 13:31:23 UTC, Tabamon wrote: I am new at D, I'm making a sudoku solver in D, I wanted to add the option to read sudoku from a .txt file. 1) try to google "dlang read text file", most probably 1,2,3 link will help to u. 2) https://dlang.org/library/std/file/read_text.html string content = readText( fileName );
Reading formatted file
Hello World! I am new at D, I'm making a sudoku solver in D, I wanted to add the option to read sudoku from a .txt file. I am finding a bit confusing how to open, read and close files in D. I am trying to mimic the C code: FILE *f_in; f_in=fopen("sudoku.txt"); if (f_in==NULL){ exit(1); } while(ch=fgetc() != EOF){ //convert char to int... put char in matrix... } could you help me? thank you, I appreciate it! -Tabamon.
Re: Illegal Filename after basic install and trying Hello World
On Wednesday, June 26, 2019 8:39:09 AM MDT Nicholas Wilson via Digitalmars- d-learn wrote: > On Wednesday, 26 June 2019 at 13:57:22 UTC, Gilbert Fernandes > > None do help. The option "override linker settings from sc.ini" > > it may be called dmd.conf (it is on my Mac, but the windows may > be different) On Windows, it's sc.ini, whereas it's dmd.conf on every other platform. I have no clue why Windows is different from the rest. - Jonathan M Davis
Re: question about call cpp class constructer without new , and define cpp delegate
On Thursday, 27 June 2019 at 05:37:08 UTC, ChangLoong wrote: If I want call cpp class constructer without new method, is there a way to do that ? If what you really want is to actually allocate using C++ new operator from D, then that is very problematic and not portable even across compilers on same OS. If C++ side has poor design around this specific issue and expects passed object to be delete'd (using the C++ delete operator) later then you are in trouble. In that case you have to make simple wrapper on C++ side to be able to call new/delete from D. If all you want is to allocate memory for object(existing buffer, malloc, etc..) and place it there you can use emplace function and call ctor later (see below) https://dlang.org/phobos/core_lifetime.html#.emplace , or there was one in "object" module IIRC Otherwise it is also possible to just call constructors manually using its internal name myObj.__ctor(..params..) / this.__ctor(...) (destructors also possible, see __dtor/__xdtor. hint: __dtor is probably not what you want, read the docs first) And finally to just allocate with GC using D new operator auto myObj = new MyClass(...); Just make sure that this object won't be delete'd from C++ and also if the cpp api accept a delegate as parameter, how to create one from d and pass to cpp ? Probably not possible. There are no delegates in C++, instead it has pointers to member functions and limited lambdas, and there is no analogs in D. You can try to craft it somehow to be ABI compatible, but probably easier to just make simple wrapper on C++ side. IIRC member pointers is just pointer, and you provide 'this' context on call, while in D delegate is 2 pointers - context AND function