Re: (u)byte calling char overload instead of int

2018-09-01 Thread Peter Alexander via Digitalmars-d-learn
On Saturday, 1 September 2018 at 17:17:37 UTC, puffi wrote: Hi, Is it by design that when calling functions with either ubyte or byte variables the char overload is called instead of the int (or generic) one? It seems this is by design. "If two or more functions have the same match level, th

Re: Is this a good idea?

2018-09-01 Thread Peter Alexander via Digitalmars-d-learn
On Saturday, 1 September 2018 at 16:20:11 UTC, Dr.No wrote: why move flush to outside the synchronized block? flush should be thread safe. In general, yiu want as little code as possible to run under the lock. Not that important though. trying out this approach I found to be ok except in som

Re: Is this a good idea?

2018-08-30 Thread Peter Alexander via Digitalmars-d-learn
On Thursday, 30 August 2018 at 19:59:17 UTC, Dr.No wrote: I would to process the current block in parallel but priting need to be theread-safe so I'm using foreach(x; parallel(arr)) { auto a = f(x); auto res = g(a); synchronized { stdout.writeln(res); stdout.flush();

Re: Parallelizing factorial computation

2018-08-24 Thread Peter Alexander via Digitalmars-d-learn
On Friday, 24 August 2018 at 13:04:47 UTC, Uknown wrote: I was quite surprised by the fact that parallel ran so much slower than recursive and loop implementations. Does anyone know why? n = 100 is too small to see parallelism gains. Try n = 1 https://run.dlang.io/is/XDZTSd

Patterns to avoid GC with capturing closures?

2018-08-24 Thread Peter Alexander via Digitalmars-d-learn
Consider this code, which is used as an example only: auto scaleAll(int[] xs, int m) { return xs.map!(x => m * x); } As m is captured, the delegate for map will rightly allocate the closure in the GC heap. In C++, you would write the lambda to capture m by value, but this is not a facility

Re: Fun with floating point

2015-02-07 Thread Peter Alexander via Digitalmars-d-learn
On Saturday, 7 February 2015 at 21:33:51 UTC, Kenny wrote: The above code snippet works correctly when I use LDC compiler (it finds expected 'f' value and prints it to console). I'm wondering is it a bug in DMD? p.s. the final code used by both compilers: import std.stdio; import std.conv; i

Re: Fun with floating point

2015-02-07 Thread Peter Alexander via Digitalmars-d-learn
On Saturday, 7 February 2015 at 23:06:15 UTC, anonymous wrote: On Saturday, 7 February 2015 at 22:46:56 UTC, Ali Çehreli wrote: 1.0 is famously not representable exactly. 1.0 is representable exactly, though. I think he meant 0.1 :-)

Re: Shared and GC

2015-01-15 Thread Peter Alexander via Digitalmars-d-learn
On Thursday, 15 January 2015 at 17:05:32 UTC, Ola Fosheim Grøstad wrote: On Thursday, 15 January 2015 at 15:31:17 UTC, Peter Alexander wrote: On Thursday, 15 January 2015 at 15:24:55 UTC, Ola Fosheim Grøstad wrote: That would be nice, because then a precise garbage collector could choose

Re: Shared and GC

2015-01-15 Thread Peter Alexander via Digitalmars-d-learn
On Thursday, 15 January 2015 at 15:24:55 UTC, Ola Fosheim Grøstad wrote: I am trying to understand the idea behind "shared" typing fully. If I am only allowed to share objects with another thread if it is typed "shared", doesn't that imply that it should be allocated as shared too and only be

Re: Copy only frame pointer between objects of nested struct

2015-01-07 Thread Peter Alexander via Digitalmars-d-learn
On Tuesday, 6 January 2015 at 23:32:25 UTC, Artur Skawina via That shows a static struct, so I'm not sure it's the same problem. static structs with template alias parameters to local symbols count as nested structs. Your solution would likely work, but yes, I'm looking for something less h

Copy only frame pointer between objects of nested struct

