Re: Bug? 0 is less than -10

2015-10-06 Thread Laeeth Isharc via Digitalmars-d-learn
On Wednesday, 7 October 2015 at 02:53:32 UTC, Steven 
Schveighoffer wrote:

On 10/6/15 7:21 PM, Laeeth Isharc wrote:
could we have ssize_t defined in phobos somewhere so your code 
ends up

being portable ;) (It's trivial to do, obviously).


ptrdiff_t

-Steve


It seems unnatural to use such a name when the variable has 
nothing to do with pointers - it doesn't contribute to the 
readability.  Yes, it's trivial, but small things cumulatively 
matter.  Adam tends to use int and when that gets mixed up with 
an auto size_t (eg via length) then his code doesn't compile on 
64 bit.  And if it happens with his code, you can imagine this 
isn't a problem that inexperienced users never encounter.


Re: Bug? 0 is less than -10

2015-10-06 Thread Steven Schveighoffer via Digitalmars-d-learn

On 10/6/15 7:21 PM, Laeeth Isharc wrote:

could we have ssize_t defined in phobos somewhere so your code ends up
being portable ;) (It's trivial to do, obviously).


ptrdiff_t

-Steve


Re: AWS API Dlang, hmac sha256 function.

2015-10-06 Thread Rikki Cattermole via Digitalmars-d-learn

On 07/10/15 3:18 PM, holo wrote:

Congrats on getting it working!


@Rikki Thanks :)

I was trying to write my own lib from beginning based on examples but
after some time i resign from that idea (will back to it when i will
have some more experience) and right now im trying to customize that one
from link which yawniek paste:

https://github.com/yannick/vibe-aws/blob/master/source/vibe/aws/sigv4.d

I removed import from vibe.d library and copy/paste missed functions
formEncode and filterURLEncode (BTW: what that "(R)" mean in it?
filterURLEncode(R)(ref R dst, ..., ..) ).


If you see a template argument (which R is) it is typically meant for a 
range. In this case an output range. Although there should be 
isOutputRange!R in an template if condition, to check this.



Next thing what i did was to replace hmac function to use hmac module
from newest library. Now whole code looks like this:

module sigv4;

import std.array;
import std.algorithm;
import std.digest.sha;
import std.range;
import std.stdio;
import std.string;
import std.format;
import std.digest.hmac;


const algorithm = "AWS4-HMAC-SHA256";


void filterURLEncode(R)(ref R dst, string str, string allowed_chars =
null, bool form_encoding = false)
{
 while( str.length > 0 ) {
 switch(str[0]) {
 case ' ':
 if (form_encoding) {
 dst.put('+');
 break;
 }
 goto default;
 case 'A': .. case 'Z':
 case 'a': .. case 'z':
 case '0': .. case '9':
 case '-': case '_': case '.': case '~':
 dst.put(str[0]);
 break;
 default:
 if (allowed_chars.canFind(str[0])) dst.put(str[0]);
 else formattedWrite(dst, "%%%02X", str[0]);
 }
 str = str[1 .. $];
 }
}

string formEncode(string str, string allowed_chars = null)
@safe {
 auto dst = appender!string();
 dst.reserve(str.length);
 filterURLEncode(dst, str, allowed_chars, true);
 return dst.data;
}

struct CanonicalRequest
{
 string method;
 string uri;
 string[string] queryParameters;
 string[string] headers;
 ubyte[] payload;
}

string canonicalQueryString(string[string] queryParameters)
{
 alias encode = formEncode;

 string[string] encoded;
 foreach (p; queryParameters.keys())
 {
 encoded[encode(p)] = encode(queryParameters[p]);
 }
 string[] keys = encoded.keys();
 sort(keys);
 return keys.map!(k => k ~ "=" ~ encoded[k]).join("&");
}

