Re: A few notes on choosing between Go and D for a quick project

2015-03-13 Thread bearophile via Digitalmars-d
Walter Bright: On 3/13/2015 3:34 AM, bearophile wrote: Strict mode is a D2 with immutable+@safe+pure by default, Thank you Walter for giving me an actual answer :-) Note that you can get this largely by starting a module with the following: immutable @safe pure: immutable in my

Re: A few notes on choosing between Go and D for a quick project

2015-03-13 Thread bearophile via Digitalmars-d
Ola Fosheim Grøstad: Yes, but what is a strict mode. I agree with most of what is said about D and Go in the top post, but is there a market for yet another high level language that isn't high level enough? Strict mode is a D2 with immutable+@safe+pure by default, something like a var

Re: A few notes on choosing between Go and D for a quick project

2015-03-13 Thread bearophile via Digitalmars-d
Andrei Alexandrescu: That's a rather random collection - strict seems to be D without the stuff bearophile dislikes. -- Andrei I am OK with that definition. Is that your best critique to those suggestions? :-) Bye, bearophile

Compilation changes

2015-03-13 Thread bearophile via Digitalmars-d-learn
Time ago Dmitry Olshansky gave me a nice script to compile dmd+Phobos from github, starting like this: My recipe on Windows, by Dmitry Olshansky: 0. Make sure there are no other DMD in the Windows path. 1. Get the latest DMD release zip you can find, unzip to some drive root(! otherwise get's

Re: A few notes on choosing between Go and D for a quick project

2015-03-13 Thread bearophile via Digitalmars-d
Florin Mihaila: This note is getting too long, so if some of what I wrote needs more clarification, feel free to ask. I'm glad my humble notes are sparking discussion, and I hope D continues to grow. Constructive notes (as yours) are always welcome in this friendly community. Thank you for

Re: A few notes on choosing between Go and D for a quick project

2015-03-12 Thread bearophile via Digitalmars-d
Andrei Alexandrescu: ## some or all of @safe, immutable, pure should be the default +1 I have a ER on @safe by default and Walter seems to agree. But I'd like more. A strict D mode? :-) # libraries, projects should be prominently listed and nurtured # single-idea advantage; D seems to

Re: Int to float?

2015-03-06 Thread bearophile via Digitalmars-d-learn
Ola Fosheim Grøstad: D claims to follow C, so using unions for type punning is ultimately implementation defined. I am not sure if D is the same as C regarding this. Bye, bearophile

Re: Parallel Merge Sort

2015-03-04 Thread bearophile via Digitalmars-d
Josh: swap(A, B) what exactly is this doing? You should learn to answer your own questions in many cases. Take a look at the source code of Phobos, you will see the swap() implementation inside std.algorithm. It swaps the content of two values. Here the values are the structs that

Re: Parallel Merge Sort

2015-03-04 Thread bearophile via Digitalmars-d
Ali Çehreli: I think there is bug in the algorithm: auto a = [90, 50, 33, 72, 35]; a.mergeSort; assert(a == [33, 50, 72, 90, 35]);// Incorrect ordering :( I translated that D code from C code of Wikipedia in few minutes, the probability of mistakes is high. Better to use

Re: RCArray is unsafe

2015-03-04 Thread bearophile via Digitalmars-d
Walter Bright: The complexity of a free list doesn't remotely compare to that of adding an ownership system. A sound complete ownership system is the only good enough solution for D. That's my opinion. Bye, bearophile

Re: is expression and type tuples

2015-03-04 Thread bearophile via Digitalmars-d-learn
Jack Applegame: On Tuesday, 3 March 2015 at 17:49:24 UTC, bearophile wrote: That's 1 + n-1 :-) Could you please explain what does '1 + n-1' mean? This is your code: template Is(ARGS...) if(ARGS.length % 2 == 0) { enum N = ARGS.length/2; static if(N == 1) enum Is = is(ARGS[0] :

Re: RCArray is unsafe

2015-03-04 Thread bearophile via Digitalmars-d
Walter Bright: How do you type an an array of pointers with different owners? Sound doesn't mean it should be able to do everything. It will be just an approximated model. It means it's going to forbid some valid code, just like every type system. You use some @system code to work around

[rosettacode] std.container.DList problems

2015-03-04 Thread bearophile via Digitalmars-d-learn
This code used to work in DMD 2.065: http://rosettacode.org/wiki/Strand_sort#D import std.stdio, std.container; DList!T strandSort(T)(DList!T list) { static DList!T merge(DList!T left, DList!T right) { DList!T result; while (!left.empty !right.empty) { if

Re: Implicit fall through not detected (Example from lex.html)

2015-03-03 Thread bearophile via Digitalmars-d-learn
Andre: I am also not really happy with the actual behavor (w / wi switch needed) You shall always compile your D code with warnings active, unless you need them disabled for some real reason. Eventually the fall through warning will become a deprecation and then an error. It's meant to be

Re: is expression and type tuples

2015-03-03 Thread bearophile via Digitalmars-d-learn
Jack Applegame: Seems like is expression doesn't support type tuples: pragma(msg, is(short : int)); // true enum Test(ARGS...) = is(ARGS[0..2] : ARGS[2..4]); pragma(msg, is(Test!(int, int, int, int))); // false pragma(msg, Test!(int, short, int, int)); // false Is it

Re: is expression and type tuples

2015-03-03 Thread bearophile via Digitalmars-d-learn
Jack Applegame: or use recursion (with splitting in two, and not 1 + n-1). Bye, bearophile I already have one: template Is(ARGS...) if(ARGS.length % 2 == 0) { enum N = ARGS.length/2; static if(N == 1) enum Is = is(ARGS[0] : ARGS[1]); else enum Is = is(ARGS[0] : ARGS[N])

Re: Parallel Merge Sort

2015-03-03 Thread bearophile via Digitalmars-d
Josh wrote: How can I make my parallel code more efficient. Currently, I am getting destroyed by the serial merge sort. http://pastebin.com/M0GKfTTX That serial merge sort I've written is little more than a toy. I suggest you to compare your parallel sort with a serial sort that allocates

Re: Contradictory justification for status quo

2015-03-01 Thread bearophile via Digitalmars-d
Walter Bright: Actually, Kenji fearlessly deals with some of the hardest bugs in the compiler that require a deep understanding of how the compiler works and how it is supposed to work. He rarely does trivia. I regard Kenji's contributions as invaluable to the community. But my point was

Re: DIP74: Reference Counted Class Objects

2015-03-01 Thread bearophile via Digitalmars-d
Jacob Carlborg: @arc class Foo { T1 opAddRef(); T2 opRelease(); } ... Alternative A gives a clear documentation it's a reference counted class without having to scan the methods. Assuming you want something like DIP74, this design design seems safer than the design proposed in

Re: Contradictory justification for status quo

2015-02-28 Thread bearophile via Digitalmars-d
Andrei Alexandrescu: I'm following with interest the discussion My Reference Safety System (DIP???). Right now it looks like a lot of work - a long opener, subsequent refinements, good discussion. It also seems just that - there's work but there's no edge to it yet; right now a DIP along

Re: Contradictory justification for status quo

2015-02-28 Thread bearophile via Digitalmars-d
Zach the Mystic: You can see exactly how D works by looking at how Kenji spends his time. For a while he's only been fixing ICEs and other little bugs which he knows for certain will be accepted. I agree that probably there are often better ways to use Kenji time for the development of D.

Re: Contradictory justification for status quo

2015-02-27 Thread bearophile via Digitalmars-d
Andrei Alexandrescu: Safety is good to have, and the simple litmus test is if you slap @safe: at the top of all modules and you use no @trusted (or of course use it correctly), you should have memory safety, guaranteed. I have suggested to switch to @safe by default:

Re: What am I doing wrong here - canFind with iota is not working

2015-02-27 Thread bearophile via Digitalmars-d
immutable float item = 0.174531f; r.canFind!q{ feqrel(cast()a, cast()b) = 21 }(item).writeln; } With a recent Phobos bug fix you can now write: r.canFind!q{ feqrel(a, b) = 21 }(item).writeln; Bye, bearophile

Re: What am I doing wrong here - canFind with iota is not working

2015-02-26 Thread bearophile via Digitalmars-d
Laeeth Isharc: Ie I think people diagnosed the problem, but what is the solution... A possible solution: void main() @safe { import std.stdio, std.range, std.algorithm, std.math; immutable float oneDegree = (PI / 180.0f); immutable float first = -(oneDegree * 10.0f);

Re: DIP74: Reference Counted Class Objects

2015-02-26 Thread bearophile via Digitalmars-d
Andrei Alexandrescu: The compiler detects automatically and treats specially all classes and interfaces that define the following two methods: Defining only one of those methods needs to give a compile-time error. This lowering assumes left-to-right evaluation of function parameters.

Re: Memory safety depends entirely on GC ?

2015-02-24 Thread bearophile via Digitalmars-d
Andrei Alexandrescu: using stack-allocated arrays in safe code is a rather niche use). I use stack-allocated arrays in supposedly safe code. And having a future D compiler that makes that kind of code actually safe is a good thing. Bye, bearophile

Re: Memory safety depends entirely on GC ?

2015-02-24 Thread bearophile via Digitalmars-d
Andrei Alexandrescu: It definitely is a good thing. The question is balancing its goodness against the costs of allowing it. It's not impossible you may get asked to change your code to use a struct instead of a fixed-sized array. I don't like the look of the annotations of DIP25. I'd like

Re: Let's Play Code Golf

2015-02-23 Thread bearophile via Digitalmars-d
Steve Sobel: It can get down to 155 using ranges, but those imports really are killer. You usually don't want to design a language for code golfing (but several exist, like http://esolangs.org/wiki/GolfScript ). void main(){import

Re: A Refcounted Array Type

2015-02-23 Thread bearophile via Digitalmars-d
Walter Bright: 5. bounds checking When you go past bounds of a built-in array you get an error located in the user code, while if you put a pre-condition in your Array struct to detect the same errors, you get a run-time error message located in that pre-condition instead. I'd like some

Re: DDMD just went green on all platforms for the first time

2015-02-22 Thread bearophile via Digitalmars-d
Benjamin Thaut: Are there any performance comparisons? E.g. how long das DDMD take to compile phobos vs regular DMD? Yes, there are several performance comparisons I'm curious about, like binary size, compilation speed, compilation speed of the compiler, max memory to compile Phobos without

Re: UFCS on template alias ?

2015-02-21 Thread bearophile via Digitalmars-d-learn
Baz: Is this a normal behaviour ? Try to move the definition of poly to module-level scope. This is a design decision to avoid other troubles. Bye, bearophile

Re: D : dmd vs gdc : which one to choose?

2015-02-19 Thread bearophile via Digitalmars-d-learn
Mayuresh Kathe: Should I choose DMD or go with GDC? It's a good idea to use all available compilers. LDC and DMD are both useful. Every one of them has advantages and disadvantages. Bye, bearophile

Re: let (x,y) = ...

2015-02-19 Thread bearophile via Digitalmars-d-announce
Kagamin: Doesn't let normally declare a new variable? You are right, yours is a valid point... So tie could be a better name after all. Bye, bearophile

Re: let (x,y) = ...

2015-02-19 Thread bearophile via Digitalmars-d-announce
Ola Fosheim Grøstad: Maybe change the name to tie: http://www.cplusplus.com/reference/tuple/tie/ ? I prefer let, it's much more traditional and descriptive. C++ standard library is often a bad example to follow... Bye, bearophile

Re: let (x,y) = ...

2015-02-19 Thread bearophile via Digitalmars-d-announce
Mengu: that's a great example to show d's strength. thank you. It's also a great way to show what's missing in D syntax. Bye, bearophile

Re: let (x,y) = ...

2015-02-19 Thread bearophile via Digitalmars-d-announce
Kagamin: Or even more obvious (VBA,TSQL): set (x,y,z) = [1,2,3]; I prefer to use set as in Python, to define sets: s = set([1, 2, 3]) 2 in s True Bye, bearophile

Re: Mimicking C++'s indexing behavior in D associative arrays

2015-02-18 Thread bearophile via Digitalmars-d-learn
Rikki Cattermole: Foo*[string] bar; Foo v = *bar.grab(mykey); Is this the setdefault of Python dicts? If this need is strong a new function could be added to Phobos (or even druntime if you want to reduce the number of hash computations). Bye, bearophile

Re: contiguous ranges

2015-02-17 Thread bearophile via Digitalmars-d
Andrei Alexandrescu: for an array r, is r.retro contiguous or not? Is the most useful contiguous range forward? So can you name it ForwardContiguousRange? Bye, bearophile

Re: Consistency

2015-02-16 Thread bearophile via Digitalmars-d
John Colvin: I'm quite a fan of python's // operator for integer division, especially when combined with python 3's choice to make / always mean floating point division (i.e. 3/2 == float(3)/2, 3//2 == 1). It recognises that integer division is a weird thing and separates it from the much

Re: Consistency

2015-02-16 Thread bearophile via Digitalmars-d
Daniel Murphy: No, they were still O(n) worst case, for a single bucket with a degenerate binary tree. I see. I was unable to hit this degenerate case in my testing code, but I guess that was possible. Thank you. Bye, bearophile

Re: what is the offical way to handle multiple list in map() ?

2015-02-16 Thread bearophile via Digitalmars-d-learn
Baz: is this the official way ? It seems a way to perform nested mapping in D. --- auto fruits = [apple, banana, orange][]; auto vegies = [grass, salad][]; Those trailing [] are unneded. auto youreallygonna = map!( `map!(a = eat ~ a)(a)` )([fruits, vegies]); Better to use another

Re: contiguous ranges

2015-02-16 Thread bearophile via Digitalmars-d
Vlad Levenfeld: a ContiguousRange would be any RandomAccessRange which has a member called ptr which supports a dereferencing operator * that yields an ElementType!R. This notion is useful for functions which might otherwise perform an element-by-element transfer to an OutputRange via put,

Re: Consistency

2015-02-15 Thread bearophile via Digitalmars-d
John Colvin: tup1.expand.only.countUntil(2).writeln; Admittedly, it's a little longer than expected :) A little shorter: tup1[].only.countUntil(2).writeln; Bye, bearophile

Re: ranges reading garbage

2015-02-15 Thread bearophile via Digitalmars-d-learn
John Colvin: prints things like [0, 4, 5, 1, 1, 1459971595, 1459971596, 2, 2, 1459971596, 1459971597, 3, 4, 8, 9, 5, 5, 4441427819, 4441427820, 6, 6, 4441427820, 4441427821, 7] but the output isn't consistent, the big numbers change on each run. Try to replace the only() with: [y,

Re: Consistency

2015-02-15 Thread bearophile via Digitalmars-d
Xinok: In that respect, it only makes sense for dictionaries Values as in Python are much more useful for arrays :-) Meta: D is better off than Python in this case, I've never subscribed with point of view :-) Bye, bearophile

