Re: Trouble using 'sort'
On 07/26/2016 10:11 AM, Bahman Movaqar wrote: > Alright...further experiments. The following works: > > sort!((pp1, pp2) => cmp(pp1.price, pp2.price) > 0)(theRange) > > So it may be something about what kind of range I'm passing to `sort`. > Am I right? > I meant sort!((pp1, pp2) => cmp(pp1.price, pp2.price) > 0)(theRange.array) -- Bahman
Re: Trouble using 'sort'
On 07/26/2016 09:35 AM, Bahman Movaqar wrote: > I have a range which is the result of a couple of chained range > operations, and each element is: > > Tuple!(string, "product", double, "price") > > Now I'd like to sort the range by "price" using: > > sort!((pp1, pp2) => cmp(pp1.price, pp2.price) > 0)(theRange) > > But I get a compile time error: > > source/services.d(166,63): Error: template std.algorithm.sorting.sort > cannot deduce function from argument types !((pp1, pp2) => > cmp(pp1.price, pp2.price) > 0)(MapResult!(__lambda5, Result)), > candidates are: > /home/bahman/Programs/D/dmd-2.071.0/linux/bin64/../../src/phobos/std/algorithm/sorting.d(1027,1): >std.algorithm.sorting.sort(alias less = "a < b", SwapStrategy ss > = SwapStrategy.unstable, Range)(Range r) if ((ss == > SwapStrategy.unstable && (hasSwappableElements!Range || > hasAssignableElements!Range) || ss != SwapStrategy.unstable && > hasAssignableElements!Range) && isRandomAccessRange!Range && > hasSlicing!Range && hasLength!Range) > source/services.d(168,5): Error: var has no effect in expression (theRange) > dmd failed with exit code 1. Alright...further experiments. The following works: sort!((pp1, pp2) => cmp(pp1.price, pp2.price) > 0)(theRange) So it may be something about what kind of range I'm passing to `sort`. Am I right? -- Bahman
Trouble using 'sort'
I have a range which is the result of a couple of chained range operations, and each element is: Tuple!(string, "product", double, "price") Now I'd like to sort the range by "price" using: sort!((pp1, pp2) => cmp(pp1.price, pp2.price) > 0)(theRange) But I get a compile time error: source/services.d(166,63): Error: template std.algorithm.sorting.sort cannot deduce function from argument types !((pp1, pp2) => cmp(pp1.price, pp2.price) > 0)(MapResult!(__lambda5, Result)), candidates are: /home/bahman/Programs/D/dmd-2.071.0/linux/bin64/../../src/phobos/std/algorithm/sorting.d(1027,1): std.algorithm.sorting.sort(alias less = "a < b", SwapStrategy ss = SwapStrategy.unstable, Range)(Range r) if ((ss == SwapStrategy.unstable && (hasSwappableElements!Range || hasAssignableElements!Range) || ss != SwapStrategy.unstable && hasAssignableElements!Range) && isRandomAccessRange!Range && hasSlicing!Range && hasLength!Range) source/services.d(168,5): Error: var has no effect in expression (theRange) dmd failed with exit code 1. And I have no clue what it is complaining about. I'd really appreciate any hint/help on this. Thanks, -- Bahman
Re: randomIO, std.file, core.stdc.stdio
On Tuesday, 26 July 2016 at 04:05:22 UTC, Charles Hixson wrote: Yes, but I really despise the syntax they came up with. It's probably good if most of your I/O is ranges, but mine hasn't yet ever been. (Combining ranges with random I/O?) that's why i wrote iv.stream, and then iv.vfs, with convenient things like `readNum!T`, for example. you absolutely don't need to reimplement the whole std.stdio.File if all you need it better API. thanks to UFCS, you can write your new API as free functions accepting std.stdio.File as first arg. or even generic stream, like i did in iv.stream: enum isReadableStream(T) = is(typeof((inout int=0) { auto t = T.init; ubyte[1] b; auto v = cast(void[])b; t.rawRead(v); })); enum isWriteableStream(T) = is(typeof((inout int=0) { auto t = T.init; ubyte[1] b; t.rawWrite(cast(void[])b); })); T readInt(T : ulong, ST) (auto ref ST st) if (isReadableStream!ST) { T res; ubyte* b = cast(ubyte*) foreach (immutable idx; 0..T.sizeof) { if (st.rawRead(b[idx..idx+1]).length != 1) throw new Exception("read error"); } return res; } and then: auto fl = File("myfile"); auto i = fl.readInt!uint; something like that.
Re: Getting most derived type of object that implements interface
Ahh I see, thanks guys.
Re: randomIO, std.file, core.stdc.stdio
On 07/25/2016 07:11 PM, ketmar via Digitalmars-d-learn wrote: On Tuesday, 26 July 2016 at 01:19:49 UTC, Charles Hixson wrote: then I will prefer the core.stdc.stdio approach. I find it's appearance extremely much cleaner... only if you are really used to write C code. when you see pointer, or explicit type size argument in D, it is a sign of C disease. I'll probably wrap those routines in a struct to ensure things like files being properly closed, and not have explicit pointers persisting over large areas of code. exactly what std.stdio.File did! ;-) Yes, but I really despise the syntax they came up with. It's probably good if most of your I/O is ranges, but mine hasn't yet ever been. (Combining ranges with random I/O?)
Re: Trouble checking for null-ness
On 07/25/2016 05:47 PM, Adam D. Ruppe wrote: > On Monday, 25 July 2016 at 13:09:22 UTC, Bahman Movaqar wrote: >> From what I could gather, it's not possible to check for `null` at >> runtime for reference based types. Am I right? > > No, it is only possible to check for null for reference based types. But > map's result is not a reference based type. Oh, I see now. Thanks. -- Bahman
Re: randomIO, std.file, core.stdc.stdio
On Tuesday, 26 July 2016 at 01:19:49 UTC, Charles Hixson wrote: then I will prefer the core.stdc.stdio approach. I find it's appearance extremely much cleaner... only if you are really used to write C code. when you see pointer, or explicit type size argument in D, it is a sign of C disease. I'll probably wrap those routines in a struct to ensure things like files being properly closed, and not have explicit pointers persisting over large areas of code. exactly what std.stdio.File did! ;-)
Re: randomIO, std.file, core.stdc.stdio
On 07/25/2016 05:18 PM, ketmar via Digitalmars-d-learn wrote: On Monday, 25 July 2016 at 18:54:27 UTC, Charles Hixson wrote: Are there reasons why one would use rawRead and rawWrite rather than fread and fwrite when doiing binary random io? What are the advantages? In particular, if one is reading and writing structs rather than arrays or ranges, are there any advantages? yes: keeping API consistent. ;-) for example, my stream i/o modules works with anything that has `rawRead`/`rawWrite` methods, but don't bother to check for any other. besides, `rawRead` is just looks cleaner, even with all `()[0..1])` noise. so, a question of style. OK. If it's just a question of "looking cleaner" and "style", then I will prefer the core.stdc.stdio approach. I find it's appearance extremely much cleaner...except that that's understating things. I'll probably wrap those routines in a struct to ensure things like files being properly closed, and not have explicit pointers persisting over large areas of code. (I said a lot more, but it was just a rant about how ugly I find rawRead/rawWrite syntax, so I deleted it.)
Re: Static ternary if
On Monday, 25 July 2016 at 22:57:05 UTC, Gorge Jingale wrote: On Monday, 25 July 2016 at 22:27:11 UTC, Cauterite wrote: On Monday, 25 July 2016 at 02:15:12 UTC, Gorge Jingale wrote: Is there a static ternary if? (A == B) ? C : D; for compile type that works like static if. You can pretty easily make your own; template staticIf(bool cond, alias a, alias b) { static if (cond) { alias staticIf = a; } else { alias staticIf = b; }; }; The drawback is that there's no 'short-circuiting'; a and b are both evaluated. Cool, that would work. I don't think the lazy evaluation is a problem at compile time? Just makes for longer times, but should be pretty minuscule. If that's ok, then try out std.traits.Select or std.traits.select: https://dlang.org/phobos/std_traits.html#Select
Re: randomIO, std.file, core.stdc.stdio
On Monday, 25 July 2016 at 18:54:27 UTC, Charles Hixson wrote: Are there reasons why one would use rawRead and rawWrite rather than fread and fwrite when doiing binary random io? What are the advantages? In particular, if one is reading and writing structs rather than arrays or ranges, are there any advantages? yes: keeping API consistent. ;-) for example, my stream i/o modules works with anything that has `rawRead`/`rawWrite` methods, but don't bother to check for any other. besides, `rawRead` is just looks cleaner, even with all `()[0..1])` noise. so, a question of style.
Re: Static ternary if
On Monday, 25 July 2016 at 22:27:11 UTC, Cauterite wrote: On Monday, 25 July 2016 at 02:15:12 UTC, Gorge Jingale wrote: Is there a static ternary if? (A == B) ? C : D; for compile type that works like static if. You can pretty easily make your own; template staticIf(bool cond, alias a, alias b) { static if (cond) { alias staticIf = a; } else { alias staticIf = b; }; }; The drawback is that there's no 'short-circuiting'; a and b are both evaluated. Cool, that would work. I don't think the lazy evaluation is a problem at compile time? Just makes for longer times, but should be pretty minuscule.
Re: Static ternary if
On Monday, 25 July 2016 at 02:15:12 UTC, Gorge Jingale wrote: Is there a static ternary if? (A == B) ? C : D; for compile type that works like static if. You can pretty easily make your own; template staticIf(bool cond, alias a, alias b) { static if (cond) { alias staticIf = a; } else { alias staticIf = b; }; }; The drawback is that there's no 'short-circuiting'; a and b are both evaluated.
randomIO, std.file, core.stdc.stdio
Are there reasons why one would use rawRead and rawWrite rather than fread and fwrite when doiing binary random io? What are the advantages? In particular, if one is reading and writing structs rather than arrays or ranges, are there any advantages?
Re: Default implementations in inherited interfaces
On Sunday, 24 July 2016 at 07:54:11 UTC, Jonathan Marler wrote: On Thursday, 21 July 2016 at 13:37:30 UTC, Saurabh Das wrote: On Thursday, 21 July 2016 at 12:42:14 UTC, Adam D. Ruppe wrote: On Thursday, 21 July 2016 at 09:41:27 UTC, Saurabh Das wrote: Java 8 has a 'default' keyword that allows interfaces to provide a default implementation and sub-classes can optionally override it if needed. The rationale behind it was extending interfaces without causing old code to faill. (called "virtual extension methods" or "defender methods"). The use case is similar to above. Is there a way to achieve an equivalent functionality in D? Thanks, Saurabh What an interesting technique. I've never seen this before. Maybe a DIP is in order? I think it would be low priority relative to the current work being done, but this technique seems like a good thing to support in the language. I am studying the use cases for defender methods. It would be good to support this in D. I don't think I have enough knowledge about the subject to write a DIP for it yet though.
Re: Trouble checking for null-ness
On Monday, 25 July 2016 at 13:09:22 UTC, Bahman Movaqar wrote: From what I could gather, it's not possible to check for `null` at runtime for reference based types. Am I right? No, it is only possible to check for null for reference based types. But map's result is not a reference based type.
Re: Trouble checking for null-ness
On 07/25/2016 05:07 PM, Bahman Movaqar wrote: > Suppose I have the following function: > > public auto max(alias comp, Range)(Range r) > in { > assert(r !is null && !r.empty); > } > body { > // ... > } > > When the function after a series of chained `map` operations, I get the > following error: > > Error: incompatible types for ((r) !is (null)): > 'MapResult!(__lambda2, SInvoiceLine[])' and 'typeof(null)' > > Of course if I remove `r !is null` from the `in` block, everything will > work. But I'm curious; how can I check for a `null` in this case? Thank you people for the answers. >From what I could gather, it's not possible to check for `null` at runtime for reference based types. Am I right? -- Bahman
Re: Static ternary if
On Monday, 25 July 2016 at 05:00:23 UTC, Ali Çehreli wrote: On 07/24/2016 07:15 PM, Gorge Jingale wrote: Is there a static ternary if? (A == B) ? C : D; for compile type that works like static if. The way to force an expression at compile time is to use it for something that's needed at compile time. For example, you can initialize a manifest constant (enum) with that expression: void main() { enum i = (__MODULE__.length % 2) ? 42 : 43; pragma(msg, i); } Instead of enum, you can use 'static const' as well. Ali It also works for "real" enumerated types. enum ver = 0; // version(Windows) ... else enum VersionRelative1 { A = ver ? 1 : 2, B = ver ? 3 : 4, } enum VersionRelative2 { A = !ver ? 1 : 2, B = !ver ? 3 : 4, } unittest { static assert(VersionRelative1.A == 2); static assert(VersionRelative2.A == 1); } which is quite cool and not widely known.
Re: Trouble checking for null-ness
On Monday, 25 July 2016 at 12:37:18 UTC, Bahman Movaqar wrote: Suppose I have the following function: public auto max(alias comp, Range)(Range r) in { assert(r !is null && !r.empty); } body { // ... } When the function after a series of chained `map` operations, I get the following error: Error: incompatible types for ((r) !is (null)): 'MapResult!(__lambda2, SInvoiceLine[])' and 'typeof(null)' Of course if I remove `r !is null` from the `in` block, everything will work. But I'm curious; how can I check for a `null` in this case? In the general case, the vast majority of ranges you work with will be value types. In the rare case where it's a reference type, you can use static if to specialize the assert. Two possibilities have already been suggested in this thread, but given that ranges are nearly always going to be a struct or a class, then you might do this inside the template: static if(is(Range == class)) { // check for null } You may also want to add a constraint: import std.range : isInputRange; public auto max(alias comp, Range)(Range r) if(isInputRange!Range) { static if(is(Range == class)) assert(r !is null && !r.empty); else assert(!r.empty); }
Re: Trouble checking for null-ness
On Monday, 25 July 2016 at 12:47:25 UTC, Cauterite wrote: (!__traits(compiles, r is null) || r !is null) && !r.empty Ah, whoops that's wrong, looks like ketmar had the right idea.
Re: Trouble checking for null-ness
On Monday, 25 July 2016 at 12:37:18 UTC, Bahman Movaqar wrote: But I'm curious; how can I check for a `null` in this case? Well, if you're happy with assertion failure by access violation, you may not even need to check for null, because generally if you try to call .empty on a null pointer you'll get an access violation (killing two birds with one stone). Otherwise you could try (!__traits(compiles, r is null) || r !is null) && !r.empty
Re: Trouble checking for null-ness
On Monday, 25 July 2016 at 12:37:18 UTC, Bahman Movaqar wrote: Error: incompatible types for ((r) !is (null)): 'MapResult!(__lambda2, SInvoiceLine[])' and 'typeof(null)' Of course if I remove `r !is null` from the `in` block, everything will work. But I'm curious; how can I check for a `null` in this case? You can't. null is not a valid value for the return type of map.
Re: Trouble checking for null-ness
static if (is(typeof(r is null))) { ...you can do your assert here... }
Trouble checking for null-ness
Suppose I have the following function: public auto max(alias comp, Range)(Range r) in { assert(r !is null && !r.empty); } body { // ... } When the function after a series of chained `map` operations, I get the following error: Error: incompatible types for ((r) !is (null)): 'MapResult!(__lambda2, SInvoiceLine[])' and 'typeof(null)' Of course if I remove `r !is null` from the `in` block, everything will work. But I'm curious; how can I check for a `null` in this case? Thanks, -- Bahman
Re: Getting most derived type of object that implements interface
On 7/25/16 5:54 AM, Kagamin wrote: Cast it to Object: FooInterface a = new BarImplementsInterface(); FooBaseClass b = new BarDerivedClass(); Object o = cast(Object)a; writefln("a class: %s", a.classinfo.name); writefln("b class: %s", b.classinfo.name); writefln("o class: %s", o.classinfo.name); Yes, for the unrelated reason that COM objects may not be D objects, interfaces that can only possibly be D Objects don't implicitly cast to Object. -Steve
dub set default compiler for project
dub build has the --compiler= option. Is there any way to set it to default a custom version (own branch, resides in ../../ldcbuild/bin/ldc2 ) of ldc2 in the dub.json (or .sdl)? The project will only compile with that compiler.
Re: Cannot compare object.opEquals is not nogc
On 7/23/16 5:44 PM, Rufus Smith wrote: On Saturday, 23 July 2016 at 17:27:24 UTC, Lodovico Giaretta wrote: On Saturday, 23 July 2016 at 17:04:42 UTC, Jonathan Marler wrote: On Saturday, 23 July 2016 at 16:46:20 UTC, Jonathan Marler wrote: [...] Actually Im going to disagree with myself. This technique actually wouldn't work with virtual methods:) I don't think we have the big problems with @nogc that people points out. I mean, we cannot decide that specific methods or opXXX must always be @nogc. That's too restrictive. So, what we need to do is: - use templates: with them, we can have our algorithms be @safe when applied to @safe types, @nogc when applied to @nogc types, and so on; for example, instead of taking a specific delegate type, we shall always accept a generic type, and use traits to guarantee it is some delegate; in this way, we can accept @safe delegate and propagate @safety to our algorithm, or accept @system and have our algorithm usable in @system code; same with @nogc et al. - when we use virtual methods, we are giving up all compiler-checked attributes; in this situation we have two options: - we trust what we are doing: e.g. we cannot mark a thing @nogc, but we know it is and the profiler confirms that no allocation happens, so we are happy; our aim is having code that doesn't freeze because of collections, and not marking code @nogc. This is bad. It only creates a faulty foundation. The whole point of nogc is to enforce nogc behavior. If you don't use it your not enforcing anything and then things slip by only to create problems later. This mentality is completely wrong and leads to decay. This is exactly why we are discussing this right now, because someone decided that it was ok to ignore other use cases which eventually turn out to be quite important. Again, I want to stress that Object.opEquals has been around since early D1 days, @nogc is only a few years old, it was not a wrong decision. @nogc cannot be added without breaking code, and even if you could, it's not correct for opEquals. The issue is one of design, Object should not define which attributes are acceptable on all objects, that should be up to the class designer. The solution is to deprecate and remove all convenience functions from Object. DMD should probably be branched, and all GC stuff removed, then built back up to have the proper features that the GC version has. I think someone has essentially done this on their own, but never built it up to full capacity. This is an extreme solution for a minor problem. -Steve
Re: How to pause terminal in D on Linux?
On Sunday, 24 July 2016 at 00:54:21 UTC, Zekereth wrote: On Saturday, 23 July 2016 at 19:08:00 UTC, WhatMeWorry wrote: What I thought would be trivial is becoming a nightmare. Can anybody set me straight. Thanks in advance. [...] Use the getchar() function. void pause(const string msg = "Press enter/return to continue...") { write(msg); getchar(); } just call std.stdio.stdin.read() and ignore the result...
Re: Singletons importing each other?
On Sunday, 24 July 2016 at 15:38:13 UTC, lqjglkqjsg wrote: On Sunday, 24 July 2016 at 15:07:20 UTC, Jack wrote: [...] - You can use a 3rd module that imports the two that "cycle". - You can use another singleton implementation that doesn't rely on a static this. e.g a kind of "lazy factory" that control the uniquness. Oh ok. Thanks for the ideas. Cheers!
Re: Getting most derived type of object that implements interface
yep, cast it. without the cast, compiler assuming that it knows the type in runtime, and is using well-known classinfo address instead of really looking into instance for that.
Re: Getting most derived type of object that implements interface
Cast it to Object: FooInterface a = new BarImplementsInterface(); FooBaseClass b = new BarDerivedClass(); Object o = cast(Object)a; writefln("a class: %s", a.classinfo.name); writefln("b class: %s", b.classinfo.name); writefln("o class: %s", o.classinfo.name);
Re: debug public release private
On Monday, July 25, 2016 04:58:55 Gorge Jingale via Digitalmars-d-learn wrote: > debug mixin("public"); else mixin("private"); > > Doesn't work. > > It's nice to have public members when debugging because they show > up in the debugger and one can access internals for checking. One > can enable per line using debug but it requires lots of duplicate > code. > > > Is there any easy way to do this? What kind of deubgger cares about public or private? I don't think that I've ever seen a debugger that did. Debuggers normally give you access to everything. - Jonathan M Davis
Getting most derived type of object that implements interface
I'm having trouble getting the full name of an object of a class that implements an interface, using typeid() or .classinfo, the behavior seems to be different from that of a class that simply derives other classes. interface FooInterface {} class BarImplementsInterface : FooInterface {} class FooBaseClass {} class BarDerivedClass : FooBaseClass {} void main() { FooInterface a = new BarImplementsInterface(); FooBaseClass b = new BarDerivedClass(); writefln("a class: %s", a.classinfo.name); writefln("b class: %s", b.classinfo.name); } Output: a class: test.FooInterface b class: test.BarDerivedClass I expected "a class: test.BarImplementsInterface" as the result output.. Am I expecting the wrong behavior? Is there a preferred way to do this?
Re: JSON Serialization with runtime filtering.
On Saturday, 23 July 2016 at 21:30:52 UTC, yawniek wrote: On Friday, 22 July 2016 at 12:36:31 UTC, Alexander Milushev wrote: I there any json serialization library which allow to make decision about ignoring fields in runtime? I trying to write rest client but server accept either 'cmd' or 'args' field for example and I need to find solution. can you give an example? i'm not sure what you mean by "ignore at runtime" if its simply ignoring fields of a struct then thats easily doable with https://github.com/tamediadigital/asdf Skipping all non-initialized fields in struct/class, or skipping all field equal some default value which can be set compile time.
Re: Transform/Compile to C/CPP as a target
On Saturday, 23 July 2016 at 12:27:24 UTC, ParticlePeter wrote: Is there any kind of project or workflow that converts D (subset) to C/CPP ? The short answer is no, not for any recent version of D.