string canonicalHeaders(string[string] headers)
{
 string[string] trimmed;
 foreach (h; headers.keys())
 {
 trimmed[h.toLower().strip()] = headers[h].strip();
 }
 string[] keys = trimmed.keys();
 sort(keys);
 return keys.map!(k => k ~ ":" ~ trimmed[k] ~ "\n").join("");
}

string signedHeaders(string[string] headers)
{
 string[] keys = headers.keys().map!(k => k.toLower()).array();
 sort(keys);
 return keys.join(";");
}

string hash(T)(T payload)
{
 auto hash = sha256Of(payload);
 return hash.toHexString().toLower();
}

string makeCRSigV4(CanonicalRequest r)
{
 auto cr =
 r.method.toUpper() ~ "\n" ~
 (r.uri.empty ? "/" : r.uri) ~ "\n" ~
 canonicalQueryString(r.queryParameters) ~ "\n" ~
 canonicalHeaders(r.headers) ~ "\n" ~
 signedHeaders(r.headers) ~ "\n" ~
 hash(r.payload);

 return hash(cr);
}

unittest {
 string[string] empty;

 auto r = CanonicalRequest(
 "POST",
 "/",
 empty,
 ["content-type": "application/x-www-form-urlencoded;
charset=utf-8",
  "host": "iam.amazonaws.com",
  "x-amz-date": "20110909T233600Z"],
 cast(ubyte[])"Action=ListUsers&Version=2010-05-08");

 auto sig = makeCRSigV4(r);

 assert(sig ==
"3511de7e95d28ecd39e9513b642aee07e54f4941150d8df8bf94b328ef7e55e2");
}

struct SignableRequest
{
 string dateString;
 string timeStringUTC;
 string region;
 string service;
 CanonicalRequest canonicalRequest;
}

string signableString(SignableRequest r) {
 return algorithm ~ "\n" ~
 r.dateString ~ "T" ~ r.timeStringUTC ~ "Z\n" ~
 r.dateString ~ "/" ~ r.region ~ "/" ~ r.service ~
"/aws4_request\n" ~
 makeCRSigV4(r.canonicalRequest);
}

unittest {
 string[string] empty;

 SignableRequest r;
 r.dateString = "20110909";
 r.timeStringUTC = "233600";
 r.region = "us-east-1";
 r.service = "iam";
 r.canonicalRequest = CanonicalRequest(
 "POST",
 "/",
 empty,
 ["content-type": "application/x-www-form-urlencoded;
charset=utf-8",
  "host": "iam.amazonaws.com",
  "x-amz-date": "20110909T233600Z"],
 cast(ubyte[])"Action=ListUsers&Version=2010-05-08");

 auto sampleString =
 algorithm ~ "\

Re: AWS API Dlang, hmac sha256 function.

2015-10-06 Thread holo via Digitalmars-d-learn

Congrats on getting it working!


@Rikki Thanks :)

I was trying to write my own lib from beginning based on examples 
but after some time i resign from that idea (will back to it when 
i will have some more experience) and right now im trying to 
customize that one from link which yawniek paste:


https://github.com/yannick/vibe-aws/blob/master/source/vibe/aws/sigv4.d

I removed import from vibe.d library and copy/paste missed 
functions formEncode and filterURLEncode (BTW: what that "(R)" 
mean in it? filterURLEncode(R)(ref R dst, ..., ..) ).


Next thing what i did was to replace hmac function to use hmac 
module from newest library. Now whole code looks like this:


module sigv4;

import std.array;
import std.algorithm;
import std.digest.sha;
import std.range;
import std.stdio;
import std.string;
import std.format;
import std.digest.hmac;


const algorithm = "AWS4-HMAC-SHA256";


void filterURLEncode(R)(ref R dst, string str, string 
allowed_chars = null, bool form_encoding = false)