Re: Consistency

2015-02-15 Thread bearophile via Digitalmars-d
Meta: Oh, whoops. I mixed up average-case complexity with worst-case. Although, isn't lookup O(n) in the worst case for hash tables? D associative arrays used to be O(1) amortized and O(n ln n) in worst case. Now they are O(1) amortized and O(n) worst case. And for an adversary it's not too

Re: ranges reading garbage

2015-02-15 Thread bearophile via Digitalmars-d-learn
FG: Odd... Still something is wrong. It prints: [0, 4, 5, 1, 1, 5, 6, 2, 2, 6, 7, 3, 4, 8, 9, 5, 5, 5, 6, 6, 6, 6, 7, 7] instead of this: [0, 4, 5, 1, 1, 5, 6, 2, 2, 6, 7, 3, 4, 8, 9, 5, 5, 9, 10, 6, 6, 10, 11, 7] This is less lazy and gives another result: import std.range,

Re: Data-Flow (Escape) Analysis to Aid in Avoiding GC

2015-02-13 Thread bearophile via Digitalmars-d-learn
Per Nordlöw: Then how does the GC know when to release when there are multiple references? The mark phase counts what's reachable and what can't be reached. If an object has one pointer to it, or one hundred pointers, it is not removed. If nothing points to it, it is removed. I suggest

