const ref parameters and r-value references

2014-05-02 Thread Mark Isaacson via Digitalmars-d-learn
I'm in the process of learning/practicing D and I noticed something that seems peculiar coming from a C++ background: If I compile and run: void fun(const ref int x) { //Stuff } unittest { fun(5); //Error! Does not compile } I get the specified error in my unit test. I understand that the

Re: const ref parameters and r-value references

2014-05-02 Thread Mark Isaacson via Digitalmars-d-learn
Auto ref parameters seem to be just what I need. Thanks! I'd still be curious if anyone has additional information regarding the rationale at play (I'm spoiled, reading TDPL and having each decision explained in text).

Postblit not invokable with MyStruct(MyStruct()); ?

2014-05-02 Thread Mark Isaacson via Digitalmars-d-learn
I have just discovered that the postblit constructor is not able to be invoked like a "normal" constructor, or, as one would manually do so in C++ with a copy constructor. Accordingly I have a couple questions: 1) What are the various ways to invoke the postblit constructor? I have not tested

Re: Postblit not invokable with MyStruct(MyStruct()); ?

2014-05-02 Thread Mark Isaacson via Digitalmars-d-learn
Did some thinking: Realized that the appropriate mechanism to express that A and B are two ways of representing the same thing is to do so via opCast. I had not considered this option carefully initially as I am translating someone else's C++ code to D and hoped that they had used the appropria

Re: Postblit not invokable with MyStruct(MyStruct()); ?

2014-05-02 Thread Mark Isaacson via Digitalmars-d-learn
@bearophile - Unless I'm missing something, alas, no. Neither A nor B is a subtype of the other. In particular, in the real code one is a CartesianVector and the other a PolarVector. For what it's worth, 'foo' is actually opBinary addition. Thanks for the thought though.

Re: Postblit not invokable with MyStruct(MyStruct()); ?

2014-05-03 Thread Mark Isaacson via Digitalmars-d-learn
What actually fails is the initialization of 'a'. Add another this(A a) { /* Stuff */ } constructor to the 'A' struct, and it will work. And, yes, the missing cpctors are a language problem. artur Thanks. Yeah, I figured I could do that, I was just hoping that I could leverage the po

Re: const ref parameters and r-value references

2014-05-04 Thread Mark Isaacson via Digitalmars-d-learn
Thanks for the insights! I suppose we'll get a chance to see where things stand at this year's dconf. It's quite interesting that D's concept of r-values seems less developed than C++. Here's hoping that that only results in a better thought out solution.

C++ std::map equivalent? (An in-order iterable associative container)

2014-05-04 Thread Mark Isaacson via Digitalmars-d-learn
I'm looking for a means to associate a key with a value and iterate over said container in-order. My natural choice in C++ would be std::map. When I look in std.container, I see that there is a RedBlackTree implementation, however this does not associate a key with a value (it is the equivale

Re: C++ std::map equivalent? (An in-order iterable associative container)

2014-05-04 Thread Mark Isaacson via Digitalmars-d-learn
On Sunday, 4 May 2014 at 21:40:04 UTC, bearophile wrote: Mark Isaacson: 2) Create a wrapper struct that contains key and value and whose comparison operator is defined only on the key. This would essentially be doing what the C++ implementation does. Until we have a tree-based associative ma

Re: C++ std::map equivalent? (An in-order iterable associative container)

2014-05-04 Thread Mark Isaacson via Digitalmars-d-learn
Interesting. I clearly have more to learn about Tuple. I think I concur with Dicebot's alterations for self-documentation. Thanks for all of your suggestions gentlemen.

Re: Need help with movement from C to D

2014-05-04 Thread Mark Isaacson via Digitalmars-d-learn
On Monday, 5 May 2014 at 03:57:54 UTC, Andrey wrote: Guys, could someone help me with suitable template? I have C macro, which calculates the offset of the field in a struct: #define offsetof(type, field) ((long) &((type *)0)->field) A similar D code is, as far as I know, type.field.offse

Reading a single whitespace-separated word from stdin

2014-05-05 Thread Mark Isaacson via Digitalmars-d-learn
I'm trying my hand at reading from standard input and having little luck. In particular, I would like to be able to do the rough equivalent of C++'s: cin >> myString; As opposed to reading the whole line. I attempted to do this with readf: string result; readf(" %s ", &result); However th

Re: Reading a single whitespace-separated word from stdin

2014-05-06 Thread Mark Isaacson via Digitalmars-d-learn
Fair enough. I've done stuff like that in the past. I'm trying to implement a university project that was originally designed for C++ style I/O... and so where I'd have otherwise jumped at something like that from the beginning, my hands are slightly tied. Suppose I'll make due/not fully comp

Re: Reading a single whitespace-separated word from stdin