{
while( str.length > 0 ) {
switch(str[0]) {
case ' ':
if (form_encoding) {
dst.put('+');
break;
}
goto default;
case 'A': .. case 'Z':
case 'a': .. case 'z':
case '0': .. case '9':
case '-': case '_': case '.': case '~':
dst.put(str[0]);
break;
default:
if (allowed_chars.canFind(str[0])) 
dst.put(str[0]);
else formattedWrite(dst, "%%%02X", str[0]);
}
str = str[1 .. $];
}
}

string formEncode(string str, string allowed_chars = null)
@safe {
auto dst = appender!string();
dst.reserve(str.length);
filterURLEncode(dst, str, allowed_chars, true);
return dst.data;
}

struct CanonicalRequest
{
string method;
string uri;
string[string] queryParameters;
string[string] headers;
ubyte[] payload;
}

string canonicalQueryString(string[string] queryParameters)
{
alias encode = formEncode;

string[string] encoded;
foreach (p; queryParameters.keys())
{
encoded[encode(p)] = encode(queryParameters[p]);
}
string[] keys = encoded.keys();
sort(keys);
return keys.map!(k => k ~ "=" ~ encoded[k]).join("&");
}

string canonicalHeaders(string[string] headers)
{
string[string] trimmed;
foreach (h; headers.keys())
{
trimmed[h.toLower().strip()] = headers[h].strip();
}
string[] keys = trimmed.keys();
sort(keys);
return keys.map!(k => k ~ ":" ~ trimmed[k] ~ "\n").join("");
}

string signedHeaders(string[string] headers)
{
string[] keys = headers.keys().map!(k => k.toLower()).array();
sort(keys);
return keys.join(";");
}

string hash(T)(T payload)
{
auto hash = sha256Of(payload);
return hash.toHexString().toLower();
}

string makeCRSigV4(CanonicalRequest r)
{
auto cr =
r.method.toUpper() ~ "\n" ~
(r.uri.empty ? "/" : r.uri) ~ "\n" ~
canonicalQueryString(r.queryParameters) ~ "\n" ~
canonicalHeaders(r.headers) ~ "\n" ~
signedHeaders(r.headers) ~ "\n" ~
hash(r.payload);

return hash(cr);
}

unittest {
string[string] empty;

auto r = CanonicalRequest(
"POST",
"/",
empty,
["content-type": "application/x-www-form-urlencoded; 
charset=utf-8",

 "host": "iam.amazonaws.com",
 "x-amz-date": "20110909T233600Z"],
cast(ubyte[])"Action=ListUsers&Version=2010-05-08");

auto sig = makeCRSigV4(r);

assert(sig == 
"3511de7e95d28ecd39e9513b642aee07e54f4941150d8df8bf94b328ef7e55e2");

}

struct SignableRequest
{
string dateString;
string timeStringUTC;
string region;
string service;
CanonicalRequest canonicalRequest;
}

string signableString(SignableRequest r) {
return algorithm ~ "\n" ~
r.dateString ~ "T" ~ r.timeStringUTC ~ "Z\n" ~
r.dateString ~ "/" ~ r.region ~ "/" ~ r.service ~ 
"/aws4_request\n" ~

makeCRSigV4(r.canonicalRequest);
}