2015-01-06 Thread Peter Alexander via Digitalmars-d-learn
Consider: auto foo(T)(T a) { T b; // Error: cannot access frame pointer of main.X b.data[] = 1; return b; } void main() { struct X { this(int) {} int[4096] data; } foo(X()); } Note the error is because you c

Re: What exactly shared means?

2015-01-02 Thread Peter Alexander via Digitalmars-d-learn
On Friday, 2 January 2015 at 23:51:05 UTC, John Colvin wrote: The rule (in C(++) at least) is that all data is assumed to be visible and mutable from multiple other threads unless proved otherwise. However, given that you do not write a race, the compiler will provide full sequential consistenc

Re: Initialization of nested struct fields

2015-01-01 Thread Peter Alexander via Digitalmars-d-learn
On Friday, 2 January 2015 at 00:08:02 UTC, anonymous wrote: Apparently dmd thinks that the result of f must be a nested struct. I.e. it needs a context pointer. And I guess hell would break loose if you'd use a nested struct with a null context pointer. At least when the context pointer is actu

Initialization of nested struct fields

2015-01-01 Thread Peter Alexander via Digitalmars-d-learn
Can someone please explain this behaviour? I find it totally bizarre. auto f(T)(T x) { struct S { T y; this(int) { } } return S(0); } void main() { f(f(0)); } Error: constructor f376.f!(S).f.S.this field y must be initialized in

Re: Can the order in associative array change when keys are not midified?

2015-01-01 Thread Peter Alexander via Digitalmars-d-learn
On Thursday, 1 January 2015 at 13:13:10 UTC, Andrej Mitrovic via Digitalmars-d-learn wrote: On 1/1/15, Idan Arye via Digitalmars-d-learn wrote: If I have an associative array and I only modify it's values, without changing the keys, can I assume that the order won't change? Associative arrays

Re: readln with buffer fails

2014-10-29 Thread Peter Alexander via Digitalmars-d-learn
You need to take a slice of the buffer: char[] buf = Input[]; readln(buf); // line now in buf The reason for this is because you need to know where the string ends. If you just passed in Input, how would you know how long the line read was?

Re: Reflections on isPalindrome

2014-10-24 Thread Peter Alexander via Digitalmars-d-learn
On Friday, 24 October 2014 at 21:56:20 UTC, Nordlöw wrote: bool isPalindrome(R)(in R range) @safe pure Aside: for templates, just let the compiler infer @safe and pure. You don't know whether the range operations on R are pure or not. As for the actual algorithm, there's no need for the rand

Re: Are there desktop appications being developed in D currently?

2014-08-09 Thread Peter Alexander via Digitalmars-d-learn
On Saturday, 9 August 2014 at 00:34:43 UTC, Puming wrote: Yes, rust is a more infantile language compared to D, but people are already using them to create complicate applications like browser! Rust was designed to build Servo. The people building Servo are the people building Rust. With all

Re: spawnProcess command-line arguments help

2014-08-04 Thread Peter Alexander via Digitalmars-d-learn
On Sunday, 3 August 2014 at 23:48:09 UTC, Martin wrote: When I use the spawnProcess function in std.process, the command line arguments that I provide to the function seem to get "quoted". I can't reproduce this on OS X with 2.066rc1 (args are unquoted). Can someone else check Windows? Sounds

Re: How to test templates for equality?

2014-06-30 Thread Peter Alexander via Digitalmars-d-learn
template Foo(T...) {} template Bar(T...) {} template isFoo(alias F) { enum isFoo = __traits(isSame, F, Foo); } pragma(msg, isFoo!Foo); // true pragma(msg, isFoo!Bar); // false

Re: Compiler support for T(n) notation for initialization of variables

2014-06-07 Thread Peter Alexander via Digitalmars-d-learn
Well, it doesn't work in 2.065, so must be 2.066 :-) P.S. thanks for letting me know about this feature. I had no idea it was going in!

Re: Why function template 'among' is of complexity O(1) and not O(n)?

2014-02-19 Thread Peter Alexander
On Wednesday, 19 February 2014 at 09:46:04 UTC, Gopan wrote: On Wednesday, 19 February 2014 at 09:09:25 UTC, Tobias Pankrath It's O(k) with k = vals.length, which is the number of arguments given to the function, which is a compile time constant and not dependent on any input of the final progr

Re: std.algorithm.splitter improovement?

2013-12-14 Thread Peter Alexander
On Saturday, 14 December 2013 at 16:00:06 UTC, seany wrote: the std.algorithm.splitter returns a blank or null (eg a null string "") between two consecuting delimeters. for example, splitting "hello world" (two spaces between words) will return ["hello" , "", "world"] is there an improoved

Re: join of range of ranges?

2013-09-22 Thread Peter Alexander
On Sunday, 22 September 2013 at 18:13:39 UTC, Peter Alexander wrote: On Sunday, 22 September 2013 at 14:26:14 UTC, bearophile wrote: auto r2 = [1, 2] .map!(x => [1, 2].map!(y => '*')) .join("_"); The problem is that you are trying to map

Re: join of range of ranges?

2013-09-22 Thread Peter Alexander
On Sunday, 22 September 2013 at 14:26:14 UTC, bearophile wrote: auto r2 = [1, 2] .map!(x => [1, 2].map!(y => '*')) .join("_"); The problem is that you are trying to map a range of range of chars with a range of dchars. auto r2 = [1, 2] .map!(x =>

Re: Switch and break

2012-01-19 Thread Peter Alexander
On 19/01/12 9:55 PM, RenatoL wrote: Just curious: why in D we are not obligated to use break in every branch of a swicth structure? That is: switch (i) { case 1: writeln("You wrote 1"); case 2: writeln("You wrote 2");

Re: Get name of enum val at compile-time?

2012-01-15 Thread Peter Alexander
On 15/01/12 10:29 PM, Philippe Sigaud wrote: On Sun, Jan 15, 2012 at 22:19, Timon Gehr wrote: Nick: Goddamnnit, what the fuck is wrong with me? Yes that works :) I suspect a better error message would have prevented this. DMD still has some potential of improvement in that area. =) In that

Re: Fixed matrix rows joining

2012-01-15 Thread Peter Alexander
On 15/01/12 2:19 AM, Andrej Mitrovic wrote: I guess join() could be specialized for static arrays and then just do a dup and a cast? Would that work ok? There should be no need to allocate extra memory to do this.

Re: A tutorial on D templates

2012-01-13 Thread Peter Alexander
On 13/01/12 10:48 PM, DNewbie wrote: I can't understand it. Why would someone need template programming. What problem does template solve? Suppose you want to write a function to get the minimum of two integers. It's easy: int min(int a, int b) { return a < b ? a : b; } Suppose then yo

Re: Is this really a bug?

2012-01-06 Thread Peter Alexander
On 7/01/12 1:19 AM, Daniel wrote: Hi, I've read on Bugzilla Issue 6398 that this is a bug: static int value; ref foo(){ printf("getter\n"); return value; } ref foo(int x){ printf("setter\n"); value = x; return value; } void main(){ foo = 1; } // Should print "setter", but print "getter" in 2.0

Re: An issue with lazy delegates

2012-01-06 Thread Peter Alexander
On 5/01/12 5:26 AM, Andrej Mitrovic wrote: The first call doesn't do anything because the delegate is wrapped inside of another delegate. I want this template to be versatile enough to be used by both lazy expressions and delegate literals, but I don't know how. void test(T)(lazy T dg) { st

Re: Cartesian product of ranges?

2012-01-01 Thread Peter Alexander
On 14/12/11 9:21 PM, Timon Gehr wrote: On 12/14/2011 09:14 PM, Justin Whear wrote: I've looked through std.algorithm and std.range, but haven't found anything to compute the Cartesian product of several ranges. I have the nagging feeling that this can be accomplished by combining several of the

Re: auto

2011-11-26 Thread Peter Alexander
On 24/11/11 10:38 PM, Andrej Mitrovic wrote: But I bet you would waste more memory at compile-time if you had to type a long template instance name instead of using auto. We're talkin' bytes here! Actually, using `auto` should be faster because the compiler doesn't need to do any name lookup.

Aligning data in memory / on the stack

2011-09-16 Thread Peter Alexander
Can anyone help me with this? http://stackoverflow.com/questions/7375165/aligning-stack-variables-in-d --- Is there a way to align data on the stack? In particular, I want to create an 16-byte aligned array of floats to load into XMM registers using movaps, which is significantly faster than

Re: NaCl stable ABI

2011-08-03 Thread Peter Alexander
On 2/08/11 2:24 AM, Adam Ruppe wrote: From what I can tell, it's Google's alternative to Flash; they want to make crappy games on it. Consider that the first thing they ported to it, again, just like their javascript nonsense, was Quake. (I think Google loves Javascript too much to let it go an

Linker errors on OSX

2011-06-19 Thread Peter Alexander
I've been having strange linker errors recently and I have no ideas why they've started happening. When compiling/linking: import std.stdio; void main() { writeln("Hello"); } with DMD 2.053 I get linker error: Undefined symbols: "_D3std9exception7bailOutFAyaixAaZv", referenced from: _

Running DMD tests

2011-06-13 Thread Peter Alexander
I'm trying to run the test suite for DMD, but I'm running into issues. I've cloned dmd from github, and successfully built dmd, but when I run 'make' from the dmd/test dir, I get: $ make Creating output directory: test_results Building d_do_test tool object.d: Error: module object is i

Re: "not an lvalue"

2011-05-01 Thread Peter Alexander
On 1/05/11 2:53 PM, Dmitry Olshansky wrote: Ehm.. Well, first things first: you shouldn't use classes for lightweight & plain data things like vectors. There are structs for that. In general, structs are value-like objects living on the stack while classes are reference-like objects living on the

Re: "not an lvalue"

2011-05-01 Thread Peter Alexander
On 1/05/11 2:30 PM, CrypticMetaphor wrote: Hi, I've been away from D for a while, but now I'm back and I'm stuck with an compile time error. I've got a Matrix33 class and a Vector3 class, but something is wrong with the way I return my Vector3 in my matrix class: If I do this I get an error: M

Re: Should writef try to statically check for format specifiers?

2011-04-26 Thread Peter Alexander
On 11/04/11 9:35 PM, Andrej Mitrovic wrote: I realize runtime checks would be out of the question... debug doRuntimeCheck(); :-)

Re: Ranges

2011-03-18 Thread Peter Alexander
On 18/03/11 5:53 PM, Jonathan M Davis wrote: On Friday, March 18, 2011 03:32:35 spir wrote: On 03/18/2011 10:29 AM, Peter Alexander wrote: On 13/03/11 12:05 AM, Jonathan M Davis wrote: So, when you're using a range of char[] or wchar[], you're really using a range of dchar. These

Re: Ranges

2011-03-18 Thread Peter Alexander
On 13/03/11 12:05 AM, Jonathan M Davis wrote: So, when you're using a range of char[] or wchar[], you're really using a range of dchar. These ranges are bi-directional. They can't be sliced, and they can't be indexed (since doing so would likely be invalid). This generally works very well. It's e

Re: Double-dispatch

2011-02-16 Thread Peter Alexander
On 13/02/11 9:32 PM, Sean Eskapp wrote: == Quote from bearophile (bearophileh...@lycos.com)'s article Sean Eskapp: Is there a nicer way to do this in D, or am I stuck with the same thing? Andrei has recently said no one needs double dispatch (in D) :-) So Andrei will be interested in your use

Re: Starting with D

2011-02-07 Thread Peter Alexander
On 6/02/11 11:35 PM, Julius wrote: Hi there, i'm all new to D but not new to programming in general. I'd like to try D but i didn't find a nice tutorial yet. I don't want to read a whole book, I just want to get the basics so I can start. Can you help me find something like that? Best regards, J

Setting thread priority

2011-02-05 Thread Peter Alexander
How do you set the priority of a thread, or otherwise control how much CPU time it gets? It appears that std.thread had an answer for this, but it has been removed from Phobos by the looks of things. On a side note, why is std.thread still in the online documentation if it was removed long a

Re: Asynchronous concurrency with reference types

2011-02-05 Thread Peter Alexander
On 4/02/11 11:44 PM, Sean Kelly wrote: Peter Alexander Wrote: How would you do it with message passing though? As I understand, all of the std.concurrency message passing routines are blocking, and I need this to be asynchronous. What do you mean by blocking? The receive call will block

Re: Asynchronous concurrency with reference types

2011-02-05 Thread Peter Alexander
On 5/02/11 12:11 AM, Sean Kelly wrote: Peter Alexander Wrote: Things might be easier if the error messages associated with D's concurrent features weren't especially unhelpful (for example, trying to spawn a thread with reference type parameters just gives you a 'no match for

Re: Asynchronous concurrency with reference types

2011-02-04 Thread Peter Alexander
On 4/02/11 8:42 PM, spir wrote: On 02/04/2011 07:18 PM, Peter Alexander wrote: I would like to be able to spawn a thread, which does a lot of work, and then returns (in some way) the result of its computations to the main thread. I would really like to avoid copying its results if possible

Re: Asynchronous concurrency with reference types

2011-02-04 Thread Peter Alexander
On 4/02/11 8:23 PM, Jesse Phillips wrote: Peter Alexander Wrote: Essentially, the work that doWork does needs to be returned to the main thread asynchronously, and obviously in a thread-safe manner. What's the best way to do this? The above won't work because ~= isn't atomic,

Asynchronous concurrency with reference types

2011-02-04 Thread Peter Alexander
I would like to be able to spawn a thread, which does a lot of work, and then returns (in some way) the result of its computations to the main thread. I would really like to avoid copying its results if possible. Logically, what I would like is something like this: class LotsOfData { ... } v

Re: Development blog? D news?

2011-01-17 Thread Peter Alexander
On 17/01/11 8:16 AM, bioinfornatics wrote: will be great if we have a planet. An example here : http://planet.fedoraproject.org/ A planet it is a web page who feed several blog, user can use a cms for to write a blog like Dotclear, wordpress ... http://planet.dsource.org/ Problem is: it's no

Development blog? D news?

2011-01-17 Thread Peter Alexander
Take a look at the website (either digitalmars, or d-programming-language). There is absolutely no indication on those sites that the D language is actually going anywhere. Other than version numbers on the downloads, nothing changes. Outsiders have no idea what's going on. Here's a suggestio

Re: Initialising arrays at compile time

2011-01-02 Thread Peter Alexander
On 2/01/11 2:16 PM, bearophile wrote: Is this good enough? struct Vec { double[2] e; static enum Vec zero = Vec([0.0, 0.0]); this(real x, real y) { e[0] = x; e[1] = y; } } Well it works, so yes :-) That's quite irritating. Why does the automatically gen

Initialising arrays at compile time

2011-01-02 Thread Peter Alexander
Ok, someone put me out of my misery, I can't figure out for the life of me how to do this. Basically, I have a vector class and want enums for the zero vectors: struct Vec { this(real x, real y) { e[0] = x; e[1] = y; } real[2] e; enum Vec zero = Vec(0, 0); } What can I do? The above doe

Re: Understanding isInfinite(Range)

2010-09-04 Thread Peter Alexander
== Quote from Steven Schveighoffer (schvei...@yahoo.com)'s article > On Fri, 03 Sep 2010 11:12:29 -0400, Andrej Mitrovic > wrote: > > What does char[1 + Range.empty] do? It looks rather cryptic.. > char[1+Range.empty] is a type. If Range.empty is a compile-time constant, > then this type is valid

Re: Is "is" the same as ptr == ptr for arrays?

2010-08-07 Thread Peter Alexander
On 7/08/10 4:33 PM, simendsjo wrote: Is the following equalent? int[] a; int[] b = a; assert(a is b); assert(a.ptr == b.ptr); No. (a is b) implies (a.ptr == b.ptr) but (a.ptr == b.ptr) does not imply (a is b) For example: int[] a = [1, 2, 3]; int[] b = a[0..1]; Here, a.ptr == b.ptr, but

Re: Default constructor for structs

2010-08-07 Thread Peter Alexander
On 7/08/10 3:44 PM, bearophile wrote: Peter Alexander: Bye, bearophile Thanks for your detailed reply. However, I'm not particularly satisfied with any of those :( I have already gone with the single parameter init constructor in my code until I find something better, but really, I

Default constructor for structs

2010-08-07 Thread Peter Alexander
Since default constructors for structs are not allowed, how do I go about making all the elements of this Matrix struct default to zero? (floats default to nan by default). struct Matrix(int M, int N) { float[M][N] elements; } Matrix(3, 3) m; assert(m[0][0] == 0); ?

Re: Partial template function specialization

2009-08-20 Thread Peter Alexander
Lars T. Kyllingstad Wrote: > Disclaimer: You didn't say whether you use D1 or D2, but I use D2, so > I'll give my answer in D2 code. It is highly likely it will also work in D1. I'm using neither :) I'm just considering learning at the moment. > First of all, I don't know how it is in C++, bu

Re: Partial template function specialization

2009-08-20 Thread Peter Alexander
Simen Kjaeraas Wrote: > > T foo( T, U )( U value ) {...} > > template fooPartial( T ) { >alias foo!( T, int ) fooPartial; > } > > This may or may not have been what you want. Not quite sure what's going on there, but it doesn't look like what I want. I want a general function: T foo(T, U)

Partial template function specialization

2009-08-19 Thread Peter Alexander
Hey all, I'm considering learning D at the moment, and one thing that has bothered me in C++ is the lack of partial template function specializations. For example, you can create something like this: template const Field norm(const V& v); and you can specialize it: template <> const double n