2014-05-06 Thread Mark Isaacson via Digitalmars-d-learn
Indeed. However, doing so looks more painful than redefining my goals. Upon further examination it seems that I had more flexibility than I originally estimated. Besides, the real reason I'm implementing this project is just to practice for when I get to write production D code in a week anyway

Re: Reading a single whitespace-separated word from stdin

2014-05-06 Thread Mark Isaacson via Digitalmars-d-learn
An exceptionally generous offer! May take you up on that. Thank you :).

Re: Temporary silence output (stdout)

2014-05-10 Thread Mark Isaacson via Digitalmars-d-learn
On Saturday, 10 May 2014 at 20:24:50 UTC, MarisaLovesUsAll wrote: Hi! I sometimes got a useless messages in stdout from SDL_Image library, and I want to temporary silence it. How do I do? Consider using either version or debug statements. If you want the messages to be opt-in, debug statements

Re: Unique Enum Members

2016-05-05 Thread Mark Isaacson via Digitalmars-d-learn
On Thursday, 5 May 2016 at 12:54:08 UTC, Nordlöw wrote: Is there a way to calculate unique enum members without using sort, such as *not* done in my current implementation: auto uniqueEnumMembers(T)() { import std.traits: EnumMembers; import std.algorithm: sort, uniq; return [EnumMe

Creating a "fixed-range int" with opDispatch and/or alias this?

2016-06-01 Thread Mark Isaacson via Digitalmars-d-learn
I'm trying to create a type that for all intents and purposes behaves exactly like an int except that it limits its values to be within a certain range [a,b]. Theoretically, I would think this looks something like: struct FixedRangeInt { this(int min, int max, int value=0) { this.min = m

Looping over all enum values

2014-05-28 Thread Mark Isaacson via Digitalmars-d-learn
Is there a mechanism that allows you to loop over all of an enum's values? I've wanted to do so a couple times now for CTFE/mixin templates. I was able to loop over the underlying type when my enum was integer-based, but I can't do that if the values are strings (also that solution is quite hacky

Re: Looping over all enum values

2014-05-28 Thread Mark Isaacson via Digitalmars-d-learn
On Wednesday, 28 May 2014 at 20:20:37 UTC, Adam D. Ruppe wrote: On Wednesday, 28 May 2014 at 20:19:45 UTC, Mark Isaacson wrote: Is there a mechanism that allows you to loop over all of an enum's values? I've wanted to do so a couple times now for CTFE/mixin templates. __traits(allMembers) on t

Are tests interruptible/concurrent? Is use of a (thread local) global safe in tests?

2014-05-30 Thread Mark Isaacson via Digitalmars-d-learn
I'm having fun running some unittests. I set up a simple homemade mock of std.net.curl's functions that essentially just consists of a global queue that I can add strings to and get back in a predictable order when calling std.net.curl.get/post/etc. I use this mock in a couple of different module

Linking with C on Windows

2014-06-05 Thread Mark Isaacson via Digitalmars-d-learn
I'm having a very difficult time figuring out exactly how to do this. I've compiled my D code into a .obj file with dmd, but I don't know what C compiler I should use (or if it makes any difference). I've attempted to use MinGW gcc, which spits out .o files, and Visual Studio, which does... someth

Re: Linking with C on Windows

2014-06-05 Thread Mark Isaacson via Digitalmars-d-learn
On Thursday, 5 June 2014 at 22:59:48 UTC, bearophile wrote: Mark Isaacson: My attempts to have either the MinGW linker or the Visual Studio linker accept my D .objs have all failed. Try using the dmc compiler for the C code. Bye, bearophile I'd considered that, but in the long term that w

Re: Linking with C on Windows

2014-06-05 Thread Mark Isaacson via Digitalmars-d-learn
Still unresolved, but a thought: I decided to take a step back and try to link with C on Linux first. I found out that if I did the linking step with dmd things worked, but not with gcc. The reason then became apparent: dmd knows to pass druntime and phobos and all of that stuff to the linker. Ru

Re: Using D static library from C

2014-06-05 Thread Mark Isaacson via Digitalmars-d-learn
I found that if I told dmd rather than gcc to do the linking that everything just magically worked. In other words: gcc -c cstuff.c dmd -c dstuff.d dmd cstuff.o dstuff.o I presume that dmd would be similarly smart with static libraries.

How to free memory of an associative array

2014-06-24 Thread Mark Isaacson via Digitalmars-d-learn
How can I free the memory used by an associative array? I need to be able to reuse the same array, but set it to an empty state and free up the memory it used previously. I do not believe that setting the associative array to null is sufficient to free the memory, as it is possible that someone

Getting libcurl for 64 bit Windows

2014-06-26 Thread Mark Isaacson via Digitalmars-d-learn
I am attempting to make use of std.net.curl and having trouble acquiring libcurl for 64 bit Windows. I need to be able to link with the MSVC linker (which happens to be the default when compiling using dmd with -m64). I've looked on the libcurl website and not found any downloads that look promisi

Re: Getting libcurl for 64 bit Windows

2014-06-26 Thread Mark Isaacson via Digitalmars-d-learn
Managed to build it successfully I think, but have actually returned to the problem that initially caused me to want to try and build the library in the first place: If I try to build a simple program: import std.stdio; import std.net.curl; void main() { writeln("Hello world"); } The progra

Re: Getting libcurl for 64 bit Windows

2014-06-26 Thread Mark Isaacson via Digitalmars-d-learn
Resolved the issue.

Equivalent of DllMain on OSX?

2014-07-25 Thread Mark Isaacson via Digitalmars-d-learn
I am presently trying to port a driver I wrote for Windows to OSX. The one thing standing in my way is figuring out how to get the equivalent of DllMain on OSX. I need a place to call Runtime.initialize() and whatnot. Reading the wiki, it seemed like `shared static this()` was the appropriat

Re: Equivalent of DllMain on OSX?

2014-07-25 Thread Mark Isaacson via Digitalmars-d-learn
Loading multiple D shared libraries isn't supported on OS X yet, see these warnings in druntime: https://github.com/D-Programming-Language/druntime/blob/master/src/rt/sections_osx.d#L198 If you only have a single D shared library, I think it's possible, you just may have to tweak dmd/druntim

Getting what came *before* the results of a find call

2015-02-27 Thread Mark Isaacson via Digitalmars-d-learn
What's the idiomatic way of getting everything *before* the results of a call to find? assert("hello world".find(" world").what_goes_here??? == "hello"); In an article Andrei wrote a few years ago (http://www.informit.com/articles/article.aspx…) he mentioned a function like this with the name

Re: Getting what came *before* the results of a find call

2015-02-27 Thread Mark Isaacson via Digitalmars-d-learn
Not that it's super important, but the link didn't copy well, so here's that: http://www.informit.com/articles/article.aspx?p=1407357&seqNum=12

Re: Getting what came *before* the results of a find call

2015-02-27 Thread Mark Isaacson via Digitalmars-d-learn
Ahhh right - forgot about that. Thanks!

Equivalent of C++ function-scope static initialization

2015-03-02 Thread Mark Isaacson via Digitalmars-d-learn
I'm looking for the D equivalent of: //C++ void foo() { static string bar = painfulToInitialize(); //Always returns the same value /* A bunch of code */ } I don't need the thread-safety that C++ provides in that case, though it wouldn't hurt. I'm essentially trying to memoize the result

Re: Equivalent of C++ function-scope static initialization

2015-03-02 Thread Mark Isaacson via Digitalmars-d-learn
On Monday, 2 March 2015 at 23:07:30 UTC, Ali Çehreli wrote: immutable string bar; shared static this() { bar = painfulToInitialize(); } void foo() { } Clever :). > I don't need the thread-safety that C++ provides in that case, I am not aware of such safety. (?) Is that a newer C++ fea

Getting associative array value by reference

2015-03-31 Thread Mark Isaacson via Digitalmars-d-learn
I'm presently trying to create the value of a key in an associative array if it does not exist, and then maintain a reference/pointer to the value. This is what I came up with, but it seems really crufty and I feel that there must be a cleaner way: Value[string] assocArray; foreach (...) {

Re: Getting associative array value by reference

2015-03-31 Thread Mark Isaacson via Digitalmars-d-learn
Not quite. You'll note that I am creating the elements in the associative array, not just iterating over them; some of them just happen to be duplicates. FWIW: That foreach happens to be over an input stream.

Why doesn't map!(a => writeln(a)) print anything?

2015-04-17 Thread Mark Isaacson via Digitalmars-d-learn
Why can't I print things using the map algorithm? Specifically: http://ideone.com/VLp4Xa

Re: Why doesn't map!(a => writeln(a)) print anything?

2015-04-17 Thread Mark Isaacson via Digitalmars-d-learn
On Saturday, 18 April 2015 at 01:04:24 UTC, Adam D. Ruppe wrote: map evaluates its arguments on demand. Think of it as returning a function that does the work instead of actually doing the work - you still need to call that function, which happens when you loop over it. std.algorithm.each is

Observing exceptions in a destructor

2015-04-21 Thread Mark Isaacson via Digitalmars-d-learn
I'd like to be able to know if my destructor is being called because an exception was thrown. Any way to do that? I tried this: http://ideone.com/JbXH2w (Pasted here for convenience): import std.stdio, std.exception; struct Catcher { ~this() { try {} catch (Exception e

Re: Observing exceptions in a destructor

2015-04-21 Thread Mark Isaacson via Digitalmars-d-learn
Oh well :(. Yeah, it's just for debugging. I want to publish a script that automatically gathers relevant debug information so that my users can just copy paste it all into one place, ready for me to take a look even if I can't repro. One of the primitives in my script is a wrapper around std.pro