unittest {
string[string] empty;

SignableRequest r;
r.dateString = "20110909";
r.timeStringUTC = "233600";
r.region = "us-east-1";
r.service = "iam";
r.canonicalRequest = CanonicalRequest(
"POST",
"/",
empty,
["content-type": "application/x-www-form-urlencoded; 
charset=utf-8",

 "host": "iam.amazonaws.com",
 "x-amz-date": "20110909T233600Z"],
cast(ubyte[])"Action=ListUsers&Version=2010-05-08");

auto sampleString =
algorithm ~ "\n" ~
"20110909T233600Z\n" ~
 

Re: How to check if JSONValue of type object has a key?

2015-10-06 Thread Marco Leise via Digitalmars-d-learn
Am Tue, 06 Oct 2015 21:39:28 +
schrieb Fusxfaranto :

> Additionally, just like associative arrays, if you need to access 
> the value, you can get a pointer to it with the in operator (and 
> if the key doesn't exist, it will return a null pointer).
> 
> const(JSONValue)* p = "key" in root;
> if (p)
> {
>  // key exists, do something with p or *p
> }
> else
> {
>  // key does not exist
> }

And you could go further and write

if (auto p = "key" in root)
{
 // key exists, do something with p or *p
}
else
{
 // key does not exist
}

-- 
Marco



Re: Varargs and default arguments

2015-10-06 Thread Steven Schveighoffer via Digitalmars-d-learn

On 10/6/15 4:27 PM, anonymous wrote:


You can put an expression tuple ("expression AliasSeq"??) there. T.init is
one that always fits T's types. But you could generate one with different
values, too.


void foo(T...)(string str=null, T args = T.init) {
//...
}
void main()
{
foo();
foo("");


What is T in this case? I wondered, and it is an empty expression tuple 
(prints out as "()"). Interesting.


Note, this doesn't seem to work on 2.067.

There are other ways to solve this too:

void foo() {return foo(null);}
void foo(T...)(string str, T args) {...}

I find it quite fascinating that in anonymous' solution, the T.init 
doesn't ever actually get used!


-Steve


Re: Bug? 0 is less than -10

2015-10-06 Thread Laeeth Isharc via Digitalmars-d-learn

On Tuesday, 6 October 2015 at 14:55:23 UTC, Adam D. Ruppe wrote:

On Tuesday, 6 October 2015 at 14:46:56 UTC, tcak wrote:

void main(){
size_t dec = 0;


How is it generating "true" for (dec <= -10) ? Is there a 
special casting or something?


size_t is unsigned, so the -10 is cast to unsigned too for the 
comparison which yields some huge number.


Comparing signed to unsigned is almost always a mistake... but 
one D inherited from C.


This is a reason why I prefer to use int instead of size_t 
where I can but that might require casts and truncation too.


could we have ssize_t defined in phobos somewhere so your code 
ends up being portable ;) (It's trivial to do, obviously).


Re: How to check if JSONValue of type object has a key?

2015-10-06 Thread Fusxfaranto via Digitalmars-d-learn
On Tuesday, 6 October 2015 at 20:44:30 UTC, via 
Digitalmars-d-learn wrote:
On Tue, Oct 06, 2015 at 08:28:46PM +, Borislav Kosharov via 
Digitalmars-d-learn wrote:

JSONValue root = parseJSON(text);
if(root["key"].isNull == false) {


try

if("key" in root) {
// it is there
} else {
// it is not there
}


you can also do

if("key" !in root) {}


Additionally, just like associative arrays, if you need to access 
the value, you can get a pointer to it with the in operator (and 
if the key doesn't exist, it will return a null pointer).


const(JSONValue)* p = "key" in root;
if (p)
{
// key exists, do something with p or *p
}
else
{
// key does not exist
}


Re: How to check if JSONValue of type object has a key?

2015-10-06 Thread via Digitalmars-d-learn
On Tue, Oct 06, 2015 at 08:28:46PM +, Borislav Kosharov via 
Digitalmars-d-learn wrote:
> JSONValue root = parseJSON(text);
> if(root["key"].isNull == false) {

try

if("key" in root) {
// it is there
} else {
// it is not there
}


you can also do

if("key" !in root) {}



Re: Problem with type cast in if condition

2015-10-06 Thread Adam D. Ruppe via Digitalmars-d-learn

On Tuesday, 6 October 2015 at 20:35:28 UTC, NiRu wrote:

if(is(typeof(value) == User)){


Use `static if` instead of plain `if` here.

Regular if does a runtime branch, so both true and else branches 
must compile with the same types.


Static if does a compile time branch, allowing different code to 
be compiled based on the condition - meaning types can change too.


Problem with type cast in if condition

2015-10-06 Thread NiRu via Digitalmars-d-learn

Hi there,

I have a problem with the typecast within my if condition, the 
dmd2 compiler says he can not cast int to User, but it never 
reaches an int type the condition (at most, the else branch).


my try:

struct User {
string name;
}

void printName(T)(T value)
{
if(is(typeof(value) == User)){
User u = cast(User) value;
writeln(u.name);

write("User Type: ");
writeln(value);
} else {
write("Another Type: ");
writeln(value);
}

}

void main(string[] args)
{
User person;
person.name = "Jacub";

printName(362);
printName("have a nice day");
printName(person);
}



without the type cast line I have the following output:


Another Type: 362
Another Type: have a nice day
User Type: User("Jacub")


Instead with type cast "Error: cannot cast expression value of 
type int to User"


where is the problem?

Thanks!


How to check if JSONValue of type object has a key?

2015-10-06 Thread Borislav Kosharov via Digitalmars-d-learn
I'm using std.json for parsing json. I need to check if a 
specific string key is in JSONValue.object. The first thing I 
tried was:


JSONValue root = parseJSON(text);
if(root["key"].isNull == false) {
//do stuff with root["key"]
}

But that code doesn't work, because calling root["key"] will 
throw an exception of key not fount if "key" isn't there.


I need some method `root.contains("key")` method to check if the 
object has a key.


Thanks in advance!



Re: Varargs and default arguments

2015-10-06 Thread anonymous via Digitalmars-d-learn
On Tuesday 06 October 2015 22:01, Nick Sabalausky wrote:

> Ok, D-style varargs can accept a parameter length of zero:
> 
> ---
> void foo(T...)(T args) {
>  //...
> }
> foo();
> foo(t1, t2);
> ---

Terminology fun:

The spec uses the term "D-style variadic function" for something else yet: 
`int abc(char c, ...);` -- http://dlang.org/function.html#variadic

Yours is technically a "variadic function template", I guess, i.e., a 
function template that's variadic, not a template of a variadic function.

> Is there any way to stick a param with a default value before that?
> 
> ---
> void foo(T...)(string str=null, T args=/+what goes here???+/) {
>  //...
> }
> foo();
> foo(str);
> foo(str, t1, t2);
> ---
> 
> Obviously this can be worked around easily enough by splitting it into 
> two overloads ("foo(string str=null)" and "foo(string str, T args)"), 
> but is there a way to do it in one?

You can put an expression tuple ("expression AliasSeq"??) there. T.init is 
one that always fits T's types. But you could generate one with different 
values, too.


void foo(T...)(string str=null, T args = T.init) {
//...
}
void main()
{
foo();
foo("");
foo("", 1, 2);
}



Varargs and default arguments

2015-10-06 Thread Nick Sabalausky via Digitalmars-d-learn

Ok, D-style varargs can accept a parameter length of zero:

---
void foo(T...)(T args) {
//...
}
foo();
foo(t1, t2);
---

Is there any way to stick a param with a default value before that?

---
void foo(T...)(string str=null, T args=/+what goes here???+/) {
//...
}
foo();
foo(str);
foo(str, t1, t2);
---

Obviously this can be worked around easily enough by splitting it into 
two overloads ("foo(string str=null)" and "foo(string str, T args)"), 
but is there a way to do it in one?


Re: Linker error with dmd

2015-10-06 Thread Chris via Digitalmars-d-learn

On Friday, 2 October 2015 at 14:03:08 UTC, John Colvin wrote:

On Friday, 2 October 2015 at 09:43:54 UTC, Chris wrote:
Why do I get this error msg with dmd 2.067.1 and 2.068.0 in 
release mode:


$ dub --build=release

(.data._D65TypeInfo_xC3std5range10interfaces18__T10InputRangeTiZ10InputRange6__initZ+0x10):
 undefined reference to 
`_D64TypeInfo_C3std5range10interfaces18__T10InputRangeTiZ10InputRange6__initZ'
collect2: error: ld returned 1 exit status
--- errorlevel 1
dmd failed with exit code 1.

It works fine with the latest version of ldc2.


What is it that you are building?


An executable. I mainly use a code base that compiles perfectly 
well in release mode. I couldn't find the reason for this error 
message, plus, ldc2 has no problem with it.


Re: Bug? 0 is less than -10

2015-10-06 Thread Meta via Digitalmars-d-learn

On Tuesday, 6 October 2015 at 14:46:56 UTC, tcak wrote:

Maybe I am just too stressed out to see the problem.

[code]
import std.stdio;

void main(){
size_t dec = 0;

	writeln( dec, " ", (dec <= -10), " ", (dec >= 10), " ", ((dec 
<= -10) || (dec >= 10)) );

}
[/code]

[output]
0 true false true
[/output]

How is it generating "true" for (dec <= -10) ? Is there a 
special casting or something?


DMD 2.068.2, Ubuntu 64-bit


If you need to mix signed and unsigned values but want to avoid 
this issue, you an use the comparison functions from 
std.functional.


http://dlang.org/phobos/std_functional.html#.lessThan


Re: What is the postfix for min long value?

2015-10-06 Thread Jonathan M Davis via Digitalmars-d-learn
On Tuesday, October 06, 2015 15:16:12 tcak via Digitalmars-d-learn wrote:
> While writing max ulong value, I added the "u" postfix. So
> compiler accepted it as ulong value (That's my interpretation if
> correct on compiler's side).
>
> writeln( 18_446_744_073_709_551_615u );
>
> But when I try to print out minimum value of long, compiler says
> Error: signed integer overflow
>
> writeln( -9_223_372_036_854_775_808 );
>
> In case that is the wrong value, I checked it with writeln(
> long.min ); already.
>
> Do I need to put a postfix for that number? I checked
> documentation by searching "dlang integer" etc, but couldn't have
> found any information/anything about postfix at all.

L is the prefix to use, but it looks like there's a compiler bug here,
because

long l = -9_223_372_036_854_775_807L;

compiles

and

long l = -9_223_372_036_854_775_808L;

doesn't, even though that's the same as long.min.

long l = -9_223_372_036_854_775_807UL;

compiles and does the right thing, though that's a pretty silly thing to
have to do.

Actually, digging through bugzilla, it looks like it's already been
reported:

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

Though since C has the same problem, it's treated as an enhancement rather
than a bug (which seems wrong to me). Apparently, the problem stems from the
compiler processing the literal and _then_ applying the sign, and long.max
is 9_223_372_036_854_775_807L. Apparently, -2L^^63 will work though.

All in all, it's probably not a big deal, since you should probably just
being using long.min anyway, but this doesn't seem like particularly good
behavior to me.

- Jonathan M Davis



Re: What is the postfix for min long value?

2015-10-06 Thread anonymous via Digitalmars-d-learn
On Tuesday 06 October 2015 17:39, Ali Çehreli wrote:

> I would expect the following to work:
> 
>  writeln( -9_223_372_036_854_775_808L);
> 
> But it doesn't compile:
> 
>Error: signed integer overflow
> 
> It looks like a compiler bug to me. If so, a very embarrassing one. :)

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


Re: What is the postfix for min long value?

2015-10-06 Thread Ali Çehreli via Digitalmars-d-learn

On 10/06/2015 08:16 AM, tcak wrote:
> While writing max ulong value, I added the "u" postfix.

Better to use U to be consistent with L (see below).

> But when I try to print out minimum value of long, compiler says
> Error: signed integer overflow
>
> writeln( -9_223_372_036_854_775_808 );

I would expect the following to work:

writeln( -9_223_372_036_854_775_808L);

But it doesn't compile:

  Error: signed integer overflow

It looks like a compiler bug to me. If so, a very embarrassing one. :)

(You can use UL and LU as well.)

> Do I need to put a postfix for that number? I checked documentation by
> searching "dlang integer" etc, but couldn't have found any
> information/anything about postfix at all.

They go by "suffix". The officital documentation:

  http://dlang.org/lex.html#integerliteral

My short mention of them start at the section "The L suffix":

  http://ddili.org/ders/d.en/literals.html#ix_literals.literal

(They were missing in my index section. Adding now...)

Ali



What is the postfix for min long value?

2015-10-06 Thread tcak via Digitalmars-d-learn
While writing max ulong value, I added the "u" postfix. So 
compiler accepted it as ulong value (That's my interpretation if 
correct on compiler's side).


writeln( 18_446_744_073_709_551_615u );

But when I try to print out minimum value of long, compiler says
Error: signed integer overflow

writeln( -9_223_372_036_854_775_808 );

In case that is the wrong value, I checked it with writeln( 
long.min ); already.


Do I need to put a postfix for that number? I checked 
documentation by searching "dlang integer" etc, but couldn't have 
found any information/anything about postfix at all.


Re: Bug? 0 is less than -10

2015-10-06 Thread anonymous via Digitalmars-d-learn

On Tuesday, 6 October 2015 at 14:46:56 UTC, tcak wrote:

Maybe I am just too stressed out to see the problem.

[code]
import std.stdio;

void main(){
size_t dec = 0;

	writeln( dec, " ", (dec <= -10), " ", (dec >= 10), " ", ((dec 
<= -10) || (dec >= 10)) );

}
[/code]

[output]
0 true false true
[/output]

How is it generating "true" for (dec <= -10) ? Is there a 
special casting or something?


DMD 2.068.2, Ubuntu 64-bit


dec is a size_t. size_t is unsigned. -10 is cast to unsigned for 
the comparison, resulting in some huge value.


Re: Bug? 0 is less than -10

2015-10-06 Thread Adam D. Ruppe via Digitalmars-d-learn

On Tuesday, 6 October 2015 at 14:46:56 UTC, tcak wrote:

void main(){
size_t dec = 0;


How is it generating "true" for (dec <= -10) ? Is there a 
special casting or something?


size_t is unsigned, so the -10 is cast to unsigned too for the 
comparison which yields some huge number.


Comparing signed to unsigned is almost always a mistake... but 
one D inherited from C.


This is a reason why I prefer to use int instead of size_t where 
I can but that might require casts and truncation too.


Bug? 0 is less than -10

2015-10-06 Thread tcak via Digitalmars-d-learn

Maybe I am just too stressed out to see the problem.

[code]
import std.stdio;

void main(){
size_t dec = 0;

	writeln( dec, " ", (dec <= -10), " ", (dec >= 10), " ", ((dec <= 
-10) || (dec >= 10)) );

}
[/code]

[output]
0 true false true
[/output]

How is it generating "true" for (dec <= -10) ? Is there a special 
casting or something?


DMD 2.068.2, Ubuntu 64-bit


Re: Concatenation of ubyte[] to char[] works, but assignation doesn't

2015-10-06 Thread Jonathan M Davis via Digitalmars-d-learn
On Tuesday, October 06, 2015 09:28:27 Marc Schütz via Digitalmars-d-learn wrote:
> I see, this is a new problem introduced by `char + int = char`.
> But at least the following could be disallowed without
> introducing problems:
>
>  int a = 'a';
>  char b = 32;

Sure, it would be nice, but I rather doubt that Walter would go for it. He
seems to be fully in the camp of folks that think that life is better if
charater types and bool always are treated as integral types - which
unfortunately, creates fun problems like this

void foo(bool b) { writeln("bool"); }
void foo(long l) { writeln("long"); }

foo(1); // prints bool

In this case, adding on overload that takes int fixes the problem, because
integer literals default to int, but in general, it's just stupid IMHO. But
when it was brought up last, Walter didn't think that there was any problem
with it and that it was just fine to require that the int overload be added
to fix it.

> But strictly speaking, we already accept overflow (i.e. loss of
> precision) for ints, so it's a bit inconsistent to disallow it
> for chars.

Overflow is only permitted when doing arithmetic operations (when you really
can't do anything about it except maybe throw an exception, which would be
too expensive to be worth it) or when casting explicitly (in which case,
you're telling the compiler that you don't care). Overflow is never allowed
via implicit conversions.

- Jonathan M Davis




Re: Concatenation of ubyte[] to char[] works, but assignation doesn't

2015-10-06 Thread Ola Fosheim Grøstad via Digitalmars-d-learn

On Tuesday, 6 October 2015 at 09:28:29 UTC, Marc Schütz wrote:
I see, this is a new problem introduced by `char + int = char`. 
But at least the following could be disallowed without 
introducing problems:


int a = 'a';
char b = 32;

But strictly speaking, we already accept overflow (i.e. loss of 
precision) for ints, so it's a bit inconsistent to disallow it 
for chars.


Yes, D does not have overflow, it has modular arithmetics. So the 
same argument would hold for an enumeration (like character 
ranges), do you want them to be modular (a circle) or monotonic 
(a line). Neither is a good fit for unicode. It probably would 
make most sense to split the unicode universe into multiple typed 
ranges, some enumerations, some non-enumerations and avoid char 
altogether.




Re: Concatenation of ubyte[] to char[] works, but assignation doesn't

2015-10-06 Thread Marc Schütz via Digitalmars-d-learn
On Tuesday, 6 October 2015 at 05:38:36 UTC, Jonathan M Davis 
wrote:
Your suggestion only works by assuming that the result will fit 
in a char, which doesn't fit at all with how coversions are 
currently done in D. It would allow for narrowing conversions 
which lost data. And there's no way that Walter would go for 
that (and I don't think that he should). VRP solves the problem 
insofar as it can guarantee that the result will fit in the 
target type and thus reduces the need for casting, but simply 
assuming that char + int will fit in a char just doesn't work 
unless we're going to allow narrowing conversions to lose data, 
which we aren't.


If we were to allow the specific conversions that you're 
suggesting but only when VRP was used, then that could work, 
though it does make the implicit rules even screwier, becauses 
it becomes very dependent on how the int that you're trying to 
assign to a char was generated in the first place (straight 
assignment wouldn't work, but '0' - 40 would, whereas 'a' + 500 
wouldn't, etc.). VRP already makes it a bit funky as it is, 
though mostly in a straightforward manner.


I see, this is a new problem introduced by `char + int = char`. 
But at least the following could be disallowed without 
introducing problems:


int a = 'a';
char b = 32;

But strictly speaking, we already accept overflow (i.e. loss of 
precision) for ints, so it's a bit inconsistent to disallow it 
for chars.


Re: Picking specific D compiler with rdmd

2015-10-06 Thread Dicebot via Digitalmars-d-learn

--compiler


Re: Picking specific D compiler with rdmd

2015-10-06 Thread Nordlöw via Digitalmars-d-learn

On Tuesday, 6 October 2015 at 07:16:39 UTC, Nordlöw wrote:
I find now flag to rdmd for choosing a specific dmd. Is there 
none?


Doh, there was already: --compiler


Picking specific D compiler with rdmd

2015-10-06 Thread Nordlöw via Digitalmars-d-learn
I find now flag to rdmd for choosing a specific dmd. Is there 
none?


Re: std.Algebraic alias this

2015-10-06 Thread Radu via Digitalmars-d-learn

On Tuesday, 6 October 2015 at 06:37:16 UTC, Nicholas Wilson wrote:

On Monday, 5 October 2015 at 11:31:32 UTC, Radu wrote:
There is a weird rule on how compiler treats alias this for 
the N and S types bellow.


[...]


Please file a bug report.

Also do the errors change if you reverse the order in T i.e. 
alias T = Algebraic!(S,N); ?


bug report

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