Re: Setting a list of values

2016-05-07 Thread sigod via Digitalmars-d-learn

On Monday, 2 May 2016 at 23:41:39 UTC, Steven Schveighoffer wrote:

On 5/2/16 6:00 PM, sigod wrote:

On Monday, 2 May 2016 at 10:15:04 UTC, Marc Schütz wrote:

On Monday, 2 May 2016 at 08:46:31 UTC, Ali Çehreli wrote:

[...]


Warning (better: disallowing altogether) about `=>` directly 
followed
by `{` should be enough to cover all cases. To express that 
you really
want a lambda returning a lambda, it can be rewritten either 
as:


(x) => () { assert(x); }

or as:

(x) => ({ assert(x); })

This check can be done purely by looking at the tokens. 
Should we
someday introduce tuples with `{}`, the check needs to be 
done after
the node starting with `{` has been parsed to distinguish 
between

delegate and tuple literals.


It's good idea. I myself stumbled into this before.


Agree.

-Steve


https://issues.dlang.org/show_bug.cgi?id=16001


Re: Setting a list of values

2016-05-07 Thread sigod via Digitalmars-d-learn

On Wednesday, 4 May 2016 at 04:56:54 UTC, Joel wrote:

On Sunday, 1 May 2016 at 05:42:00 UTC, Ali Çehreli wrote:

[...]


This seems to work the best:

arr.each!(a => { writeln(a); }());


And the ugliest. And probably slowest.


Re: Setting a list of values

2016-05-02 Thread sigod via Digitalmars-d-learn

On Monday, 2 May 2016 at 10:15:04 UTC, Marc Schütz wrote:

On Monday, 2 May 2016 at 08:46:31 UTC, Ali Çehreli wrote:

[...]


Warning (better: disallowing altogether) about `=>` directly 
followed by `{` should be enough to cover all cases. To express 
that you really want a lambda returning a lambda, it can be 
rewritten either as:


(x) => () { assert(x); }

or as:

(x) => ({ assert(x); })

This check can be done purely by looking at the tokens. Should 
we someday introduce tuples with `{}`, the check needs to be 
done after the node starting with `{` has been parsed to 
distinguish between delegate and tuple literals.


It's good idea. I myself stumbled into this before.


Re: relative benefit of .reserve and .length

2016-04-29 Thread sigod via Digitalmars-d-learn
On Thursday, 28 April 2016 at 14:08:26 UTC, Steven Schveighoffer 
wrote:

On 4/28/16 8:56 AM, Jay Norwood wrote:

[...]


.reserve should make an improvement for large amount of 
appending, since you pre-allocate the data.


[...]


How about `assumeSafeAppend`? Does it have any positive impact on 
performance?


Re: Iterating over thread local storage variables

2016-03-11 Thread sigod via Digitalmars-d-learn

On Friday, 11 March 2016 at 18:45:13 UTC, Anonymouse wrote:

On Friday, 11 March 2016 at 17:33:43 UTC, sigod wrote:

On Friday, 11 March 2016 at 17:03:38 UTC, Anonymouse wrote:

On Friday, 11 March 2016 at 15:21:38 UTC, maik klein wrote:

[...]


As a drive-by comment, mind that there is a race there. 
_instantiated may have been set after the if statement but 
before the synchronized block. You have to test it again 
inside.


