Re: Bug? 0 is less than -10
On Wednesday, 7 October 2015 at 16:25:02 UTC, Marc Schütz wrote: Lionello Lunesu posted a PR that should fix this: https://github.com/D-Programming-Language/dmd/pull/1913 See also the discussion in the linked bug report. Unfortunately it seems it's been forgotten since then... Meanwhile I have even improved my solution posted to fix bug #259 to use less casts: int opCmp(T, U)(const(T) a, const(U) b) pure @safe @nogc nothrow if(isIntegral!T && isIntegral!U && !is(Unqual!T == Unqual!U)) { static if(isSigned!T && isUnsigned!U && T.sizeof <= U.sizeof) { return (a < 0) ? -1 : opCmp(cast(U)a, b); } else static if(isUnsigned!T && isSigned!U && T.sizeof >= U.sizeof) { return (b < 0) ? 1 : opCmp(a, cast(T)b); } else // both signed or both unsigned or the unsigned type is smaller { // do what the compiler always did so far: alias C = CommonType!(T, U); // use the larger of the both return opCmp(cast(C)a, cast(C)b); } } And on comparison with number literals everything will be optimized away, so no execution timer overhead at all!
Re: Tell GC to use shared memory
On Thursday, 8 October 2015 at 05:46:31 UTC, ketmar wrote: On Thursday, 8 October 2015 at 04:38:43 UTC, tcak wrote: Is it possible to modify GC (without rebuilding the compiler), so it uses a given shared memory area instead of heap for allocations? sure. you don't need to rebuild the compiler, only druntime. Any better solution? Like overriding GC class, etc.
Check template parameter whether it has "length"
I am "trying" to write a function that takes an array of items, and returns the length of longest item. [code] size_t maxLength(A)( const A[] listOfString ) if( __traits( hasMember, A, "length" ) ) { return 0; // not implemented yet } [/code] I tried it with if( __traits( compiles, A.length ) ) as well. But compiler doesn't match it. writeln("Max Length: ", maxLength( ["foo", "123456789"] )); Compilers says it cannot deduce function from argument types ... I do not want to check whether the type "A" is string, char[], etc. As long as it has length (please do not put me into ranges, library functions etc as much as possible), I want the function to accept it.
Re: Check template parameter whether it has "length"
On Thursday, 8 October 2015 at 09:29:30 UTC, tcak wrote: I am "trying" to write a function that takes an array of items, and returns the length of longest item. [code] size_t maxLength(A)( const A[] listOfString ) if( __traits( hasMember, A, "length" ) ) { return 0; // not implemented yet } [/code] I tried it with if( __traits( compiles, A.length ) ) as well. But compiler doesn't match it. writeln("Max Length: ", maxLength( ["foo", "123456789"] )); Compilers says it cannot deduce function from argument types ... I do not want to check whether the type "A" is string, char[], etc. As long as it has length (please do not put me into ranges, library functions etc as much as possible), I want the function to accept it. I'm 99% sure something like __traits(hasMember, int[], "length" ) should evaluate to true. Please file a bug at issues.dlang.org I notice it also doesn't work for "ptr". The correct workaround: __traits(compiles, A.init.length )); or __traits(compiles, listOfStrings.length )); A.length doesn't work because length is not a static member, so it's only accessible from an instance. The __traits(compiles, ...) solution is actually more general because it will work if .length is implemented via UFCS and opDispatch. FYI: If you want to check whether a statement will compile, as opposed to an expression, make a function/delegate out of it, e.g.: __traits(compiles, { size_t n = A.init.length; }); to check that A has a member length that can be assigned to size_t. P.S. always check std.traits for solutions all your static reflection problems, there's a lot of good stuff in there.
Re: Threading Questions
On Thursday, 8 October 2015 at 02:31:24 UTC, bitwise wrote: If you have System.Collections.Generic.List(T) static class member, there is nothing wrong with using it from multiple threads like this: The equivalent of your D example would be class Foo { static List numbers = new List(); void bar() { new Thread(()=>{ numbers.Add(1); }).Start(); } }
Re: Check template parameter whether it has "length"
On 10/08/15 11:29, tcak via Digitalmars-d-learn wrote: > I am "trying" to write a function that takes an array of items, and returns > the length of longest item. > > [code] > size_t maxLength(A)( const A[] listOfString ) if( __traits( hasMember, A, > "length" ) ) > { > return 0; // not implemented yet > } > [/code] > > I tried it with > > if( __traits( compiles, A.length ) ) > > as well. But compiler doesn't match it. Use `A.init.length` instead of `A.length`. You could also use something like if(is(typeof(A.init.length):size_t)) artur
Re: Tell GC to use shared memory
GC is chosen at link time simply to satisfy unresolved symbols. You only need to compile your modified GC and link with it, it will be chosen instead of GC from druntime, no need to recompile anything else.
Re: Check template parameter whether it has "length"
On Thursday, 8 October 2015 at 09:29:30 UTC, tcak wrote: I am "trying" to write a function that takes an array of items, and returns the length of longest item. [code] size_t maxLength(A)( const A[] listOfString ) if( __traits( hasMember, A, "length" ) ) { return 0; // not implemented yet } [/code] I tried it with if( __traits( compiles, A.length ) ) as well. But compiler doesn't match it. writeln("Max Length: ", maxLength( ["foo", "123456789"] )); Compilers says it cannot deduce function from argument types ... I do not want to check whether the type "A" is string, char[], etc. As long as it has length (please do not put me into ranges, library functions etc as much as possible), I want the function to accept it. iirc there's a hasLength trait in std.traits e.g. static assert(hasLength!int[])
Re: Threading Questions
On Thursday, 8 October 2015 at 10:11:38 UTC, Kagamin wrote: On Thursday, 8 October 2015 at 02:31:24 UTC, bitwise wrote: If you have System.Collections.Generic.List(T) static class member, there is nothing wrong with using it from multiple threads like this: The equivalent of your D example would be class Foo { static List numbers = new List(); void bar() { new Thread(()=>{ numbers.Add(1); }).Start(); } } That still doesn't explain what you mean about it being illegal in other languages or why you brought up C# in the first place. Bit
Re: Bug? 0 is less than -10
On 10/7/15 1:27 AM, Laeeth Isharc wrote: 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 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. ptrdiff_t is in the C spec, ssize_t is not. No reason to name all the types of snow here. A machine-word-sized signed integer is ptrdiff_t. -Steve
Re: Check template parameter whether it has "length"
On Thursday, 8 October 2015 at 09:50:12 UTC, John Colvin wrote: On Thursday, 8 October 2015 at 09:29:30 UTC, tcak wrote: [...] I'm 99% sure something like __traits(hasMember, int[], "length" ) should evaluate to true. Please file a bug at issues.dlang.org I notice it also doesn't work for "ptr". The correct workaround: __traits(compiles, A.init.length )); or __traits(compiles, listOfStrings.length )); A.length doesn't work because length is not a static member, so it's only accessible from an instance. The __traits(compiles, ...) solution is actually more general because it will work if .length is implemented via UFCS and opDispatch. FYI: If you want to check whether a statement will compile, as opposed to an expression, make a function/delegate out of it, e.g.: __traits(compiles, { size_t n = A.init.length; }); to check that A has a member length that can be assigned to size_t. P.S. always check std.traits for solutions all your static reflection problems, there's a lot of good stuff in there. __traits(compiles, { size_t n = A.init.length; }); did the trick. [code] size_t maxLength(A)( const A[] listOfString ) if( __traits( compiles, { size_t len = A.init.length; } ) ) { size_t len = 0; foreach(A str; listOfString) if( str.length > len ) len = str.length; return len; } [/code] BTW, there is nothing like std.traits.hasLength.
Re: Check template parameter whether it has "length"
On Thursday, 8 October 2015 at 15:22:02 UTC, tcak wrote: BTW, there is nothing like std.traits.hasLength. yeah, that's because __traits(hasMember, ...) should be good enough, but obviously not in this case at the moment.
Re: Check template parameter whether it has "length"
On Thursday, 8 October 2015 at 15:22:02 UTC, tcak wrote: BTW, there is nothing like std.traits.hasLength. You're just looking in the wrong place =) http://dlang.org/phobos/std_range_primitives.html#hasLength
Re: Threading Questions
On Thursday, 8 October 2015 at 13:44:46 UTC, bitwise wrote: That still doesn't explain what you mean about it being illegal in other languages or why you brought up C# in the first place. Illegal means the resulting program behaves incorrectly, potentially leading to silent failures and data corruption. C# is a language that allows such bugs, and D disallows them - treats such code as invalid and rejects.
Array of BitArrays definition compiles in DMD but not in GDC.
In my code I'm passing an array of BitArrays to a constructor like this (though mostly as a placeholder): Terrain t = new Terrain(1, 15, [ BitArray([1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]), BitArray([1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1]), BitArray([0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1]), BitArray([0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1]), BitArray([0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1]), BitArray([1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1]), ]); The code compiles fine when using DMD 2.068.1 but GDC 5.2.1, I get this error for every line: error: cannot implicityly convert expression ([stuff]) of type int[] to ulong Why does this compiler difference exist and how do I get around it? Thanks!
Re: How to check if JSONValue of type object has a key?
Thanks guys that was I was looking for!
Re: Threading Questions
On Thursday, 8 October 2015 at 20:42:46 UTC, Kagamin wrote: On Thursday, 8 October 2015 at 13:44:46 UTC, bitwise wrote: That still doesn't explain what you mean about it being illegal in other languages or why you brought up C# in the first place. Illegal means the resulting program behaves incorrectly, potentially leading to silent failures and data corruption. C# is a language that allows such bugs, and D disallows them - treats such code as invalid and rejects. Ah, I see. I thought you meant illegal meant it won't compile. Wouldn't it be more correct to say that it's undefined behaviour? Bit
Re: AWS API Dlang, hmac sha256 function.
I was fighting with it a little bit and after all i just leave original function which was in code, removed dependencies from vibe.d and finally tried to contact api. Here is my present code: #!/usr/bin/rdmd -L-lcurl import std.stdio; import std.string; import std.file; import std.datetime; import std.process; import std.digest.sha; import std.net.curl; import sigv4; auto zone = "us-east-1"; auto service = "ec2"; void main() { auto accKey = environment["AWS_ACCESS_KEY"]; auto secKey = environment["AWS_SECRET_KEY"]; auto currentClock = Clock.currTime; auto currentDate = cast(Date)currentClock; auto curDateStr = currentDate.toISOString; auto currentTime = cast(TimeOfDay)currentClock; auto curTimeStr = currentTime.toISOString; auto xamztime = curDateStr ~ "T" ~ curTimeStr ~ "Z"; string[string] empty; SignableRequest r; r.dateString = curDateStr; r.timeStringUTC = curTimeStr; r.region = zone; r.service = service; r.canonicalRequest = CanonicalRequest( "GET", "/", ["action" : "DescribeInstances", "version" : "2013-10-15"], ["content-type" : "application/x-www-form-urlencoded; charset=utf-8", "host" : service ~ ".amazonaws.com", "x-amz-date" : xamztime], cast(ubyte[])""); auto qParm = canonicalQueryString(r.canonicalRequest.queryParameters); auto sigHead = canonicalHeaders(r.canonicalRequest.headers); auto sigStr = signableString(r); auto sigKey = signingKey(secKey, curDateStr, zone, service); auto signature = sign(sigKey, cast(ubyte[])sigStr).toHexString().toLower(); writeln(); writeln(qParm); writeln(); writeln(sigHead); writeln(); writeln(sigStr); writeln(); writeln(signature); writeln(); auto client = HTTP(); client.clearRequestHeaders; client.addRequestHeader("content-type:", "application/x-www-form-urlencoded; charset=utf-8"); client.addRequestHeader("host:", service ~ ".amazonaws.com"); client.addRequestHeader("x-amz-date:", xamztime); client.addRequestHeader("Authoryzation:", "AWS4-HMAC-SHA256" ~ " " ~ "Credential=" ~ accKey ~ "/" ~ xamztime ~ "/" ~ zone ~ "/" ~ service ~ "/" ~ "aws4_request" ~ ", " ~ "SignedHeaders=" ~ "content-type;host;x-amz-date" ~ ", " ~ "Signature=" ~ signature); auto url = service ~ ".amazonaws.com?" ~ "Action=DescribeInstances=2013-10-15"; writeln(url); auto content = get(url, client); writeln(content); } Everything is compiling but im getting 400 (Bad Request): [root@ultraxps aws]# ./header.d action=DescribeRegions=2013-10-15 content-type: application/x-www-form-urlencoded; charset=utf-8 host: ec2.amazonaws.com x-amz-date: 20151009T053800Z AWS4-HMAC-SHA256 20151009T053800Z 20151009/us-east-1/ec2/aws4_request 888595748692147ceafafcae3941ec0d83ac42c97641e4d954d7447a00c56270 69b1e4c5212cc6b485569fdfb43f7dde94413b36c50393c55d4810ced47f167b ec2.amazonaws.com?Action=DescribeRegions=2013-10-15 std.net.curl.CurlException@/usr/include/dlang/dmd/std/net/curl.d(824): HTTP request returned status code 400 (Bad Request) /tmp/.rdmd-0/rdmd-header.d-54C0A2BD6BD71C27D9AC7319D786A6F3/header(pure @safe bool std.exception.enforce!(std.net.curl.CurlException, bool).enforce(bool, lazy const(char)[], immutable(char)[], ulong)+0x65) [0x5004bd] /tmp/.rdmd-0/rdmd-header.d-54C0A2BD6BD71C27D9AC7319D786A6F3/header(char[] std.net.curl._basicHTTP!(char)._basicHTTP(const(char)[], const(void)[], std.net.curl.HTTP)+0x1a2) [0x4fc0ba] /tmp/.rdmd-0/rdmd-header.d-54C0A2BD6BD71C27D9AC7319D786A6F3/header(char[] std.net.curl.get!(std.net.curl.HTTP, char).get(const(char)[], std.net.curl.HTTP)+0x6a) [0x4fbec2] /tmp/.rdmd-0/rdmd-header.d-54C0A2BD6BD71C27D9AC7319D786A6F3/header(_Dmain+0x840) [0x4f8f98] /tmp/.rdmd-0/rdmd-header.d-54C0A2BD6BD71C27D9AC7319D786A6F3/header(_D2rt6dmain211_d_run_mainUiPPaPUAAaZiZ6runAllMFZ9__lambda1MFZv+0x1f) [0x533eab] /tmp/.rdmd-0/rdmd-header.d-54C0A2BD6BD71C27D9AC7319D786A6F3/header(void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).tryExec(scope void delegate())+0x2a) [0x533e06] /tmp/.rdmd-0/rdmd-header.d-54C0A2BD6BD71C27D9AC7319D786A6F3/header(void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).runAll()+0x2b) [0x533e67] /tmp/.rdmd-0/rdmd-header.d-54C0A2BD6BD71C27D9AC7319D786A6F3/header(void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).tryExec(scope void delegate())+0x2a) [0x533e06] /tmp/.rdmd-0/rdmd-header.d-54C0A2BD6BD71C27D9AC7319D786A6F3/header(_d_run_main+0x1d2) [0x533d86] /tmp/.rdmd-0/rdmd-header.d-54C0A2BD6BD71C27D9AC7319D786A6F3/header(main+0x12) [0x52cb62]
Re: AWS API Dlang, hmac sha256 function.
On Friday, 9 October 2015 at 04:04:57 UTC, holo wrote: r.dateString = client.addRequestHeader("Authoryzation:", "AWS4-HMAC-SHA256" ~ " " ~ "Credential=" ~ accKey ~ "/" ~ xamztime ~ "/" ~ zone ~ "/" ~ service ~ "/" ~ "aws4_request" ~ ", " ~ "SignedHeaders=" ~ "content-type;host;x-amz-date" ~ ", " ~ "Signature=" ~ signature); authorisation ??? (Or maybe authorization) ie a typo (check other fields carefully too, just in case) auto url = service ~ ".amazonaws.com?" ~ "Action=DescribeInstances=2013-10-15"; writeln(url); auto content = get(url, client); writeln(content); } Everything is compiling but im getting 400 (Bad Request): [root@ultraxps aws]# ./header.d action=DescribeRegions=2013-10-15 content-type: application/x-www-form-urlencoded; charset=utf-8 host: ec2.amazonaws.com x-amz-date: 20151009T053800Z AWS4-HMAC-SHA256 20151009T053800Z 20151009/us-east-1/ec2/aws4_request 888595748692147ceafafcae3941ec0d83ac42c97641e4d954d7447a00c56270 69b1e4c5212cc6b485569fdfb43f7dde94413b36c50393c55d4810ced47f167b ec2.amazonaws.com?Action=DescribeRegions=2013-10-15 std.net.curl.CurlException@/usr/include/dlang/dmd/std/net/curl.d(824): HTTP request returned status code 400 (Bad Request) /tmp/.rdmd-0/rdmd-header.d-54C0A2BD6BD71C27D9AC7319D786A6F3/header(pure @safe bool std.exception.enforce!(std.net.curl.CurlException, bool).enforce(bool, lazy const(char)[], immutable(char)[], ulong)+0x65) [0x5004bd] /tmp/.rdmd-0/rdmd-header.d-54C0A2BD6BD71C27D9AC7319D786A6F3/header(char[] std.net.curl._basicHTTP!(char)._basicHTTP(const(char)[], const(void)[], std.net.curl.HTTP)+0x1a2) [0x4fc0ba] /tmp/.rdmd-0/rdmd-header.d-54C0A2BD6BD71C27D9AC7319D786A6F3/header(char[] std.net.curl.get!(std.net.curl.HTTP, char).get(const(char)[], std.net.curl.HTTP)+0x6a) [0x4fbec2] /tmp/.rdmd-0/rdmd-header.d-54C0A2BD6BD71C27D9AC7319D786A6F3/header(_Dmain+0x840) [0x4f8f98] /tmp/.rdmd-0/rdmd-header.d-54C0A2BD6BD71C27D9AC7319D786A6F3/header(_D2rt6dmain211_d_run_mainUiPPaPUAAaZiZ6runAllMFZ9__lambda1MFZv+0x1f) [0x533eab] /tmp/.rdmd-0/rdmd-header.d-54C0A2BD6BD71C27D9AC7319D786A6F3/header(void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).tryExec(scope void delegate())+0x2a) [0x533e06] /tmp/.rdmd-0/rdmd-header.d-54C0A2BD6BD71C27D9AC7319D786A6F3/header(void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).runAll()+0x2b) [0x533e67] /tmp/.rdmd-0/rdmd-header.d-54C0A2BD6BD71C27D9AC7319D786A6F3/header(void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).tryExec(scope void delegate())+0x2a) [0x533e06] /tmp/.rdmd-0/rdmd-header.d-54C0A2BD6BD71C27D9AC7319D786A6F3/header(_d_run_main+0x1d2) [0x533d86] /tmp/.rdmd-0/rdmd-header.d-54C0A2BD6BD71C27D9AC7319D786A6F3/header(main+0x12) [0x52cb62] /usr/lib/libc.so.6(__libc_start_main+0xf0) [0x7f9c39f71610] [root@ultraxps aws]# What am i doing wrong? What is strange when when i do same with curl its responding normally of course is not authenticated). [root@ultraxps aws]# curl ec2.amazonaws.com?Action=DescribeInstances=2013-10-15 [1] 20390 [root@ultraxps aws]# MissingParameterThe request must contain the parameter AWSAccessKeyIde1352781-c2b4-4e74-ade3-80d655efd0ac
Re: Bug? 0 is less than -10
On Thursday, 8 October 2015 at 13:32:17 UTC, Steven Schveighoffer wrote: On 10/7/15 1:27 AM, Laeeth Isharc wrote: 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 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. ptrdiff_t is in the C spec, ssize_t is not. No reason to name all the types of snow here. A machine-word-sized signed integer is ptrdiff_t. -Steve Whatever - it's not that important to me. D isn't C, and has learnt from C's mistakes and infelicities. I love the beauty of C, but I'm glad I don't need to do my work in it. One reason why D is under appreciated is that these seemingly small differences make a big difference in practice, even if in theory they shouldn't. Initially I was quite offended by no loop aliasing. Until I saw how much time it saved me in avoiding silly mistakes. But really there are better things to worry about at the moment. Laeeth.