multi-dimensional dynamic arrays
Strange to me that this compiles, since I would expect there to be some C-like limitation on the position of the unspecified dimension. Is allowing this somehow useful? int[1][][1] ub; writeln("ub",ub);
Modify Function Pointer to Take Additional Parameters
I'm trying to write a function that will adjust the parameters of a function pointer. In the code below, my goal is to call the function qux with a variety of different function pointers (in the actual application, I don't have the ability to modify qux). I created a function foo that I thought would adjust it properly. The problem is that the foo function converts the function pointer into a delegate. I was able to get something that works in this simple example by introducing a delegate alias and an alternate definition of qux that takes a delegate. However, in my actual application, I can't modify what the equivalent of qux would take as parameters. So I was just curious if there was any other alternative. alias fp1 = int function(int x); alias fp2 = int function(int x, int y); auto foo(T)(T f) { static if (is(T == fp2)) return f; else static if (is(T == fp1)) { return (int x, int y) => f(x); } else return 0; } int bar(int x) { return x; } int baz(int x, int y) { return x + y; } int qux(int x, int y, fp2 f) { return f(x, y); } void main() { import std.stdio : writeln; auto foo_bar = foo(); writeln(qux(1, 2, foo_bar)); //compiler error writeln(qux(1, 2, )); }
Re: Duration at runtime
On Friday, 19 February 2016 at 04:21:43 UTC, Zekereth wrote: On Friday, 19 February 2016 at 04:16:23 UTC, Adam D. Ruppe wrote: On Friday, 19 February 2016 at 04:08:02 UTC, Zekereth wrote: How is seconds able to be read at compile time but unitType cannot? "seconds" is a literal value that the compiler knows about. unitType is a variable that might change between its declaration and use (it doesn't here, but the compiler doesn't check if it actually does, just if it *can*), so the compiler doesn't allow it. Thanks a lot Adam! So is there a way around this?. I want duration to be configurable at runtime. Never mind I found a better solution to my problem by storing a Duration instead of the unitType. Works just fine. Thanks a lot I appreciate your help!
Re: Duration at runtime
On Friday, 19 February 2016 at 04:16:23 UTC, Adam D. Ruppe wrote: On Friday, 19 February 2016 at 04:08:02 UTC, Zekereth wrote: How is seconds able to be read at compile time but unitType cannot? "seconds" is a literal value that the compiler knows about. unitType is a variable that might change between its declaration and use (it doesn't here, but the compiler doesn't check if it actually does, just if it *can*), so the compiler doesn't allow it. Thanks a lot Adam! So is there a way around this?. I want duration to be configurable at runtime.
Re: Duration at runtime
On Friday, 19 February 2016 at 04:08:02 UTC, Zekereth wrote: How is seconds able to be read at compile time but unitType cannot? "seconds" is a literal value that the compiler knows about. unitType is a variable that might change between its declaration and use (it doesn't here, but the compiler doesn't check if it actually does, just if it *can*), so the compiler doesn't allow it.
Duration at runtime
I'm confused by the following: import std.stdio; import std.datetime; void main() { string unitType = "seconds"; auto seconds = 1; // auto myDur = dur!(unitType)(seconds); // Error unitType can't be read at compile time. auto myDur = dur!("seconds")(seconds); // Compiles why? } How is seconds able to be read at compile time but unitType cannot? Thanks!
Re: Arrays of noncopyables/Invalid memory operation
On Thu, Feb 18, 2016 at 05:18:33PM -0800, Ali Çehreli via Digitalmars-d-learn wrote: > On 02/18/2016 12:12 PM, Ali Çehreli wrote: > > > > Hmm, why does the destructor of object 60 run in the middle of > > > nowhere? > > > > It so happens that the GC decides to collect when moving the array > > to a new location at that point. If you add the following to the > > beginning of main, you will see that the next object to destroy is > > 59, then 58, etc. > > > > import core.memory; > > GC.disable; > > > > So, everything makes sense other than throwing in a destructor > > having issues with the current GC. :-/ > > No, it doesn't make sense to me anymore. > > All we're doing here is growing an array. There shouldn't be any > destructor calls when just doing that, no? As the array grows, the > elements should be "moved" to the new location; why the destructor? I > hope this is related to the recent fix. [...] Consider this: struct HasDtor { int data; ~this() { ... } } HasDtor[] func(HasDtor[] arr) { HasDtor[] middleSlice; for (i; 0 .. 1000) { arr ~= HasDtor(i); if (i == 500) { middleSlice = arr; } } return arr; } Suppose the array gets moved sometime after i=500 because it ran out of space in the current memory location. Since there is another slice middleSlice pointing at the old data, it's not just a matter of *moving* the elements over to the new location; they have to be *copied* over so that there are now two copies of the original elements -- the GC doesn't know whether func may try to access the original elements through middleSlice, so it cannot just move them. Only when middleSlice goes out of scope, can the old elements be destructed. So we see that when an array is grown, the elements cannot simply be moved to the new location; we must make copies of them, otherwise any slice of the old data will become invalid. So after an array is moved, there will be two copies of data, and if the original elements are unreferenced after all, the GC will call the dtors when it runs the next collection cycle. Then when the new elements become unreferenced, the dtors will be called again at the following collection cycle, on the new copies of the elements. T -- It always amuses me that Windows has a Safe Mode during bootup. Does that mean that Windows is normally unsafe?
Re: Arrays of noncopyables/Invalid memory operation
On 02/18/2016 12:12 PM, Ali Çehreli wrote: > > Hmm, why does the destructor of object 60 run in the middle of nowhere? > > It so happens that the GC decides to collect when moving the array to a > new location at that point. If you add the following to the beginning of > main, you will see that the next object to destroy is 59, then 58, etc. > > import core.memory; > GC.disable; > > So, everything makes sense other than throwing in a destructor having > issues with the current GC. :-/ No, it doesn't make sense to me anymore. All we're doing here is growing an array. There shouldn't be any destructor calls when just doing that, no? As the array grows, the elements should be "moved" to the new location; why the destructor? I hope this is related to the recent fix. Ali
Re: Installing DUB on OSX
On Thursday, 18 February 2016 at 16:33:51 UTC, John Colvin wrote: On Thursday, 18 February 2016 at 07:52:11 UTC, Joel wrote: On Thursday, 18 February 2016 at 07:11:23 UTC, Joel wrote: I had dub installed in a folder that meant I had to put 'sudo dub' to run it. I've tried to fix the problem, but where do you put it (also I tried one place, but couldn't put it in that folder)? I've now tried 'brew install dub' and 'brew upgrade dub', but they come up with, 'Warning: dub-0.9.22 already installed', or 'Error: dub 0.9.22 already installed'. Sounds like you have some problems with your homebrew. What does `brew doctor` give? Did you accidentally use `sudo brew` at some point? I don't think I put 'sudo brew' at any point (I can't remember). I hope I haven't broken my OSX! Joels-MacBook-Pro:Debug joelcnz$ brew doctor Please note that these warnings are just used to help the Homebrew maintainers with debugging if you file an issue. If everything you use Homebrew for is working fine: please don't worry and just ignore them. Thanks! Warning: /usr/local/include isn't writable. This can happen if you "sudo make install" software that isn't managed by by Homebrew. If a formula tries to write a file to this directory, the install will fail during the link step. You should probably `chown` /usr/local/include Warning: /usr/local/sbin isn't writable. This can happen if you "sudo make install" software that isn't managed by by Homebrew. If a formula tries to write a file to this directory, the install will fail during the link step. You should probably `chown` /usr/local/sbin Warning: The /usr/local directory is not writable. Even if this directory was writable when you installed Homebrew, other software may change permissions on this directory. Some versions of the "InstantOn" component of Airfoil are known to do this. You should probably change the ownership and permissions of /usr/local back to your user account. Warning: Broken symlinks were found. Remove them with `brew prune`: /usr/local/lib/libSDL2.dylib Warning: "config" scripts exist outside your system or Homebrew directories. `./configure` scripts often look for *-config scripts to determine if software packages are installed, and what additional flags to use when compiling and linking. Having additional scripts in your path can confuse software installed via Homebrew if the config script overrides a system or Homebrew provided script of the same name. We found the following "config" scripts: /opt/local/bin/freetype-config /opt/local/bin/libpng-config /opt/local/bin/libpng15-config /opt/local/bin/ncurses5-config /opt/local/bin/ncursesw5-config Warning: Your XQuartz (2.7.5) is outdated Please install XQuartz 2.7.7: https://xquartz.macosforge.org Warning: Unbrewed dylibs were found in /usr/local/lib. If you didn't put them there on purpose they could cause problems when building Homebrew formulae, and may need to be deleted. Unexpected dylibs: /usr/local/lib/libdsfml-audio.2.0.dylib /usr/local/lib/libdsfml-graphics.2.0.dylib /usr/local/lib/libdsfml-network.2.0.dylib /usr/local/lib/libdsfml-system.2.0.dylib /usr/local/lib/libdsfml-window.2.0.dylib /usr/local/lib/libsfml-audio.2.1.dylib /usr/local/lib/libsfml-graphics.2.1.dylib /usr/local/lib/libsfml-network.2.1.dylib /usr/local/lib/libsfml-system.2.1.dylib /usr/local/lib/libsfml-window.2.1.dylib /usr/local/lib/libsoloud_x86.dylib Warning: Unbrewed header files were found in /usr/local/include. If you didn't put them there on purpose they could cause problems when building Homebrew formulae, and may need to be deleted. Unexpected header files: /usr/local/include/DSFML/Audio/Err.h /usr/local/include/DSFML/Audio/Export.h /usr/local/include/DSFML/Audio/Listener.h /usr/local/include/DSFML/Audio/Sound.h /usr/local/include/DSFML/Audio/SoundBuffer.h /usr/local/include/DSFML/Audio/SoundFile.h /usr/local/include/DSFML/Audio/SoundRecorder.h /usr/local/include/DSFML/Audio/SoundSource.h /usr/local/include/DSFML/Audio/SoundStream.h /usr/local/include/DSFML/Audio/Types.h /usr/local/include/DSFML/Config.h /usr/local/include/DSFML/ConvertEvent.h /usr/local/include/DSFML/Graphics/Err.h /usr/local/include/DSFML/Graphics/Export.h /usr/local/include/DSFML/Graphics/Font.h /usr/local/include/DSFML/Graphics/Image.h /usr/local/include/DSFML/Graphics/RenderTexture.h /usr/local/include/DSFML/Graphics/RenderWindow.h /usr/local/include/DSFML/Graphics/Shader.h /usr/local/include/DSFML/Graphics/Text.h /usr/local/include/DSFML/Graphics/Texture.h /usr/local/include/DSFML/Graphics/Transform.h /usr/local/include/DSFML/Graphics/Types.h /usr/local/include/DSFML/Graphics/View.h /usr/local/include/DSFML/Network/Err.h /usr/local/include/DSFML/Network/Export.h /usr/local/include/DSFML/Network/Ftp.h
Re: Why is there no combination of popFront and front to pop? (aka Python `next`)
On 02/18/2016 05:04 AM, Seb wrote: > However on the other side we learn the stack syntax with return at > university You must have seen it in the context of reference types, no? There is no problem in languages that don't have value types. The problem is with the final copying of the value to the caller's context. If that operation throws, then the function is not strongly exception safe. > (it's even on wikipedia [2]) and thus I am a friend of such a > syntax: > [2] https://en.wikipedia.org/wiki/Stack_(abstract_data_type) That Wikipedia article has no mention of "exception". Dismissed... :) Exception safety involves "cohesion". That's the term Herb Sutter had used in the context of C++ exception safety. One of his guidelines: "Prefer cohesion. Always endeavor to give each piece of code—each module, each class, each function—a single, well-defined responsibility." The next() you are proposing is inferior to front() and popFront() because it mixes the two concerns: access and mutation. I guess it would be acceptable if next() required that the copy operation on the elements were @nothrow. I think then it would be safe. Ali
Re: Arrays of noncopyables/Invalid memory operation
On 02/17/2016 07:00 PM, ZombineDev wrote: > On Thursday, 18 February 2016 at 01:19:16 UTC, Ali Çehreli wrote: >> On 02/17/2016 05:14 PM, ZombineDev wrote: >> >> > The "Invalid memory operation" error is thrown only by the GC >> (AFAIK) >> > when the user tries something unsupported like allocating or >> freeing in >> > a destructor, while a GC collection is being run. >> >> That. The problem is when the assert check fails (for value==60). The >> GC fails to create the exception object. I found an open issue that depends on changing the GC implementation: https://issues.dlang.org/show_bug.cgi?id=7349 > Hmm, why does the destructor of object 60 run in the middle of nowhere? It so happens that the GC decides to collect when moving the array to a new location at that point. If you add the following to the beginning of main, you will see that the next object to destroy is 59, then 58, etc. import core.memory; GC.disable; So, everything makes sense other than throwing in a destructor having issues with the current GC. :-/ Ali
Re: Installing DUB on OSX
On 2016-02-18 10:55, Edwin van Leeuwen wrote: sudo cp dub /usr/bin/ It should say in /usr/local/bin. Because users don't have write access to /usr/bin on OS X 10.11, even with sudo. -- /Jacob Carlborg
Re: ndslice help
On Thursday, 18 February 2016 at 02:24:20 UTC, ZombineDev wrote: On Thursday, 18 February 2016 at 00:25:09 UTC, Zz wrote: Hi, I'm trying to generate the following sequences with ndslice. 0 0 0 1 1 1 1 1 1 0 0 0 0 1 2 0 1 2 2 1 0 2 1 0 It's okay with loops but was checking to see if it's possible with ndslice. Zz Here's my solution: http://dpaste.dzfl.pl/29676608fd88 The best part about ndslice is that you can use the ordinary slice operations with it like: http://dlang.org/spec/arrays.html#array-copying, http://dlang.org/spec/arrays.html#array-setting, http://dlang.org/spec/arrays.html#array-operations, etc. and also leverage the existing ranges, range transformations and algorithms from http://dlang.org/phobos/std_range and http://dlang.org/phobos/std_algorithm. In addition, I used nested array formatting with http://dlang.org/phobos/std_format. Thanks Zz
Re: Installing DUB on OSX
On Thursday, 18 February 2016 at 07:52:11 UTC, Joel wrote: On Thursday, 18 February 2016 at 07:11:23 UTC, Joel wrote: I had dub installed in a folder that meant I had to put 'sudo dub' to run it. I've tried to fix the problem, but where do you put it (also I tried one place, but couldn't put it in that folder)? I've now tried 'brew install dub' and 'brew upgrade dub', but they come up with, 'Warning: dub-0.9.22 already installed', or 'Error: dub 0.9.22 already installed'. Sounds like you have some problems with your homebrew. What does `brew doctor` give? Did you accidentally use `sudo brew` at some point?
Re: Why is there no combination of popFront and front to pop? (aka Python `next`)
On Thursday, 18 February 2016 at 13:04:09 UTC, Seb wrote: Anyways I will try my best to submit a PR and we can continue to discuss pros/cons there - imho it's an excellent learning task :) Update: Yeah I submitted my first PR to phobos: https://github.com/D-Programming-Language/phobos/pull/4010
Re: Why is there no combination of popFront and front to pop? (aka Python `next`)
On Wednesday, 17 February 2016 at 11:30:40 UTC, Jonathan M Davis wrote: Feel free to create a pull request to add next. I don't know if it would be accepted or not. I suspect that it mainly comes down to whether such a simple function would be deemed worth adding. On some level, it is a usability improvement, but in theory, we'd prefer to be adding functions to Phobos which add real value rather than simple wrappers that do basic stuff that anyone can easily do. So, it may be accepted as a usability improvement, or it may be rejected on the grounds that it's too simple to be worth it. You won't know if you don't try though. - Jonathan M Davis Thanks a lot to you all for your input! It helped me to understand D more :) My use case is really about having `pop{Front,Back}` which returns the popped element. I do understand that the current API design is a lot more flexible and better [1] and there's no objection at all from my side. However on the other side we learn the stack syntax with return at university (it's even on wikipedia [2]) and thus I am a friend of such a syntax: auto myFile = ["","1","2"]; // cut off the header myFile.next.writeln; // do sth with the body myFile.map!(to!int).map!"a + 1".map!(to!string).joiner(",").writeln; Anyways I will try my best to submit a PR and we can continue to discuss pros/cons there - imho it's an excellent learning task :) [1] https://stackoverflow.com/questions/12206242/store-results-of-stdstack-pop-method-into-a-variable [2] https://en.wikipedia.org/wiki/Stack_(abstract_data_type)
Re: Enforcing checks for return code
On Thursday, 18 February 2016 at 07:21:05 UTC, Chris Katko wrote: Hello. I'm almost brand-new to the D language and still absorbing things. I'm wondering if it's possible to fire off a compile-time (or worst case, a run-time) warning or error if a function is called, but the return value is not checked. I'm not trying to enforce whether someone actually deciphers the value's meaning correctly. I just want to enforce that somewhere, a variable or expression is receiving the return value of a particular function. Any ideas? As Jonathan said, there's no such built-in feature, and exception are preferred over return codes. However, you can implement such a check at run time: struct ForceCheck(T) { private T payload; private bool checked = false; @disable this(); @disable this(this); this()(auto ref T payload) { this.payload = payload; } ref T get() { this.checked = true; return payload; } alias get this; void ignore() { this.checked = true; } ~this() { assert(this.checked, "you forgot to check the return value"); } } auto forceCheck(T)(auto ref T value) { return ForceCheck!T(value); } auto foo() { return forceCheck(42); } void main() { { auto a = foo(); if(a != 42) { } // stored values } { if(foo() != 42) { } // direct access } { foo().ignore(); // explicitly ignore return value } { auto b = foo(); // this one asserts foo(); // as does this one } } I guess it's reasonably performant; it could be optimized further by only adding the `checked` member if assertions are enabled (using `version(assert)`).
Re: Enforcing checks for return code
On Thursday, 18 February 2016 at 07:21:05 UTC, Chris Katko wrote: Hello. I'm almost brand-new to the D language and still absorbing things. I'm wondering if it's possible to fire off a compile-time (or worst case, a run-time) warning or error if a function is called, but the return value is not checked. I'm not trying to enforce whether someone actually deciphers the value's meaning correctly. I just want to enforce that somewhere, a variable or expression is receiving the return value of a particular function. Any ideas? I imagine I could use a compiler flag to warn, but that's a global setting. I'm looking more for a specified subset of functions. Off hand, I see two reasonable ways to do this: 1) Instead of actually returning the value, require the caller to pass the destination variable as a `ref` or `out` parameter. Disadvantages: This could be a bit slower than using actual return values, and it would change the syntax. 2) Wrap the return value in a struct like this: struct MustUse(T) { pure: nothrow: private: T payload; bool used; public: @property ref T use() @nogc { used = true; return payload; } alias use this; this(T payload) @nogc { this.payload = payload; this.used = false; } ~this() { if(!used) throw new Error(T.stringof ~ " return value was not used."); } } // For convenience, use this function to infer T from the type of paylaod. MustUse!T mustUse(T)(T payload) { return MustUse!T(payload); } Example on DPaste: http://dpaste.dzfl.pl/8ba6ebf05f32 With inlining and optimizations, even the above generic implementation might be faster than passing by ref/out. A specialization for non-null pointer values could almost certainly be faster. Disadvantages: On the other hand, it might be slower. The only way to know for sure, is to write some non-trivial examples and benchmark. Regardless, the fact that it signals the error at runtime instead of compile time could be annoying.
Re: Enforcing checks for return code
On Thursday, February 18, 2016 07:21:05 Chris Katko via Digitalmars-d-learn wrote: > Hello. I'm almost brand-new to the D language and still absorbing > things. > > I'm wondering if it's possible to fire off a compile-time (or > worst case, a run-time) warning or error if a function is called, > but the return value is not checked. > > I'm not trying to enforce whether someone actually deciphers the > value's meaning correctly. I just want to enforce that somewhere, > a variable or expression is receiving the return value of a > particular function. > > Any ideas? > > I imagine I could use a compiler flag to warn, but that's a > global setting. I'm looking more for a specified subset of > functions. D has no such feature, and very little D code is going to use return codes. D has exceptions, and throwing an exception is the normal way to indicate that there was a problem. You certainly _can_ choose to use error codes if you'd like, but it's not normal practice, and the language does nothing extra to support it. - Jonathan M Davis
Re: Installing DUB on OSX
On Thursday, 18 February 2016 at 09:25:00 UTC, Joel wrote: On Thursday, 18 February 2016 at 08:24:34 UTC, Jacob Carlborg wrote: On 2016-02-18 08:11, Joel wrote: I had dub installed in a folder that meant I had to put 'sudo dub' to run it. I've tried to fix the problem, but where do you put it (also I tried one place, but couldn't put it in that folder)? You usually have read access to most paths. That means you should be able to run dub without sudo. Where is "dub" located? Run "which dub". It's currently in '/usr/local/bin'. How do I add it to '/usr/bin' (or what ever)? I get: Joels-MacBook-Pro:Downloads joelcnz$ cp dub /usr/bin cp: /usr/bin/dub: Operation not permitted sudo cp dub /usr/bin/ but to be honest I would delete dub (sudo rm /usr/local/bin/dub) and then try to install it with homebrew again.
Re: Installing DUB on OSX
On Thursday, 18 February 2016 at 08:24:34 UTC, Jacob Carlborg wrote: On 2016-02-18 08:11, Joel wrote: I had dub installed in a folder that meant I had to put 'sudo dub' to run it. I've tried to fix the problem, but where do you put it (also I tried one place, but couldn't put it in that folder)? You usually have read access to most paths. That means you should be able to run dub without sudo. Where is "dub" located? Run "which dub". It's currently in '/usr/local/bin'. How do I add it to '/usr/bin' (or what ever)? I get: Joels-MacBook-Pro:Downloads joelcnz$ cp dub /usr/bin cp: /usr/bin/dub: Operation not permitted
Re: Installing DUB on OSX
On 2016-02-18 08:11, Joel wrote: I had dub installed in a folder that meant I had to put 'sudo dub' to run it. I've tried to fix the problem, but where do you put it (also I tried one place, but couldn't put it in that folder)? You usually have read access to most paths. That means you should be able to run dub without sudo. Where is "dub" located? Run "which dub". -- /Jacob Carlborg
Re: Installing DUB on OSX
On Thursday, 18 February 2016 at 07:52:11 UTC, Joel wrote: On Thursday, 18 February 2016 at 07:11:23 UTC, Joel wrote: I had dub installed in a folder that meant I had to put 'sudo dub' to run it. I've tried to fix the problem, but where do you put it (also I tried one place, but couldn't put it in that folder)? I've now tried 'brew install dub' and 'brew upgrade dub', but they come up with, 'Warning: dub-0.9.22 already installed', or 'Error: dub 0.9.22 already installed'. What I do here: - git clone DUB to ~/Desktop/dub - built it with build.sh - put symbolic links to the binary in /usr/bin