That's why inside of `synchronized` block you can see `if 
(!instance_)`.


It does, yes, but it also calls instance_.tls.insertBack 
unconditionally. To illustrate: 
http://dpaste.dzfl.pl/8f3e78f3265a7


Apologies for derailing.


Indeed. You're right. I didn't even look at `instance_.tls...` 
line.


I guess `insertBack` just need to be moved inside of `if 
(!instance_)` scope.


Re: Iterating over thread local storage variables

2016-03-11 Thread sigod via Digitalmars-d-learn

On Friday, 11 March 2016 at 17:03:38 UTC, Anonymouse wrote:

On Friday, 11 March 2016 at 15:21:38 UTC, maik klein wrote:

static Singleton!T get()
{
if (!instantiated_)
{
synchronized(Singleton!T.classinfo){
if (!instance_){
instance_ = new Singleton!T();
}
instantiated_ = true;
instance_.tls.insertBack(_.value);


As a drive-by comment, mind that there is a race there. 
_instantiated may have been set after the if statement but 
before the synchronized block. You have to test it again inside.


That's why inside of `synchronized` block you can see `if 
(!instance_)`.


Watch this for details: 
https://www.youtube.com/watch?v=yMNMV9JlkcQ=27m54s


Re: In language tooling

2016-03-02 Thread sigod via Digitalmars-d-learn

On Wednesday, 2 March 2016 at 02:36:50 UTC, Charles wrote:
Watched a video on Jonathan Blow's language that he's 
developing, and he has a pretty neat idea of having tools being 
part of the language. Looking at the first 15 
minutes(https://www.youtube.com/watch?v=OHZwYYW9koI) or so of 
the video, is this something that could be accomplished in D 
with CTFE?


I think he makes a decent case for whether or not it'd be 
useful.


Very interesting. I wonder what Walter would say about it.


Re: Why we cannot use string in mixins?

2016-02-27 Thread sigod via Digitalmars-d-learn

On Saturday, 27 February 2016 at 23:43:07 UTC, cym13 wrote:
Could you please provide a link to said comment? Maybe some 
context would help bring some sanity over this statement.


Topic: 
https://www.reddit.com/r/programming/comments/30sqtd/why_didnt_the_d_language_become_mainstream_as/


Said comment: 
https://www.reddit.com/r/programming/comments/30sqtd/why_didnt_the_d_language_become_mainstream_as/cpvwdkb




Re: How to detect if an array if dynamic or static

2016-02-25 Thread sigod via Digitalmars-d-learn

On Thursday, 25 February 2016 at 12:18:07 UTC, mahdi wrote:

On Thursday, 25 February 2016 at 11:50:02 UTC, sigod wrote:

On Thursday, 25 February 2016 at 10:03:08 UTC, mahdi wrote:

Thanks.

So when we define the function, we MUST specify the array 
size to be able to accept a static array?
Can't we just define a function which can accept any static 
array with any size? (e.g. a function to calculate average of 
a static int array of any size)?


Static array can be accepted in place of dynamic:

void foo()
{
int[3] arr = [1, 2, 3];

bar(arr);
}

void bar(scope int[] arr)
{
import std.stdio : writeln;
writeln(arr); // [1, 2, 3]
}

But be careful not to escape such variables. Demonstration: 
http://dpaste.dzfl.pl/613e04d4fe3f


My question: If in your `bar` function, the code tries to add a 
new element to the dynamic array, it will be completely ok 
because array is dynamic. BUT if we pass a static array to this 
function, can this error be detected at compile time (and 
prevent a runtime error)? If so, how?


Also, if you need to append elements to an array inside of a 
function, then you need to mark function arguments as `ref`:


void bar(ref int[] arr)

Code wouldn't compile if you try to pass static array as `ref` 
argument.


	Error: function f436.bar (ref int[] arr) is not callable using 
argument types (int[3])


Re: How to detect if an array if dynamic or static

2016-02-25 Thread sigod via Digitalmars-d-learn

On Thursday, 25 February 2016 at 12:18:07 UTC, mahdi wrote:

On Thursday, 25 February 2016 at 11:50:02 UTC, sigod wrote:

On Thursday, 25 February 2016 at 10:03:08 UTC, mahdi wrote:

Thanks.

So when we define the function, we MUST specify the array 
size to be able to accept a static array?
Can't we just define a function which can accept any static 
array with any size? (e.g. a function to calculate average of 
a static int array of any size)?


Static array can be accepted in place of dynamic:

void foo()
{
int[3] arr = [1, 2, 3];

bar(arr);
}

void bar(scope int[] arr)
{
import std.stdio : writeln;
writeln(arr); // [1, 2, 3]
}

But be careful not to escape such variables. Demonstration: 
http://dpaste.dzfl.pl/613e04d4fe3f


My question: If in your `bar` function, the code tries to add a 
new element to the dynamic array, it will be completely ok 
because array is dynamic. BUT if we pass a static array to this 
function, can this error be detected at compile time (and 
prevent a runtime error)? If so, how?


I'm not sure if this is an error at all. Append sees that array 
doesn't have any available space and allocates new array.


writeln(arr.ptr); // 7FBFC45AA0
arr ~= 1;
writeln(arr.ptr); // 4002E000

I would say that you have poor code design if your function must 
be able to accept static arrays and append elements to it.


You might find this useful: 
http://dlang.org/phobos/std_array.html#.Appender


Re: How to detect if an array if dynamic or static

2016-02-25 Thread sigod via Digitalmars-d-learn

On Thursday, 25 February 2016 at 10:03:08 UTC, mahdi wrote:

Thanks.

So when we define the function, we MUST specify the array size 
to be able to accept a static array?
Can't we just define a function which can accept any static 
array with any size? (e.g. a function to calculate average of a 
static int array of any size)?


Static array can be accepted in place of dynamic:

void foo()
{
int[3] arr = [1, 2, 3];

bar(arr);
}

void bar(scope int[] arr)
{
import std.stdio : writeln;
writeln(arr); // [1, 2, 3]
}

But be careful not to escape such variables. Demonstration: 
http://dpaste.dzfl.pl/613e04d4fe3f


Re: Simple performance question from a newcomer

2016-02-22 Thread sigod via Digitalmars-d-learn

On Sunday, 21 February 2016 at 16:20:30 UTC, bachmeier wrote:

On Sunday, 21 February 2016 at 14:32:15 UTC, dextorious wrote:
I had heard while reading up on the language that in D 
explicit loops are generally frowned upon and not necessary 
for the usual performance reasons.


First, a minor point, the D community is usually pretty careful 
not to frown on a particular coding style (unlike some 
communities) so if you are comfortable writing loops and it 
gives you the fastest code, you should do so.


On the performance issue, you can see this related post about 
performance with reduce:

http://forum.dlang.org/post/mailman.4829.1434623275.7663.digitalmar...@puremagic.com

This was Walter's response:
http://forum.dlang.org/post/mlvb40$1tdf$1...@digitalmars.com

And this shows that LDC flat out does a better job of 
optimization in this case:

http://forum.dlang.org/post/mailman.4899.1434779705.7663.digitalmar...@puremagic.com


I can't agree with that. Between `for` and `foreach` you should 
choose one that is more readable/understandable for particular 
situation. It's compiler's task to optimize such small things.


Re: Running task once a day in vibe.d

2016-02-16 Thread sigod via Digitalmars-d-learn

On Tuesday, 16 February 2016 at 18:30:43 UTC, Nick wrote:

Hey folks

I'm making a vibe.d application. Once a day it needs to 
download some data. How do i get the program to perform this 
task once a day?


Regards, Nick


You can use `Timer`. See `setTimer`/`createTimer` in 
http://vibed.org/api/vibe.core.core/


Re: Reserving capacity in associative arrays

2016-02-14 Thread sigod via Digitalmars-d-learn

On Monday, 15 February 2016 at 03:22:44 UTC, Jon D wrote:
Is there a way to reserve capacity in associative arrays? In 
some programs I've been writing I've been getting reasonable 
performance up to about 10 million entries, but beyond that 
performance is impacted considerably (say, 30 million or 50 
million entries). GC stats (via the "--DRT-gcopt=profile:1" 
option) indicate dramatic increases in gc time, which I'm 
assuming comes from resizing the underlying hash table. I'm 
guessing that by preallocating a large size the performance 
degradation would not be quite so dramatic. The underlying 
implementation of associative arrays appears to take an initial 
number of buckets, and there's a private resize() method, but 
it's not clear if there's a public way to use these.


--Jon


Maybe try using this: http://code.dlang.org/packages/aammm


Re: Non-English characters in code - "character 0x2212 is not a valid token"

2016-02-04 Thread sigod via Digitalmars-d-learn

On Thursday, 28 January 2016 at 14:45:36 UTC, Adam D. Ruppe wrote:

On Thursday, 28 January 2016 at 14:39:46 UTC, sigod wrote:

I tried to compile your code on dpaste (2.070.0) and got this:


dpaste has an input mangling bug with some characters as a 
result of the form submission over the web.


Oh, I see.

Is there any place where one can report bugs on dpaste?


Re: print function

2016-02-04 Thread sigod via Digitalmars-d-learn

On Thursday, 4 February 2016 at 00:23:07 UTC, ixid wrote:
It would be nice to have a simple writeln that adds spaces 
automatically like Python's 'print' in std.stdio, perhaps 
called print.


It seems Andrei decided to add such function: 
http://forum.dlang.org/thread/n8vr0l$ist$1...@digitalmars.com


Re: Get the return type of the function

2016-02-03 Thread sigod via Digitalmars-d-learn

On Wednesday, 3 February 2016 at 19:21:06 UTC, Meta wrote:

On Wednesday, 3 February 2016 at 18:40:27 UTC, xtreak wrote:
Thanks. I was trying to get the return type of lambdas. I was 
trying the following and got an error. I was using dpaste with 
dmd 2.070


writeln(ReturnType!(a =(a *a)))

Error: template instance f662.main.ReturnType!((a) => a * a) 
does not match template declaration ReturnType(func...) if 
(func.length == 1 && isCallable!func)


Ah, I see. I'd like to test something; can you please change 
`(a) => a * a` to

`(int a) => a * a` and post the results?


This works.

http://dpaste.dzfl.pl/92c254ef6cf6


Re: Counting time difference.

2016-02-03 Thread sigod via Digitalmars-d-learn

On Wednesday, 3 February 2016 at 22:27:07 UTC, holo wrote:
When i start same program on server in different timezone 
difference is much higher (more than hour). Why it is 
happening? Timezones shouldnt have influence on such equation.


Try using `Clock.currTime(UTC())`. And make sure all instances 
produce timezone independent timestamps.


Re: Non-English characters in code - "character 0x2212 is not a valid token"

2016-01-28 Thread sigod via Digitalmars-d-learn
On Thursday, 28 January 2016 at 13:26:27 UTC, Andrea Fontana 
wrote:

On Thursday, 28 January 2016 at 13:18:55 UTC, pineapple wrote:
I experimented with using the character 'ħ' in a variable 
name, and wasn't terribly surprised when the compiler didn't 
like it. What did surprise me is that I still got a compile 
error even when the character was in a comment. Is there any 
way to make dmd not get fussy about unicode?


Hmmm it's strange: I think d should support unicode on both 
comment and vars name.


This code works for me ;)

float distance(T)(T from, T to)
  {
 import std.math;

 auto toRadians = (float dec) => dec/180.0f*PI;

 auto R = 6371;
 auto φ1 = toRadians(from.lat);
 auto φ2 = toRadians(to.lat);
 auto Δφ = toRadians(to.lat-from.lat);
 auto Δλ = toRadians(to.lng-from.lng);

 float a = sin(Δφ/2) * sin(Δφ/2) + cos(φ1) * cos(φ2) * 
sin(Δλ/2) * sin(Δλ/2);

 float c = 2 * atan2(sqrt(a), sqrt(1-a));

 return R * c;

  }


What compiler version do you use?

I tried to compile your code on dpaste (2.070.0) and got this:

/d213/f100.d(8): Error: character '\' is not a valid token
/d213/f100.d(9): Error: character '\' is not a valid token
/d213/f100.d(10): Error: character '\' is not a valid token
/d213/f100.d(10): Error: character '\' is not a valid token
/d213/f100.d(11): Error: character '\' is not a valid token
/d213/f100.d(11): Error: character '\' is not a valid token
/d213/f100.d(13): Error: character '\' is not a valid token
/d213/f100.d(13): Error: character '\' is not a valid token
/d213/f100.d(13): Error: found 'u03c6' when expecting ','
/d213/f100.d(13): Error: expression expected, not '/'
/d213/f100.d(13): Error: found '2' when expecting ','
/d213/f100.d(13): Error: character '\' is not a valid token
/d213/f100.d(13): Error: character '\' is not a valid token
/d213/f100.d(13): Error: found 'u03c6' when expecting ','
/d213/f100.d(13): Error: expression expected, not '/'
/d213/f100.d(13): Error: found '2' when expecting ','
/d213/f100.d(13): Error: character '\' is not a valid token
/d213/f100.d(13): Error: character '\' is not a valid token
/d213/f100.d(13): Error: character '\' is not a valid token
/d213/f100.d(13): Error: character '\' is not a valid token


Re: What is the best declaration type for a string like parameter?

2016-01-28 Thread sigod via Digitalmars-d-learn

On Thursday, 28 January 2016 at 13:36:46 UTC, Puming wrote:
I have a function that reads a line of string and do some 
computation.


I searched the forum and found that people use `const(char)[]` 
or `in char[]` to accept both string and char[] arguments.



What's the difference between `const(char)[]` and `in char[]`?

If they are not the same, then which is better? If they are, 
then why both forms exists?


`in char[]` is short for `scope const char[]` or `scope 
const(char[])`.


See http://dlang.org/spec/function.html#parameters

It depends on the situation. If possible I would use `in` 
modifier. If not then just `const`.


I found it a bit confusing and not quite readable, so I made an 
alias:


alias str = const(char)[]

and so far it works. But if `in char[]` is better, then I 
cannot alias it:


alias str = in char[]

this does not compile.


Please, don't define such aliases. I'm sure a lot of developers 
will find it confusing. As I do.


`const(char)[]` or `in char[]` is perfectly understandable as 
soon as you know what it means.


Also, read this: http://dlang.org/spec/const3.html


splitter, compilation issue

2015-10-27 Thread sigod via Digitalmars-d-learn

Here's simple code:

import std.algorithm;
import std.array;
import std.file;

void main(string[] args)
{
auto t = args[1].readText()
.splitter('\n')
.filter!(e => e.length)
.split("---")
;
}

Looks like it should work, but it won't compile. DMD 2.068.2 
fails with this error:


	Error: template std.algorithm.iteration.splitter cannot deduce 
function from argument types !()(FilterResult!(__lambda2, 
Result), string), candidates are:

...
	Error: template instance 
std.array.split!(FilterResult!(__lambda2, Result), string) error 
instantiating


It compiles if I insert `.array` before `.split(...`.

Am I missing something? Or it's a bug? I've tried to make a brief 
search in the bug tracker, but didn't found anything.


P.S. dpaste gives very strange error:

/d712/f815.d(8): Error: unterminated character constant
/d712/f815.d(9): Error: unterminated character constant
... and so on



Re: splitter, compilation issue

2015-10-27 Thread sigod via Digitalmars-d-learn
Well, problem boils down to `splitter` having a greater 
constraints than most functions can meet.


Thanks everyone for clarification.

P.S. Maybe I should repost my question on SO? I really thought it 
was a bug, so I posted it here.


Re: splitter, compilation issue

2015-10-27 Thread sigod via Digitalmars-d-learn

On Tuesday, 27 October 2015 at 22:33:32 UTC, Adam D. Ruppe wrote:

On Tuesday, 27 October 2015 at 22:18:55 UTC, sigod wrote:
P.S. Maybe I should repost my question on SO? I really thought 
it was a bug, so I posted it here.


You could, but I'd say the same thing there


I don't expect different answer there. Main idea is to increase 
language presence and therefore popularity.


I saw someone (I think it was Martin Nowak) somewhere saying that 
maybe we should move questions from Learn forum to SO.


or just stick in .array somewhere to do an easy, generic 
solution


Which completely works in this case. Since I'm writing just a 
code generation tool, which I'll need to use just a few times.


Re: splitter, compilation issue

2015-10-27 Thread sigod via Digitalmars-d-learn
On Tuesday, 27 October 2015 at 21:54:33 UTC, Jonathan M Davis 
wrote:
Well, split calls splitter, and it doesn't make much of an 
attempt to check its arguments in its template constraint, 
mostly passing the buck onto splitter, since it's really just a 
wrapper around splitter that calls array on the result.


Looks like one more way to improve documentation.



Re: splitter, compilation issue

2015-10-27 Thread sigod via Digitalmars-d-learn

On Tuesday, 27 October 2015 at 21:45:10 UTC, Ali Çehreli wrote:

On 10/27/2015 01:58 PM, sigod wrote:

Here's simple code:

 import std.algorithm;
 import std.array;
 import std.file;

 void main(string[] args)
 {
 auto t = args[1].readText()
 .splitter('\n')
 .filter!(e => e.length)
 .split("---")
 ;
 }

Looks like it should work


split's documentation says that it requires a ForwardRange but 
the output of filter is an InputRange. (I can't imagine now why 
split has that requirement.)


Ali


It still doesn't work.

	src\phobos\std\array.d(1562): Error: template 
std.algorithm.iteration.splitter cannot deduce function from 
argument types !()(Result, string)


Sorry, I should've simplified example more.


Re: splitter, compilation issue

2015-10-27 Thread sigod via Digitalmars-d-learn

On Tuesday, 27 October 2015 at 22:56:07 UTC, sigod wrote:
On Tuesday, 27 October 2015 at 22:33:32 UTC, Adam D. Ruppe 
wrote:

On Tuesday, 27 October 2015 at 22:18:55 UTC, sigod wrote:
P.S. Maybe I should repost my question on SO? I really 
thought it was a bug, so I posted it here.


You could, but I'd say the same thing there


I don't expect different answer there. Main idea is to increase 
language presence and therefore popularity.


I saw someone (I think it was Martin Nowak) somewhere saying 
that maybe we should move questions from Learn forum to SO.


or just stick in .array somewhere to do an easy, generic 
solution


Which completely works in this case. Since I'm writing just a 
code generation tool, which I'll need to use just a few times.


Posted it here: http://stackoverflow.com/q/33380674/944911

Only removed `filter` from code.


Re: Curl, how to recieve data.

2015-10-18 Thread sigod via Digitalmars-d-learn

On Sunday, 18 October 2015 at 21:01:05 UTC, holo wrote:

On Sunday, 18 October 2015 at 20:12:42 UTC, sigod wrote:

[...]


I changed it to such code:

...
auto client = HTTP(endpoint ~ "?" ~ 
canonicalQueryString);

client.method = HTTP.Method.get;
client.addRequestHeader("x-amz-date", xamztime);
client.addRequestHeader("Authorization", 
authorizationHeader);
client.onReceive = (ubyte[] 
data){receivedData.put(data); return data.length;};

client.perform();

return new Result(receivedData.data);
...
 auto receivedData = appender!string();
...

and it is really much more faster than it was - hope it is what 
you had on your mind.


Yes, this is exactly what I meant.


Re: Curl, how to recieve data.

2015-10-18 Thread sigod via Digitalmars-d-learn

On Sunday, 18 October 2015 at 20:05:24 UTC, holo wrote:

@sigod

Actually im working on ec2 requests. Thank you  for help, it is 
working right now. I don't know why i was trying "+=" before 
instead of "~=". Is it good solution to make it such way?


Not really as it will trigger allocation on every call. Better 
use [`Appender`][0].


[0]: http://dlang.org/phobos/std_array.html#.Appender



Re: Curl, how to recieve data.

2015-10-18 Thread sigod via Digitalmars-d-learn

On Sunday, 18 October 2015 at 18:04:53 UTC, holo wrote:
I'm trying to receive data from curl request my sample code 
looks like that:


...
auto client = HTTP(endpoint ~ "?" ~ 
canonicalQueryString);

client.method = HTTP.Method.get;
client.addRequestHeader("x-amz-date", xamztime);
client.addRequestHeader("Authorization", 
authorizationHeader);

client.onReceive = (ubyte[] data)
{
recievedData = data;
return data.length;
};
client.perform();

return new Result(recievedData);
...

ubyte[] receivedData;

...

but im getting only last (like from "tail" command in unix 
systems) part of data which im expecting.


How to receive and save whole data which came as feedback for 
request? Or is there some other way to access it after usage of 
client.perform method?


I believe `onReceive` called multiple times with chunks of 
received data. So, you need to use `Appender` or `~`.


A bit off-topic:
Are you trying to download file from S3? It seems I should really 
start working on my S3 library...


Re: Starting a HTTPS session with D

2015-08-18 Thread sigod via Digitalmars-d-learn
On Thursday, 12 February 2015 at 12:34:21 UTC, Vladimir Panteleev 
wrote:
On Windows, if you are using the curl library included with DMD 
2.066.1, curl will use the Windows certificate store.


Did this changed? I use 2.068.0 and still have problems with SSL.

Sorry for necroposting.


Re: Does D have syntax for adding subscopes to classes?

2015-08-12 Thread sigod via Digitalmars-d-learn

On Wednesday, 12 August 2015 at 15:21:28 UTC, GregoryP wrote:
I'm just wondering if, or how much of the following is possible 
in some way in D:


class Foo {
int x;
sub Bar {
int x;
int getFooX(){ return super.x; }
sub FooBar {
int x;
int y;
int addXes(){ return x + super.x + super.super.x; }
}
}
}

Where the Xes are accessible outside the class by Foo.x, 
Foo.Bar.x, Foo.Bar.FooBar.x.


[Nested classes][0] maybe?

[0]: http://dlang.org/class.html#nested


Re: Code Reviewer

2015-08-12 Thread sigod via Digitalmars-d-learn

On Tuesday, 11 August 2015 at 22:50:52 UTC, Clayton wrote:

Hello everyone,

Am looking for someone who could help review my code . As an 
entry exercise to D am converting  3 C implementations of 
popular pattern  matching algorithms. The idea is to have 6 
final implementations ( 3 compile-time and 3 runtime) . I think 
am basically done with the coding, but being a beginner myself, 
I feel I need some do some critics so I can improve (especially 
on the compiletime ones).


I could have uploaded direct but I think the code may be way 
too long.


Help will be dearly appreciated.


Use this site: http://codereview.stackexchange.com/


Re: lambda syntax with curly braces

2015-08-10 Thread sigod via Digitalmars-d-learn

On Monday, 10 August 2015 at 16:02:31 UTC, bachmeier wrote:

On Monday, 10 August 2015 at 15:05:55 UTC, sigod wrote:
I see. But it's really counter intuitive after working with 
C#. Probably documentation should stress out the difference.


Thanks, Adam.


I assume you mean this page:

http://dlang.org/expression.html

There's an Improve this page button in the upper right 
corner. It's very easy to recommend a change.


Good point.

But I seldom do this because English isn't my native language.


Re: lambda syntax with curly braces

2015-08-10 Thread sigod via Digitalmars-d-learn

On Monday, 10 August 2015 at 14:05:30 UTC, Adam D. Ruppe wrote:

On Monday, 10 August 2015 at 13:57:50 UTC, sigod wrote:

[...]


It does exactly what that says: rewrites it to

(a) {
  return {
   writeln(a);
  };
}


which is returning a delegate.


[...]


So your code passed a delegate that returned a delegate to 
each. Since the one returned wasn't called, the writeln never 
happened.


If you call it like so:

[1,2,3,4,5]
.each!(a = {
writeln(a);
}()); // added parens call the returned delegate

then you see it.




The = thing in D is meant only for trivial, single line 
things. If you want multiple lines, that's where the {} syntax 
comes in with no need for the =.


.each!( (a) {
   writeln(a);
  });


I see. But it's really counter intuitive after working with C#. 
Probably documentation should stress out the difference.


Thanks, Adam.


Re: Concurrency Confusion

2015-08-10 Thread sigod via Digitalmars-d-learn

On Monday, 10 August 2015 at 22:21:18 UTC, 岩倉 澪 wrote:

On Saturday, 8 August 2015 at 06:24:30 UTC, sigod wrote:
Use negative value for `receiveTimeout`. 
http://stackoverflow.com/q/31616339/944911


actually this no longer appears to be true?
Passing -1.msecs as the duration gives me an assertion failure:

core.exception.AssertError@std/concurrency.d(1902): Assertion 
failure


Took a look in phobos and it appears to be from this line:
https://github.com/D-Programming-Language/phobos/blob/master/std/concurrency.d#L1904

If you look at the implementation of receiveTimeout, you'll see 
that it no longer has these lines from the stack overflow 
answer:


if( period.isNegative || !m_putMsg.wait( period ) )
return false;

https://github.com/D-Programming-Language/phobos/blob/master/std/concurrency.d#L824


That's weird. Especially when latest commit is mine: 
https://github.com/D-Programming-Language/phobos/commit/c8048fa48832a97f033b748f5e6b8edde3f2ae29


Re: Concurrency Confusion

2015-08-10 Thread sigod via Digitalmars-d-learn

On Monday, 10 August 2015 at 22:21:18 UTC, 岩倉 澪 wrote:

On Saturday, 8 August 2015 at 06:24:30 UTC, sigod wrote:
Use negative value for `receiveTimeout`. 
http://stackoverflow.com/q/31616339/944911


actually this no longer appears to be true?
Passing -1.msecs as the duration gives me an assertion failure:

core.exception.AssertError@std/concurrency.d(1902): Assertion 
failure


Took a look in phobos and it appears to be from this line:
https://github.com/D-Programming-Language/phobos/blob/master/std/concurrency.d#L1904


It looks like you're trying to use `receiveTimeout` like this:

bool value;
receiveTimeout(-1.msecs, value);

But you must use it like this:

bool value;
receiveTimeout(-1.msecs, (bool b) { value = b; });

See [`receive`][0] for example.

[0]: http://dlang.org/phobos/std_concurrency.html#.receive


Re: Concurrency Confusion

2015-08-10 Thread sigod via Digitalmars-d-learn

On Monday, 10 August 2015 at 22:21:18 UTC, 岩倉 澪 wrote:

On Saturday, 8 August 2015 at 06:24:30 UTC, sigod wrote:
Use negative value for `receiveTimeout`. 
http://stackoverflow.com/q/31616339/944911


actually this no longer appears to be true?
Passing -1.msecs as the duration gives me an assertion failure:

core.exception.AssertError@std/concurrency.d(1902): Assertion 
failure


Took a look in phobos and it appears to be from this line:
https://github.com/D-Programming-Language/phobos/blob/master/std/concurrency.d#L1904


It should be this line: 
https://github.com/D-Programming-Language/phobos/blob/master/std/concurrency.d#L1910


If you look at the implementation of receiveTimeout, you'll see 
that it no longer has these lines from the stack overflow 
answer:


if( period.isNegative || !m_putMsg.wait( period ) )
return false;

https://github.com/D-Programming-Language/phobos/blob/master/std/concurrency.d#L824


This lines still there: 
https://github.com/D-Programming-Language/phobos/blob/master/std/concurrency.d#L2081


I'll remove mentioned assert.


Re: Concurrency Confusion

2015-08-10 Thread sigod via Digitalmars-d-learn

On Monday, 10 August 2015 at 22:31:33 UTC, sigod wrote:

On Monday, 10 August 2015 at 22:21:18 UTC, 岩倉 澪 wrote:

[...]


It should be this line: 
https://github.com/D-Programming-Language/phobos/blob/master/std/concurrency.d#L1910



[...]


This lines still there: 
https://github.com/D-Programming-Language/phobos/blob/master/std/concurrency.d#L2081


I'll remove mentioned assert.


https://github.com/D-Programming-Language/phobos/pull/3545


lambda syntax with curly braces

2015-08-10 Thread sigod via Digitalmars-d-learn

From docs:
The following part = AssignExpression is rewritten to 
FunctionLiteralBody:

{ return AssignExpression ; }


So, I wonder what happens when curly braces already in place?

Consider this example:
```
import std.algorithm;
import std.stdio;

void main() {
[1,2,3,4,5]
.each!(a = { // remove `=` and you'll get output
writeln(a);
});
}
```

This code compiles and doesn't output anything. Which is very 
counterintuitive for me, because my main experience with lambdas 
was in C#. Where it's perfectly fine to write `identifiers = { 
/* some code */ }`.




Re: Concurrency Confusion

2015-08-08 Thread sigod via Digitalmars-d-learn

On Saturday, 8 August 2015 at 01:24:04 UTC, 岩倉 澪 wrote:

On Saturday, 8 August 2015 at 00:39:57 UTC, 岩倉 澪 wrote:

receiveTimeout(0.msecs,
(immutable Bar[] bar){ baz = cast(Bar[])bar; 
});


Whoops, that should be:
 receiveTimeout(0.msecs,
 (immutable(Bar)[] bar){ baz = cast(Bar[])bar; 
});


Use negative value for `receiveTimeout`. 
http://stackoverflow.com/q/31616339/944911


Re: Calling Syntax (no, not UFCS)

2015-08-03 Thread sigod via Digitalmars-d-learn

On Monday, 3 August 2015 at 22:42:15 UTC, SirNickolas wrote:

Hello! I'm new in D and it is amazing!

Can you tell me please if it is discouraged or deprecated to 
call a function by just putting its name, without brackets? 
It's quite unusual for me (used C++ and Python before), but I 
can see this practice even in the official Phobos documentation:


```
foreach (result; [ 1, 2, 3, 4 ].map!(a + a, a * a))
...
```

The code `.map!(a + a, a * a)()` also compiles and works as 
expected, of course.


http://dlang.org/function.html#optional-parenthesis

Also note http://dlang.org/function.html#property-functions


Re: std.algorithm each documentation terse

2015-07-20 Thread sigod via Digitalmars-d-learn

On Monday, 20 July 2015 at 14:40:59 UTC, jmh530 wrote:
I have found the documentation for each in std.algorithm a bit 
terse. It seemed like it was an eager version of map, but it 
seems to be a bit more limited than that.


Why are you trying to use `each` in place which belongs to `map`?




Re: Working functionally with third party libraries

2015-07-17 Thread sigod via Digitalmars-d-learn
On Friday, 17 July 2015 at 09:07:29 UTC, Jarl André Hübenthal 
wrote:
But its pretty nice to know that there is laziness in D, but 
when I query mongo I expect all docs to be retrieved, since 
there are no paging in the underlying queries? Thus, having a 
lazy functionality on top of non lazy db queries seem a bit off 
dont you think?


Not true. There's paging in MongoDB. See [Cursors][0] and 
documentation for `find` method. Also, look at [`vibe.d`'s 
implementation][1].


[0]: http://docs.mongodb.org/manual/core/cursors/
[1]: 
https://github.com/rejectedsoftware/vibe.d/blob/master/source/vibe/db/mongo/cursor.d


Re: Working functionally with third party libraries

2015-07-17 Thread sigod via Digitalmars-d-learn

On Friday, 17 July 2015 at 15:41:22 UTC, ZombineDev wrote:

eager approach, since it's more straightforward.


What makes you think it's always more straightforward? Sometimes 
(like in this case with MongoDB) you cannot write eager approach 
without first writing lazy one.


Re: incorrect data when returning static array in place of dynamic

2015-07-10 Thread sigod via Digitalmars-d-learn

On Monday, 6 July 2015 at 10:20:28 UTC, anonymous wrote:

On Monday, 6 July 2015 at 07:48:17 UTC, sigod wrote:

Aren't compiler smart enough to prevent it?

```
ubyte[] test1()
{
auto b = sha1Of();

return b; // Error: escaping reference to local b
}

ubyte[] test2()
{
return sha1Of(); // works, but returns incorrect data
}
```

Looks more like a bug to me.


dmd 2.068.0 catches this. You can get the beta here:
http://downloads.dlang.org/pre-releases/2.x/2.068.0/


That's good to know.


Re: incorrect data when returning static array in place of dynamic

2015-07-10 Thread sigod via Digitalmars-d-learn

On Monday, 6 July 2015 at 14:56:38 UTC, Marc Schütz wrote:

On Monday, 6 July 2015 at 10:20:28 UTC, anonymous wrote:

dmd 2.068.0 catches this. You can get the beta here:
http://downloads.dlang.org/pre-releases/2.x/2.068.0/


... and it already contains a std.digest.hmac module :-)


Yes, thanks. I know that. But I don't really want to use 
pre-release version on server.


Re: incorrect data when returning static array in place of dynamic

2015-07-06 Thread sigod via Digitalmars-d-learn

On Monday, 6 July 2015 at 05:30:46 UTC, thedeemon wrote:

On Sunday, 5 July 2015 at 18:57:46 UTC, sigod wrote:
Why does function return incorrect data? Using `.dup` in 
return expression or using `ubyte[20]` as return type fixes 
problem, but why?


Because sha1Of() returns ubyte[20], this is a stack-allocated 
array, a value type. If you put correct return type there, it 
will be returned by value and everything's fine. If your return 
type is ubyte[] (a reference type), a slice of stack-allocated 
array is returned which creates a reference to stack data that 
doesn't exist anymore.


Aren't compiler smart enough to prevent it?

```
ubyte[] test1()
{
auto b = sha1Of();

return b; // Error: escaping reference to local b
}

ubyte[] test2()
{
return sha1Of(); // works, but returns incorrect data
}
```

Looks more like a bug to me.


incorrect data when returning static array in place of dynamic

2015-07-05 Thread sigod via Digitalmars-d-learn

Consider this code:
```
import std.digest.digest;
import std.stdio;

ubyte[] hmac_sha1(const(ubyte)[] key, const(ubyte)[] message)
{
import std.digest.sha;

enum block_size = 64;

if (key.length  block_size)
key = sha1Of(key);
if (key.length  block_size)
key.length = block_size;

ubyte[] o_key_pad = key.dup;
ubyte[] i_key_pad = key.dup;

o_key_pad[] ^= 0x5c;
i_key_pad[] ^= 0x36;

	sha1Of(o_key_pad ~ sha1Of(i_key_pad ~ 
message)).toHexString.writeln; // prints correct string


return sha1Of(o_key_pad ~ sha1Of(i_key_pad ~ message));
}

void main()
{
	hmac_sha1(cast(ubyte[]), cast(ubyte[])).toHexString.writeln; 
// incorrect

---.writeln;
	hmac_sha1(cast(ubyte[])key, cast(ubyte[])The quick brown fox 
jumps over the lazy dog).toHexString.writeln;

}
```

prints:
```
FBDB1D1B18AA6C08324B7D64B71FB76370690E1D
18AA6C08140038FD18001000
---
DE7C9B85B8B78AA6BC8A7A36F70A90701C9DB4D9
B8B78AA6140038FD18001000
```

Why does function return incorrect data? Using `.dup` in return 
expression or using `ubyte[20]` as return type fixes problem, but 
why?




Nullable with reference types

2015-06-30 Thread sigod via Digitalmars-d-learn

Hi, everyone.

```
import std.typecons : Nullable;

class Test {}

Nullable!Test test;
assert(test.isNull);
```

Why does `Nullable` allowed to be used with reference types (e.g. 
classes)?


P.S. I have experience with C#, where `NullableT` cannot be 
used with reference types. And it sounds logical to me.


Re: Why D doesn't have an equivalent to C#'s readonly?

2015-06-30 Thread sigod via Digitalmars-d-learn

On Monday, 29 June 2015 at 20:12:12 UTC, Assembly wrote:
I believe it's a design choice, if so, could someone explain 
why? is immutable better than C#'s readonly so that the 
readonly keyword isn't even needed? for example, I'd like to 
declare a member as readonly but I can't do it directly because 
immutable create a new type (since it's a type specific, 
correct?) isn't really the same thing.


MyClass x = new MyClass();

if I do

auto x = new immutable(MyClass)();

give errors


Why? I think `const` and `immutable` even better than C#'s 
`readonly`. Also, are you aware that it's recommended to use 
`const` instead of `readonly`?


`new immutable(MyClass)()` is invalid code. Try `immutable 
MyClass x = new MyClass();`.


Re: Why D doesn't have an equivalent to C#'s readonly?

2015-06-30 Thread sigod via Digitalmars-d-learn

On Monday, 29 June 2015 at 22:22:46 UTC, anonymous wrote:

On Monday, 29 June 2015 at 22:11:16 UTC, sigod wrote:

`new immutable(MyClass)()` is invalid code.


It's perfectly fine, actually.


Yes, you're right. It seems I've mistyped `immutable` when was 
checking it with compiler.


Re: cannot use UDA with same name as one of field's name

2015-06-24 Thread sigod via Digitalmars-d-learn

On Tuesday, 23 June 2015 at 23:52:52 UTC, Adam D. Ruppe wrote:
On Tuesday, 23 June 2015 at 23:14:13 UTC, Steven Schveighoffer 
wrote:

I'm not completely sure on the syntax, try adding some parens.


Yeah, I'm pretty sure it needs to be

@(full.name.here) void foo()


Yep, something like this works.

```
@(vibe.data.serialization.name(_id)) int id;
```

But in this case `alias` looks better.


Re: Check if template has been passed a reference type or value type?

2015-06-24 Thread sigod via Digitalmars-d-learn

On Sunday, 7 June 2015 at 15:39:17 UTC, Marc Schütz wrote:

On Sunday, 7 June 2015 at 15:17:27 UTC, 1967 wrote:
I've got a template that takes in a type. Sometimes the type 
is a class, sometimes a struct, sometimes just an int. It 
doesn't much matter what it is, but if it's a reference type I 
need to check if it's null so I can make it not null before 
using it. I get an error if I try to check if a value type is 
null so I'd like to put a static if to check if the type 
passed to the template is a nullable one so I can avoid 
checking the value types passed in. What do I have to do to 
check for nullability?


You can directly check whether it allows comparison with null:

static if(is(typeof(value is null) : bool)) {
if(value is null)
  ...
}


Don't forget [`Nullable`][0] type, which won't pass this check.

[0]: http://dlang.org/phobos/std_typecons.html#.Nullable


cannot use UDA with same name as one of field's name

2015-06-23 Thread sigod via Digitalmars-d-learn

Hi. I have few questions about this piece of code.

```
import vibe.data.serialization;

struct User
{
	@name(_id) int id; // Error: function expected before (), not 
name of type string

string name;
}
```

Is it even proper compiler behavior? Is there any way to bypass 
it without using alias (e.g. `alias named = name;`)?


Re: cannot use UDA with same name as one of field's name

2015-06-23 Thread sigod via Digitalmars-d-learn
On Tuesday, 23 June 2015 at 22:10:43 UTC, Steven Schveighoffer 
wrote:

You can use @full.path.name


```
Error: unexpected ( in declarator
Error: basic type expected, not _id
Error: found '_id' when expecting ')'
Error: no identifier for declarator .data.serialization.name(int)
Error: semicolon expected following function declaration
Error: declaration expected, not ')'
```


You may be able to use @.name, but I'm not sure that parses.


```
Error: @identifier or @(ArgumentList) expected, not @.
Error: valid attributes are @property, @safe, @trusted, @system, 
@disable

Error: unexpected ( in declarator
Error: basic type expected, not _id
Error: found '_id' when expecting ')'
Error: no identifier for declarator .name(int)
Error: semicolon expected following function declaration
Error: declaration expected, not ')'
```



Re: Error: template cannot deduce function from argument types.

2014-08-24 Thread sigod via Digitalmars-d-learn

On Sunday, 24 August 2014 at 02:53:41 UTC, Damian Day wrote:

isImplicitlyConvertible!(ElementType!R, T))


Try [ElementEncodingType][0].

[0]: http://dlang.org/phobos/std_range.html#ElementEncodingType


Re: Is this RDMD bug ?

2014-08-23 Thread sigod via Digitalmars-d-learn

On Saturday, 23 August 2014 at 16:28:46 UTC, novice2 wrote:
I have 2 reduced files, wich i can't compile with new (DMD 
2.066) rdmd.exe under Windows 7 32-bit.


Command: rdmd --force --build-only aaa.d
Message Error 42: Symbol Undefined _D3etc3bbb3fooFZi

But command: dmd aaa.d etc\bbb.d
Compile without errors.
And then i replace rdmd.exe by old (from DMD 2.065) compile OK 
too.


Can anybody reproduce this?


Yes. Looks like regression.


Re: Is this RDMD bug ?

2014-08-23 Thread sigod via Digitalmars-d-learn

PR that introduced regression:
https://github.com/D-Programming-Language/tools/pull/108


Re: Is this RDMD bug ?

2014-08-23 Thread sigod via Digitalmars-d-learn
On Saturday, 23 August 2014 at 17:32:15 UTC, Vladimir Panteleev 
wrote:

No, it is not an rdmd bug.

etc is a standard D package name reserved for Phobos, the 
standard library. It is the same for std and core.


Please, point us directly to a documentation where it says that 
this words reserved.


Re: Is this RDMD bug ?

2014-08-23 Thread sigod via Digitalmars-d-learn

On Saturday, 23 August 2014 at 17:41:38 UTC, Dicebot wrote:

On Saturday, 23 August 2014 at 17:37:39 UTC, sigod wrote:
On Saturday, 23 August 2014 at 17:32:15 UTC, Vladimir 
Panteleev wrote:

No, it is not an rdmd bug.

etc is a standard D package name reserved for Phobos, the 
standard library. It is the same for std and core.


Please, point us directly to a documentation where it says 
that this words reserved.


http://dlang.org/phobos/ ?
(you don't expect to casually use package names std and 
core either, do you?)


Actually, I never got to use this names for first package name. 
(I only used `something.etc` and `something.core`.) So, I 
didn't thought about this.


But, if to think about it know:
`std` - no.
`core` - yes.
`etc` - yes.

It isn't documented. So, why should I think that this is reserved 
package names? Actually, I myself never thought that there's 
exist such thing as reserved package name.


Isn't it better to document such things? Less questions will 
arouse. Less users will stumble on strange errors.


Re: Is this RDMD bug ?

2014-08-23 Thread sigod via Digitalmars-d-learn
On Saturday, 23 August 2014 at 18:28:32 UTC, Vladimir Panteleev 
wrote:

On Saturday, 23 August 2014 at 18:23:25 UTC, sigod wrote:

Isn't it better to document such things?


Yes. Please create a pull request.


Easy to say. In my TODO list lies record to create PR for [this 
issue][0]. Today is exactly 2 months as it was created.


[0]: https://issues.dlang.org/show_bug.cgi?id=12986


Re: private selective imports

2014-08-06 Thread sigod via Digitalmars-d-learn

On Wednesday, 6 August 2014 at 18:33:23 UTC, Dicebot wrote:
Most voted DMD bug : 
https://issues.dlang.org/show_bug.cgi?id=314


+1 vote from me.


Re: unittest affects next unittest

2014-08-05 Thread sigod via Digitalmars-d-learn
On Saturday, 2 August 2014 at 06:46:04 UTC, Jonathan M Davis via 
Digitalmars-d-learn wrote:

On Fri, 01 Aug 2014 23:09:37 +
sigod via Digitalmars-d-learn 
digitalmars-d-learn@puremagic.com wrote:



Code: http://dpaste.dzfl.pl/51bd62138854
(It was reduced by DustMite.)

Have I missed something about structs? Or this simply a bug?


Don't do this with a member variable:

private Node * _root = new Node();

Directly initializing it like that sets the init value for that 
struct, and
that means that every struct of that type will have exactly the 
same value for
_root, so they will all share the same root rather than having 
different

copies. You need to initialize _root in the constructor.

- Jonathan M Davis


So, it's a static initialization? Documentation didn't mention 
it. (In class' section only 2 sentences about it and none in 
struct's section.)


This is different from many languages (C#, Java... don't know 
about C and C++). What was the reason to make this initialization 
static?


unittest affects next unittest

2014-08-01 Thread sigod via Digitalmars-d-learn

Code: http://dpaste.dzfl.pl/51bd62138854
(It was reduced by DustMite.)

Have I missed something about structs? Or this simply a bug?


Re: fuction Return function

2014-07-26 Thread sigod via Digitalmars-d-learn

On Saturday, 26 July 2014 at 20:49:30 UTC, seany wrote:
Can a function return a function in D? Sorry if i missed the 
answer somewhere


Just alias your function signature:

```d
alias MyFunctionType = void function(int);
```

Example from my own code:
```d
alias DeserializeBody = TLObject function(InBuffer);

DeserializeBody[uint] registerAll(modules...)()
{
// ...
}
```



Re: mixin assembler does not work?

2014-07-21 Thread sigod via Digitalmars-d-learn

On Sunday, 20 July 2014 at 15:54:15 UTC, bearophile wrote:

mixin template Vala2(uint count, alias arr) {


What about disallowing mixin templatename unless you add 
mixin before the template keyword?


Bye,
bearophile


I thought it's disallowed.


Re: Get folders in path

2014-07-15 Thread sigod via Digitalmars-d-learn

On Tuesday, 15 July 2014 at 08:31:10 UTC, pgtkda wrote:

How can i get all folders from a given path?


If I understood you correctly:
http://dlang.org/phobos/std_file.html#.dirEntries


Re: std.algorithm.among

2014-07-13 Thread sigod via Digitalmars-d-learn

On Sunday, 13 July 2014 at 11:18:05 UTC, bearophile wrote:
The idea of not making std.algorithm.among!() a predicate was 
not so good:



void main() {
import std.stdio, std.algorithm;
auto s = hello how\nare you;
s.until!(c = c.among!('\n', '\r')).writeln;
}


(A normal workaround is to use !!c.among!).

Bye,
bearophile


```
s.until!(among!('\n', '\r')).writeln; // Error: cannot implicitly 
convert expression (among(front(this._input))) of type uint to 
bool

```

:-(


Re: Concatenates int

2014-07-10 Thread sigod via Digitalmars-d-learn

On Thursday, 10 July 2014 at 17:30:17 UTC, Sean Campbell wrote:
if I need to Concatenate ints I'l just use a recursive pow 
based on length


int ConcatInt(int[] anint){
int total = 0;
for(int i=0;ianint.length;i++){
total += anint[i]*10^^(anint.length-i-1);
}
return total;
}


With `foreach_reverse` it looks a little better:

```d
int concat_ints(int[] ints)
{
int result;

foreach_reverse (i, int_; ints) {
result += int_ * 10 ^^ (ints.length - i - 1);
}

return result;
}
```


Re: Using enum constant from different modules

2014-07-10 Thread sigod via Digitalmars-d-learn

On Thursday, 10 July 2014 at 20:59:17 UTC, simendsjo wrote:

Strings behaves a bit odd with is(). The following passes:

import std.stdio;
void f(string a, string b) {
assert(a is b); // also true
}
void main() {
string a = aoeu;
string b = aoeu;
assert(a is b); // true
f(a, b);
writeln(passed);
}


```d
import std.stdio;
void f(string a, string b) {
   writeln(a: , a.ptr, , b: , b.ptr);
   assert(a is b); // also true
}
void main() {
   string a = aoeu;
   string b = aoeu;
   writeln(a: , a.ptr, , b: , b.ptr);
   assert(a is b); // true
   f(a, b);
   writeln(passed);
}
```

Output:
```
a: 4210A0, b: 4210A0
a: 4210A0, b: 4210A0
passed
```

Seems legit to me.


Re: SList: How do I use linearRemove?

2014-06-28 Thread sigod via Digitalmars-d-learn

On Thursday, 26 June 2014 at 16:50:38 UTC, Lemonfiend wrote:

This doesn't (why?):
auto s = SList!int(1, 2, 3, 4, 5);
auto s2 = SList!int(1, 2, 3, 4, 5);
auto r = s2[];
popFrontN(r, 1);
auto r1 = s.linearRemove(r);


This is intended behavior: 
https://issues.dlang.org/show_bug.cgi?id=12999


Re: import except one?

2014-06-26 Thread sigod via Digitalmars-d-learn

```
import std.process : Config_ = Config;
```


Re: import except one?

2014-06-26 Thread sigod via Digitalmars-d-learn

Sorry, wrong one.

There seems no solution for this. So, you must use fully 
qualified name.


Re: SList: How do I use linearRemove?

2014-06-26 Thread sigod via Digitalmars-d-learn

Take a look at unittests in [std.container.slist][0]:

```
unittest
{
auto s = SList!int(1, 2, 3, 4, 5);
auto r = s[];
popFrontN(r, 3);
auto r1 = s.linearRemove(r);
assert(s == SList!int(1, 2, 3));
assert(r1.empty);
}

unittest
{
auto s = SList!int(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
auto r = s[];
popFrontN(r, 3);
auto r1 = take(r, 4);
assert(equal(r1, [4, 5, 6, 7]));
auto r2 = s.linearRemove(r1);
assert(s == SList!int(1, 2, 3, 8, 9, 10));
assert(equal(r2, [8, 9, 10]));
}
```

[0]: 
https://github.com/D-Programming-Language/phobos/blob/master/std/container/slist.d#L575


Re: import except one?

2014-06-26 Thread sigod via Digitalmars-d-learn

Dirty solution:

```
import scriptlike;
import your_module;
import your_module : Config;
```

So, `Config` from your module will override one from scriptlike.


Re: SList: How do I use linearRemove?

2014-06-26 Thread sigod via Digitalmars-d-learn

First case is a bug. I'll make pull request.
Not sure about second.


Re: mixin(__MODULE__) fails if module name is module

2014-06-24 Thread sigod via Digitalmars-d-learn

I opened new issue: https://issues.dlang.org/show_bug.cgi?id=12986


mixin(__MODULE__) fails if module name is module

2014-06-22 Thread sigod via Digitalmars-d-learn

E.g.:

module.d: (or just `module module;` in source file)
```
import std.stdio;

void main() {
foreach (m; __traits(allMembers, mixin(__MODULE__))) { // 
module.d-mixin-4(4): Error: expression expected, not 'module'

writeln(m);
}
}
```

Documentation says:
Package names cannot be keywords, hence the corresponding 
directory names cannot be keywords, either.


Shouldn't keywords be disallowed for module names?


Re: mixin(__MODULE__) fails if module name is module

2014-06-22 Thread sigod via Digitalmars-d-learn

On Sunday, 22 June 2014 at 12:52:11 UTC, sigod wrote:

module.d: (or just `module module;` in source file)


I was wrong about `module module;` declaration.


Re: mixin(__MODULE__) fails if module name is module

2014-06-22 Thread sigod via Digitalmars-d-learn

This question seems more fit for the main D newsgroup.


Should I create new thread in the main newsgroup?


Look in Bugzilla if there is a enhancement request.


Yeah. I found one: https://issues.dlang.org/show_bug.cgi?id=456


package reflection

2014-06-22 Thread sigod via Digitalmars-d-learn
In the video Case Studies In Simplifying Code With Compile-Time 
Reflection [was pointed out][0] that it is possible to reflect 
on imported packages.


So, I tried:

reflection.d:
```
import std.stdio;

import test.module1;
import test.module2;

void main() {
foreach (m; __traits(allMembers, mixin(__MODULE__))) {
writeln(m);
}

writeln(--);

foreach (m; __traits(allMembers, test)) {
writeln(m);
}
}
```

test/module1.d:
```
module test.module1;

void module_1() {}
```

test/module2.d:
```
module test.module2;

void module_2() {}
```

It produces:
```
$ rdmd reflection.d
object
std
test
main
--
object
module_1
```

As you see `module_2` wasn't listed. If I change order of import 
declarations `module_2` will be listed instead of `module_1`.


I also tried to create `test/package.d` and publicly import other 
modules through it, but it behave in the same way.


So, how to reflect on imported packages?

[0]: http://youtu.be/xpImt14KTdc?t=42m26s


Re: Doing exercise from book, but I'm getting error with splitter

2014-06-18 Thread sigod via Digitalmars-d-learn

On Monday, 16 June 2014 at 16:49:46 UTC, Andrew Brown wrote:
   ulong[string] dictionary; // the length property is ulong, 
not

uint


Actually length is size_t (uint on x86 and ulong on x64).


Re: hijacking override from template mixin

2014-06-14 Thread sigod via Digitalmars-d-learn

On Monday, 9 June 2014 at 15:54:21 UTC, Ivan Kazmenko wrote:
I'd expect a multiple overrides of same function error, much 
like if I just paste the mixin code by hand.  Is that a bug or 
working by design?  In the latter case, please explain the 
reasoning.


http://dlang.org/template-mixin.html says:
The declarations in a mixin are ‘imported’ into the surrounding 
scope. If the name of a declaration in a mixin is the same as a 
declaration in the surrounding scope, the surrounding 
declaration overrides the mixin one.


Re: __traits with alias

2014-05-08 Thread sigod via Digitalmars-d-learn
On Thursday, 8 May 2014 at 07:33:34 UTC, Philippe Sigaud via 
Digitalmars-d-learn wrote:
A workaround is to wrap it into another template, to 'hide' 
__traits.


Like this:

alias Alias(alias a) = a; // A bit circular, I know.

Oh, thank you.

I think there is bug report / enhancement for this. I find it a 
bit

annoying, since aliasing a __traits expression is quite common.

This one https://issues.dlang.org/show_bug.cgi?id=7804, I believe.


Re: Down the VisualD0.3.38-1.exe ,found virus!

2014-05-08 Thread sigod via Digitalmars-d-learn

On Friday, 9 May 2014 at 01:02:39 UTC, FrankLike wrote:

Hi,everyone,
down VisulaD from 
http://rainers.github.io/visuald/visuald/StartPage.html

found the virus:Win32.Troj.Undef.(kcloud)

Why?

Frank


https://www.virustotal.com/en/file/bbd76ddb41a80f0526f6cf1e37a2db2736cfa8f29ed3f5fd7a4336bf4c8bbe43/analysis/

Just 5 of 52. Probably a false alarm.


__traits with alias

2014-05-07 Thread sigod via Digitalmars-d-learn

void registerAll(alias module_)()
{
 foreach (m; __traits(derivedMembers, module_)) {
 regInner!(__traits(getMember, module_, m)); // compiles

 alias a = __traits(getMember, module_, m); // fails
 //Error: basic type expected, not __traits
 //Error: semicolon expected to close alias declaration
 }
}

void regInner(alias T)()
{
 // ...
}

Is this a bug or I've missed something?