Re: Number of Bits Needed to Represent a Zero-Offset Integer

2015-02-13 Thread bearophile via Digitalmars-d-learn
H. S. Teoh: Maybe that could be the basis of a better name? Right. Bye, bearophile

Re: Number of Bits Needed to Represent a Zero-Offset Integer

2015-02-13 Thread bearophile via Digitalmars-d-learn
H. S. Teoh: So it could be called ilog2? Perhaps floorIlog2? Isn't ilog2 a different function? Bye, bearophile

Re: What is the Correct way to Malloc in @nogc section?

2015-02-13 Thread bearophile via Digitalmars-d-learn
Foo: I'm regret that I tried to help, I will delete this repo as far as possible. :) Language communities aren't perfect, but the success of a language comes from the help of many little hands :) Perhaps Rust will win over D in the end, but there's no reason to throw away your work just

Re: Number of Bits Needed to Represent a Zero-Offset Integer

2015-02-13 Thread bearophile via Digitalmars-d-learn
Dominikus Dittes Scherkl: I would recommend to use something like this: /// returns the number of the highest set bit +1 in the given value or 0 if no bit is set size_t bitlen(T)(const(T) a) pure @safe @nogc nothrow if(isUnsigned!T) { static if(T.sizeof = size_t.sizeof) // doesn't work

