Re: Why D isn't the next "big thing" already

2016-07-26 Thread phant0m via Digitalmars-d-learn

On Tuesday, 26 July 2016 at 15:11:00 UTC, llaine wrote:

Hi guys,

I'm using D since a few month now and I was wondering why 
people don't jump onto it that much and why it isn't the "big 
thing" already.


Everybody is into javascript nowadays, but IMO even for doing 
web I found Vibe.d more interesting and efficient than node.js 
for example.


I agree that you have to be pragmatic and choose the right 
tools for the right jobs but I would be interested to have 
other opinion on thoses questions.


As far as I know, most of the people wait when D will be used by 
big companies for big projects. It's the chicken-egg problem. 
Personally, I found that D improves my productivity a lot 
(comparing to C++). I gave up on opinions of other people and use 
what is convenient for me in my own projects. But not everybody 
such "brave".


Re: Accessing contents of associative arrays in an optimal way

2016-07-09 Thread phant0m via Digitalmars-d-learn

Thank you!


Accessing contents of associative arrays in an optimal way

2016-07-09 Thread phant0m via Digitalmars-d-learn

Suppose I have AA of structures:

struct Foo {
int value;
string name;
}

Foo[int] records;

As far as I know, AA implemented as a hashtable. So, will there 
be two searches performed (one search for each line)?

records[3].value = 10;
records[3].name = "name";

How can I access elements of this AA by a "reference"? In C++ I 
can use reference to an element of the map:

Foo& foo = records.find(3).second;
foo.value = 10;
foo.name = "name";

I found that in D I can use a "with" keyword to achieve the same 
effect:

with(values[0]) {
value = 10;
name = "name";
}

Is this the only optimal way?



Re: Passing structs to functions

2016-07-02 Thread phant0m via Digitalmars-d-learn

On Saturday, 2 July 2016 at 19:46:53 UTC, Adam D. Ruppe wrote:

On Saturday, 2 July 2016 at 18:37:06 UTC, phant0m wrote:

How should I pass a struct variable in D effectively?


Passing by value is often the most efficient. It depends on 
what exactly you have in the struct.


From the point of view of a hardcore C++ programmer, D looks very 
promising and very strange at the same time. I understand that my 
C++ habits are not applicable here, so I'm trying to find a 
"correct" way to do basic things.
I took a simple task (for D learning purposes): to implement a 
Point template "class" (something like Qt's QPoint).
As far as I want to be able to add two points, I've implemented 
opAdd() function:


struct Point(T) {
static assert(__traits(isArithmetic, T));

T x;
T y;

Point opAdd(const ref Point other) const {
return Point(x + other.x, y + other.y);
}
}


Now, to be able to use rvalues, I need to add another one:
Point opAdd(const Point other) const {
return opAdd(other);
}

That's where my question came from. Of course, I can use just one 
function, which  accepts arguments by value. I'm just unsure 
whether it's a good decision and a common practice.


Re: Passing structs to functions

2016-07-02 Thread phant0m via Digitalmars-d-learn

On Saturday, 2 July 2016 at 19:25:37 UTC, ketmar wrote:
note the first "()", though: this is effectively a template 
function, which compiler will instantiate either with "ref" or 
without it.


Yeah, I've noticed it. Always using function template for this 
use case seems like a weird idea.


Re: Passing structs to functions

2016-07-02 Thread phant0m via Digitalmars-d-learn

On Saturday, 2 July 2016 at 18:43:51 UTC, ketmar wrote:

void boo() (in auto ref MyStruct s) { ... }

this will accept both lvalues and rvalues, and will avoid 
copying if it can.


Thank you! Could you please explain what does "auto" in this 
context mean?


Passing structs to functions

2016-07-02 Thread phant0m via Digitalmars-d-learn
I came from a C++ world, so I'm used to passing structs by a 
const reference (I mean the case, where a function argument isn't 
changed by the function). C++ allows passing a temporary (rvalue) 
to a function, which accepts a const reference. D doesn't allow 
this. All I have found is a message from Andrei: 
http://digitalmars.com/d/archives/digitalmars/D/const_ref_rvalues_103509.html#N103514 Sadly, he didn't describe the details there. Let's suppose he's right.
How should I pass a struct variable in D effectively? When 
passing by a const reference, I need to implement 2^N additional 
overloads, where N is the number of arguments. On the other hand, 
passing by value can be very ineffective.
Maybe I shouldn't bother at all and a compiler is smart enough to 
always optimize variables passed by value?
Please, share your best practices. There are many options here 
(immutable, const, ref, in, out, inout) and I don't know what I 
should prefer.