Re: Data-Flow (Escape) Analysis to Aid in Avoiding GC

2015-02-13 Thread bearophile via Digitalmars-d-learn
Tobias Pankrath: Why should splitter.front allocate? I think that front was able to throw Unicode exceptions, that require the GC. But I think later they have become asserts, that don't require the GC. Bye, bearophile

Re: This Week in D: Issue #4

2015-02-11 Thread bearophile via Digitalmars-d-announce
It it a good idea to keep a weekly rhythm? Perhaps one issue every two, three or even four weeks is better at this stage of the D life. Bye, bearophile

Re: How to write similar code D?

2015-02-10 Thread bearophile via Digitalmars-d-learn
FG: auto query = iota(2, 2 + 10) .map!(c = [Length: 2 * c, Height: c * c - 1, Hypotenuse: c * c + 1]) .map!(x = format(%4d%4d%4d, x[Height], Unlike other languages like JavaScript, the D front-end is very weak in optimizing well such kind of code... I think D compilers handle

Re: cannot deduce function from argument types issue.

2015-02-10 Thread bearophile via Digitalmars-d-learn
ted: Could someone enlighten me ? This works: import std.range: ElementType, isInputRange; ElementType!R testFunc(R, T)(R range, T foo) if (is(ElementType!R == T)) { static assert(isInputRange!R); typeof(return) retVal = foo ^^ 2; // More DRY. return retVal; } void main() {

Re: cannot deduce function from argument types issue.

2015-02-10 Thread bearophile via Digitalmars-d-learn
Ali Çehreli: If think it is a little too much to ask from the template system of D. I remember hitting a similar problem with code like this bar() function: // OK void foo(size_t N1, size_t N2)(int[N1] a, int[N2] b) if (N2 == N1 ^^ 2) {} // Not OK void bar(size_t N)(int[N] a, int[N ^ 2]

Re: cannot deduce function from argument types issue.

2015-02-10 Thread bearophile via Digitalmars-d-learn
void bar(size_t N)(int[N] a, int[N ^ 2] b) {} I meant: void bar(size_t N)(int[N] a, int[N ^^ 2] b) {}

Re: Classes and @disable this()

2015-02-10 Thread bearophile via Digitalmars-d-learn
I think this can be filed in Bugzilla as diagnostic enhancement: class Foo { @disable this(); this(int i) {} } void main() {} https://issues.dlang.org/show_bug.cgi?id=14163 Bye, bearophile

Re: cannot deduce function from argument types issue.

2015-02-10 Thread bearophile via Digitalmars-d-learn
ted: ... where you say 'More DRY' above, are you referring to I was referring to both, but mostly to the typeof. It's more DRY (http://en.wikipedia.org/wiki/Don%27t_repeat_yourself ). You are stating only once the type of the return variable. This is less bug-prone. Bye, bearophile

Re: To write such an expressive code D

2015-02-10 Thread bearophile via Digitalmars-d-learn
Dennis Ritchie: Please help. This starts to look like homework :-) Bye, bearophile

Re: Compilation with dub + dmd: out of memory

2015-02-10 Thread bearophile via Digitalmars-d-learn
Vlasov Roman: I have the quite computer with 2 GB RAM. At compilation with dub and dmd of small project this pair eating about 1.4~1.5 GB RAM. I solve this probleb by connecting swap partition, but it calls some freezes + it take ~10% of swap, and after compilation swap not released. At

Re: To write such an expressive code D

2015-02-10 Thread bearophile via Digitalmars-d-learn
Dennis Ritchie: Output: 0 xor 0 xor 0 = 0 0 xor 0 xor 1 = 1 0 xor 1 xor 0 = 1 0 xor 1 xor 1 = 0 1 xor 0 xor 0 = 1 1 xor 0 xor 1 = 0 1 xor 1 xor 0 = 0 1 xor 1 xor 1 = 1 This man again took advantage of the fact that in D there is no such operation - (analog switch). A natural solution in D:

Re: How to write similar code D?

2015-02-09 Thread bearophile via Digitalmars-d-learn
Dennis Ritchie: Tell me, please, how to write similar С# code D: This is more or less exactly the same: void main() { import std.stdio, std.range, std.algorithm, std.typecons, std.format; auto query = iota(2, 12) .map!(c = Tuple!(int,length, int,height,

Re: A safer interface for core.stdc

2015-02-08 Thread bearophile via Digitalmars-d
H. S. Teoh: Come to think of it, is there any point in making malloc @safe/@trusted at all? I am not asking for a @trusted function. I'd like a @system template wrapper for malloc/calloc/free that is safer than the C functions (safer because it's type-aware). Bye, bearophile

Re: Classes and @disable this()

2015-02-08 Thread bearophile via Digitalmars-d-learn
fra: However making it a compiler error would be far, far better I think this can be filed in Bugzilla as diagnostic enhancement: class Foo { @disable this(); this(int i) {} } void main() {} Bye, bearophile

Re: Template constructor in a non-template struct.

2015-02-08 Thread bearophile via Digitalmars-d-learn
ChrisG: I don't really understand how I'd differentiate a constructor template from a class/struct template. One solution is to use a template struct (struct/class names start with an upper case in D, while typed enum members usually start with a lower case): enum E { option1, option2 }

Re: primitive type variables not nullable ?

2015-02-08 Thread bearophile via Digitalmars-d-learn
Tobias Pankrath: Check for null with (x is null) not via printing to stdout. In most cases instead of checking dynamic arrays for null, it's better to use std.array.empty. Bye, bearophile

Re: A safer interface for core.stdc

2015-02-07 Thread bearophile via Digitalmars-d
Andrei Alexandrescu: Such wrappers would allow safe code to use more C stdlib primitives. I'd also like a safer templated wrapper for calloc() and malloc() and similar. Bye, bearophile

Re: Another idiom I wish were gone from phobos/druntime

2015-02-06 Thread bearophile via Digitalmars-d
Ola Fosheim Grøstad: Ada with SPARK 2014 makes D look like it is stuck in the 80s: 4.2. Subprogram Contracts 4.2.1. Preconditions 4.2.2. Postconditions 4.2.3. Contract Cases 4.2.4. Data Dependencies 4.2.5. Flow Dependencies 4.2.6. State Abstraction and Contracts 4.2.6.1. State Abstraction and

Some notes on Rust

2015-02-05 Thread bearophile via Digitalmars-d
In the best language blog: http://lambda-the-ultimate.org/node/5113 The discussion is long. They discuss if a good GC can be written in the language itself, about actual security, what a GC can and can't do, and more. Bye, bearophile

Re: Do you have a better way to remove element from a array?

2015-02-05 Thread bearophile via Digitalmars-d-learn
Tobias Pankrath: Works as designed: http://dlang.org/phobos/std_algorithm.html#.remove Unfortunately it's one of the worst designed functions of Phobos: https://issues.dlang.org/show_bug.cgi?id=10959 Bye, bearophile

Re: how can I get a reference of array?

2015-02-05 Thread bearophile via Digitalmars-d-learn
zhmt: Will arr.ptr change in the future? As the array add more members , it need more memroy, then remalloc may be called, the pointer maybe change, then the stored pointer will be invalid. Will this happen? Yes, it can happen. Bye, bearophile

Re: Do you have a better way to remove element from a array?

2015-02-05 Thread bearophile via Digitalmars-d-learn
bachmeier: It seems your argument is that remove is poorly designed because it's not destructive. Or am I missing your argument? It has to be a void function (or perhaps bettter it can return true/false if it has removed the item, so it becomes @nogc and nothrow). And it has to remove the

Re: Another idiom I wish were gone from phobos/druntime

2015-02-04 Thread bearophile via Digitalmars-d
Andrei Alexandrescu: auto opSlice(size_t low, size_t high) in { assert(low = high); } body { import std.range : take; return this[low .. $].take(high - low); }

Re: FYI - DIP25 is approved for 2.067

2015-02-04 Thread bearophile via Digitalmars-d
Andrei Alexandrescu: http://wiki.dlang.org/DIP25 I'd like a more comprehensive and flexible solution to the problem of static memory ownership. Bye, bearophile

Re: Another idiom I wish were gone from phobos/druntime

2015-02-04 Thread bearophile via Digitalmars-d
Zach the Mystic: I have an idea. Treat all assert statements which come before the first non-assert statement as part of the 'in' contract. I'm not saying the compiler has to generate a whole 'in' function, but these asserts can be internally tagged to behave *as if* in an 'in' contract.

Re: Conway's game of life

2015-02-03 Thread bearophile via Digitalmars-d-learn
Paul: Regarding the immutable loop variable, I've conditioned myself never to interfere with loop control values But adding immutable you don't risk modifying the variable by mistake. It's another design mistake of D. Variables (like foreach loop indexes) must be immutable by default

Re: Conway's game of life

2015-02-03 Thread bearophile via Digitalmars-d-learn
Paul: enum WORLDSIZE = 20; enum INITIALPOP = 70; //experimental enum DEAD = 0; enum ALIVE = 1; D enums don't need to be ALL UPPERCASE :-) int world[WORLDSIZE][WORLDSIZE]; Don't forget to compile with warnings active (it's a design error of the D

Re: how convert the range to slice ?

2015-02-02 Thread bearophile via Digitalmars-d-learn
Nordlöw: Is started digging a bit... The magic happens at line 103 in cast.c. How do I most conveniently figure out which members (functions) a type (e-type) has? I figured I could check for typical InputRange members and issue a hint about using .array if e-type has them. It's probably

Re: Conway's game of life

2015-02-02 Thread bearophile via Digitalmars-d-learn
gedaiu: https://github.com/gedaiu/Game-Of-Life-D A bare-bones implementation: http://rosettacode.org/wiki/Conway%27s_Game_of_Life#Faster_Version The quality of the D GC is not important for a simple Life implementation, you just need two arrays. Bye, bearophile

Re: std.algorithm sort() and reverse() confusion

2015-02-02 Thread bearophile via Digitalmars-d-learn
Jonathan M Davis: arr.reverse.map!sqrt Yes, but arguably, chaining calls in this case is bad, We have discussed this some time... and I'd like reverse() to return the original array (like the deprecated array .reverse property). It's not a perfect design, but allowing UFCS chains is

Re: Arrays, garbage collection

2015-01-30 Thread bearophile via Digitalmars-d
Andrei Alexandrescu: No, we need to define a function for that - please file an enhancement request, thanks. -- Andrei No, the enhancement is in Bugzilla since lot of time. But it's for a built-in syntax. A function name is too much long, and when the inlining is switched off it makes the

Re: Arrays, garbage collection

2015-01-30 Thread bearophile via Digitalmars-d
deadalnix: - added optimization to promote them on heap. Thing like int[2] = [1, 2] can trivially avoid GC allocation. See my usage examples (think of the examples as not having the s suffix): void main() { // Some imports here. foo([[1, 2]s, [3, 4]]s); auto t1 = tuple([1,

Re: Arrays, garbage collection

2015-01-30 Thread bearophile via Digitalmars-d
Daniel Kozák: No, it makes more bugs possible // this is compile error so its ok int[MUST_HAVE_FIVE_ITEMS] a=[1,2,3,5]; // Error: mismatched array lengths, 5 and 4 Have you tried to compile my code? It doesn't give an error. Bye, bearophile

Re: Deducing a template retrun parameter type based on an assignment?

2015-01-30 Thread bearophile via Digitalmars-d-learn
Jeremy DeHaan: I figured that it would be smart enough to deduce the parameter type based on the type that it was trying to be assigned to. For that you need languages like Haskell/Rust. D type inference doesn't work from the type something is assigned to. Bye, bearophile

From C#7 and Haskell

2015-01-29 Thread bearophile via Digitalmars-d
Design Notes for C#7: https://github.com/dotnet/roslyn/issues/98 Perhaps they will enhance C# switch (plus an optional special method) to support a form of pattern matching. I'd like something related in D, someday. - - - - - - - Haskell in the large (PDF):

Re: Check if type is from specific template?

2015-01-29 Thread bearophile via Digitalmars-d-learn
Tofu Ninja: Basically what the title says, how do I check if a type T is an instantiation of a specific template? If you have an updated Phobos std.traits.isInstanceOf could be what you look for. Bye, bearophile

Arrays, garbage collection

2015-01-29 Thread bearophile via Digitalmars-d
The D type inference for array literals is now more flexible: void main() { auto[$][$] m1 = [[1, 2], [3, 4], [5, 6]]; pragma(msg, typeof(m1)); // int[2][3] const auto[string] aa1 = [red: 1, blue: 2]; pragma(msg, typeof(aa1)); // const(int)[string] } It helps avoid bugs like:

Re: accept @pure @nothrow @return attributes

2015-01-28 Thread bearophile via Digitalmars-d
Ola Fosheim Grøstad: [0.1*x for x in range(10)] map(operator.mul,range(10),[0.1]*10) numpy.arange(0.,0.95,0.1) numpy.linspace(0,0.9,num=10) list(itertools.islice(itertools.count(0.0, 0.1), 10)) The first one (the list comp) is Pythonic, and it's obviously the obvious one :-) If you

Re: accept @pure @nothrow @return attributes

2015-01-28 Thread bearophile via Digitalmars-d
Dicebot: Yes, but it didn't also fix anything, only introduced more ways to do the same thing - without any plans for some consistent model. One of the rules of the Python Zen: There should be one-- and preferably only one --obvious way to do it. The word obvious is important, because

Re: Concern about the ref return argument and scope

2015-01-28 Thread bearophile via Digitalmars-d
Walter Bright: Of course, there is a bit of leap of faith in the least disruption thing, only some experience with the feature will tell for sure. It's very OK to experiment. deadalnix could be right: a more general solution could be easer to learn and use for the programmers than the

Re: how convert the range to slice ?

2015-01-28 Thread bearophile via Digitalmars-d-learn
Chris Williams: Range is not castable to array. See std.array.array to generate an array from a Range. Currently this program: void main() { import std.range; int[] a = iota(10); } Gives an error like: test.d(3,19): Error: cannot implicitly convert expression (iota(10)) of type

Re: how convert the range to slice ?

2015-01-28 Thread bearophile via Digitalmars-d-learn
Nordlöw: Is there any chance we could add logic to dmd+phobos that hints user about this? It's such a fundamental part of D+Phobos that newbies are forced to learn this quickly. On the other hand an informative error message could be useful... What error message do you suggest? Bye,

Re: I left my program open for 9 hours and it used up 700mb of ram, could someone review it?

2015-01-27 Thread bearophile via Digitalmars-d-learn
Gan: Is there some special stuff I gotta do extra with structs? Do they need manually allocated and released? Most of your usages of tiny structs should be by value. So just keep in mind they are values. Even when you iterate with a foreach on a mutable array of them :-) On a second

Re: Array List object?

2015-01-27 Thread bearophile via Digitalmars-d-learn
Gan: //Initializing the array tiles = new SBTile[](0); This is often useless. //Clearing the array tiles = []; This doesn't clear the array, it rebinds it to a null pointer. Bye, bearophile

Re: Array List object?

2015-01-27 Thread bearophile via Digitalmars-d-learn
And it's named dynamic array, instead of Array List object, it's not a class instance. Bye, bearophile

Re: Virtual functions and inheritance

2015-01-27 Thread bearophile via Digitalmars-d-learn
Baz: doesn't work. And similarly to the the orginal post: I suggest to read some D documentation first, and program later. Bye, bearophile

Re: Classical bug

2015-01-27 Thread bearophile via Digitalmars-d-learn
Vladimir Panteleev: But the check seems very simple, and is easily circumvented. This compiles: byte[] func() { byte[1024] buffer; auto p = buffer[0..3]; return p; } I guess such bugs will be detected (in safe code only!) after the implementation of: http://wiki.dlang.org/DIP69

Re: I left my program open for 9 hours and it used up 700mb of ram, could someone review it?

2015-01-27 Thread bearophile via Digitalmars-d-learn
Gan: How can I make it use less CPU/RAM? Most tiny classes probably should be structs. More generally, use a struct every time you don't need a class. You can start with those two: struct SBRange { double left = 0.0, right = 0.0, top = 0.0, bottom = 0.0; } struct Point(T) { T x,

<    1   2   3   4   5   6   7   8   9   10   >