Re: foreach() behavior on ranges

2021-08-25 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Wednesday, 25 August 2021 at 11:04:35 UTC, Steven Schveighoffer wrote: It never has called `save`. It makes a copy, which is almost always the equivalent `save` implementation. -Steve Really? Then what is the use for .save method then? The only reason I can find is that you can't declare

Re: foreach() behavior on ranges

2021-08-25 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Wednesday, 25 August 2021 at 08:15:18 UTC, frame wrote: I know, but foreach() doesn't call save(). Hmm, this is a regression probably, or I missed the time frame when foreach moved to use of copy constructor for forward ranges. Do we have a well defined description of what input, forward

Re: foreach() behavior on ranges

2021-08-25 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Wednesday, 25 August 2021 at 06:51:36 UTC, bauss wrote: Of course it doesn't disallow classes but it's generally advised that you use structs and that's what you want in 99% of the cases. It's usually a red flag when a range starts being a reference type. Well, sometimes you can't avoid

Re: foreach() behavior on ranges

2021-08-24 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Tuesday, 24 August 2021 at 09:15:23 UTC, bauss wrote: A range should be a struct always and thus its state is copied when the foreach loop is created. Actually the range contracts don't mention that it needs to be a by value type. It can also be a reference type, i.e. a class. Which

Re: foreach() behavior on ranges

2021-08-24 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Tuesday, 24 August 2021 at 08:36:18 UTC, frame wrote: How do you handle that issue? Are your ranges designed to have this bug or do you implement opApply() always? This is expected behavior imho. I think what you need is a forward range, not input range. By the contract of input range, it

Re: Lexicographical object comparison by selected members of a struct

2021-08-20 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Saturday, 21 August 2021 at 04:34:46 UTC, Ali Çehreli wrote: ... Consider __traits(identifier, member) instead. This one should return member name as string, removing the need of memberName function. Also you could have an annotation @Equality.Include for example, and make mixin scan

Re: Lexicographical object comparison by selected members of a struct

2021-08-20 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Saturday, 21 August 2021 at 05:34:59 UTC, Alexandru Ermicioi wrote: ... Also there is no need for mixing string code here. You can get the field using __traits(getMember, this, member).

Re: simple (I think) eponymous template question ... what is proper idimatic way ?

2021-08-17 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Tuesday, 17 August 2021 at 19:53:52 UTC, james.p.leblanc wrote: Wow! That is absolutely beautiful ... I had never seen (or even imagined) a recursive template! This expands my mind in a good way ... and is going into my toolbox immediately. Best Regards, James Just don't over rely on

Re: simple (I think) eponymous template question ... what is proper idimatic way ?

2021-08-17 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Tuesday, 17 August 2021 at 18:11:56 UTC, james.p.leblanc wrote: Is there a more elegant way, to do this? Regards, James PS Any violations should be caught at compile time. That is template specialization: ``` auto moo(T : YourSpecialClassOrDType)(T myMoo) {•••} ``` You can also declare

Re: Getting a working example of opIndexAssign using opSlice ... have troubles ...

2021-08-16 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Monday, 16 August 2021 at 06:36:02 UTC, james.p.leblanc wrote: To be honest, I am not exactly sure what is happening here. I am unfamiliar with the "(T : T[])" syntax ... need to read That is template argument secialization. You're saying that T can be accept only types that are arrays of

Re: Drawbacks of exceptions being globally allocated

2021-08-16 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Monday, 16 August 2021 at 06:12:14 UTC, Tejas wrote: ... Fyi, check out std.exeprimental.allocator package. You can use allocators from there to do allocation of exceptions, on the heap or any other region.

Re: Drawbacks of exceptions being globally allocated

2021-08-15 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Sunday, 15 August 2021 at 16:23:25 UTC, Ali Çehreli wrote: That output contains two automatically chained exceptions: Failed: Main failed This failed too: The destructor failed Ali Hmm, wasn't aware of such use case (results of too much java :)). Considering this case I'd say it is

Re: Drawbacks of exceptions being globally allocated

2021-08-15 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Sunday, 15 August 2021 at 00:15:32 UTC, Ali Çehreli wrote: Even though this feature is probably never used, in D, multiple exception objects are chained. For example, you can throw e.g. in a destructor when there is an active exception in flight and that second object gets attached to the

Re: Drawbacks of exceptions being globally allocated

2021-08-14 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Saturday, 14 August 2021 at 13:24:22 UTC, Tejas wrote: ... I don't think there are any gotchas here. The problem with this technique, is when your exceptions aren't just simple labels but also carry some additional data, say for example specific error type, and subject that, caused this.

Re: __traits() to get parameter details only ? ... hasMember looks up everything within

2021-08-04 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Wednesday, 4 August 2021 at 22:28:53 UTC, someone wrote: Is that what you mean ? Not really. I was assuming you were talking about @property methods, and if so you could declare such cases: ``` interface HasMutableLstrSymbolId { @property lstrSymbolId(); @property lstrSymbolId(string

Re: __traits() to get parameter details only ? ... hasMember looks up everything within

2021-08-04 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Wednesday, 4 August 2021 at 15:08:24 UTC, someone wrote: However, __traits(hasMember, ...) checks for the existence of anything labeled lstrCurrencyID within the class (eg: unrelated variables with same name; not gonna happen, but, I like to code it the right way); so, question is: is

Re: how much "real-life" code can be marked @safe ?

2021-07-04 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Saturday, 3 July 2021 at 20:09:56 UTC, tsbockman wrote: On Saturday, 3 July 2021 at 16:06:33 UTC, Alexandru Ermicioi wrote: 3. An edge case. Ex: You need to mutate some data and then assume it is immutable in a constructor. Can you give a valid example where that is necessary? The main

Re: how much "real-life" code can be marked @safe ?

2021-07-03 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Friday, 2 July 2021 at 22:08:31 UTC, tsbockman wrote: (Responding out of order:) On Friday, 2 July 2021 at 00:26:52 UTC, someone wrote: But when you start attempting to declare @safe chunks of code that actually DO things ... well, it seems end-of-the-story. If you find yourself unable to

Re: how much "real-life" code can be marked @safe ?

2021-07-02 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Friday, 2 July 2021 at 00:26:52 UTC, someone wrote: ... just wondering: ... Imho, if you want all of the app to be safe, and you cannot avoid unsafe code, then there are two choices: 1. Mark the method doing unsafe stuff as @trusted, or pieces of code which are unsafe with trusted lambda

Re: Printing Tuple!(...)[] using for loop?

2021-07-02 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Friday, 2 July 2021 at 04:21:24 UTC, Kirill wrote: ... 1. use static foreach for tuple loop. 2. start column and end column should be known at compile time. Either make them immutable, or as enum constant, or pass them as an template argument. Tuple is basically a wrapper over built in

Re: Error: function `...` without `this` cannot be `const`

2021-06-30 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Wednesday, 30 June 2021 at 20:12:29 UTC, H. S. Teoh wrote: On Wed, Jun 30, 2021 at 07:40:40PM +, someone via Digitalmars-d-learn wrote: [...] @property int data() { return m_data; } // read property [...] string something() @property { return this.whatever; } [...] Now I am not

Re: Error: function `...` without `this` cannot be `const`

2021-06-30 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Wednesday, 30 June 2021 at 17:47:05 UTC, someone wrote: ... That is because const/immutable/shared are being applied on the object hence 'this' variable inside function body if function is a member of a struct or class. It doesn't make sense to have a const modifier on a simple function.

Re: cloning array

2021-06-03 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Wednesday, 2 June 2021 at 15:32:38 UTC, Sean wrote: ... You can implement deep copy using template recursion: import std; T[] rdup(T : U[], U)(T[] duped) { return duped.map!(arr => arr.rdup).array; } T[] rdup(T)(T[] duped) { return duped.dup; }

Re: Wrong selection of opEquals for objects.

2020-08-28 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Friday, 28 August 2020 at 12:29:20 UTC, Simen Kjærås wrote: Seems that these methods should be rooted out from Object, and placed in respective interfaces like: - interface Equatable(T) { bool opEquals(T value); } - Then it would be a lot more simple. People who want

Re: Wrong selection of opEquals for objects.

2020-08-28 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Friday, 28 August 2020 at 10:42:09 UTC, Alexandru Ermicioi wrote: ... Also, why it is limited to just objects? It seems that this function enforces symmetry between two objects. What about rest of the possible types, such as structs, unions?

Re: Wrong selection of opEquals for objects.

2020-08-28 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Friday, 28 August 2020 at 10:28:07 UTC, Simen Kjærås wrote: What you'll need to do is mark every function that does compare two class objects with == as @trusted or @system. No that is not a solution at all, in template code that requires safety. You basically will have to sacrifice

Re: Wrong selection of opEquals for objects.

2020-08-28 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Friday, 28 August 2020 at 08:16:01 UTC, Alexandru Ermicioi wrote: Hi everyone, Would be glad at least to pointers, where in dmd is logic for operator overloading happens, as well as for overloading rules, so I could fix it myself, if no-one is able to pick up it.

Wrong selection of opEquals for objects.

2020-08-28 Thread Alexandru Ermicioi via Digitalmars-d-learn
Hi everyone, there is https://issues.dlang.org/show_bug.cgi?id=21180 bug, anyone knows how to avoid it? Test case: - import std; class Silly { bool opEquals(const Silly silly) const @safe { return silly is this; } alias opEquals = Object.opEquals; } bool

Re: Call method if declared only

2020-03-01 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Friday, 28 February 2020 at 06:12:37 UTC, Виталий Фадеев wrote: How to implement ? I would go for a template mixin that implements send operation in each subclass. Sine template mixin's content is declared in scope of declaration not template's module, the code inside it is aware of

Re: How create a function that receive a function and run it in another threading?

2019-12-30 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Friday, 27 December 2019 at 07:06:52 UTC, mipri wrote: On Friday, 27 December 2019 at 06:08:16 UTC, Marcone wrote: import std; import core.thread; auto threading(lazy void fun){ return task!fun().executeInNewThread(); } void main(){ threading(writeln("Hello World!")); } I want

Re: What type does byGrapheme() return?

2019-12-30 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Friday, 27 December 2019 at 17:26:58 UTC, Robert M. Münch wrote: ... There are set of range interfaces that can be used to mask range type. Check for https://dlang.org/library/std/range/interfaces/input_range.html for starting point, and for

Re: equivalent of typeid(Class).name at compile-time

2019-11-22 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Thursday, 21 November 2019 at 20:48:03 UTC, Adam D. Ruppe wrote: On Thursday, 21 November 2019 at 20:45:16 UTC, Steven Schveighoffer wrote: To clarify, I need the compile time string that will match typeid(instance).name, so I can match the derived type. You have to make sure that the

Re: Translating Java into D

2019-11-16 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Friday, 15 November 2019 at 03:29:16 UTC, Heromyth wrote: On Thursday, 14 November 2019 at 19:50:22 UTC, NonNull wrote: Greetings, Java seems to be almost a subset of D in various ways. No, it's not exactly right. Java is more powerful than D as for a language. Many things that Java can do

Re: Alternative to C++ macro in D

2019-11-03 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Sunday, 3 November 2019 at 16:55:36 UTC, Vinod K Chandran wrote: Hi all, I can do this in C++. #include using namespace std ; #define end }; #define log(x) cout << x << endl #define wait std::cin.get() int main() { log("Trying to avoid the visual clutter aused by closing curly

Re: Alternative to C++ macro in D

2019-11-03 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Sunday, 3 November 2019 at 17:02:30 UTC, Vinod K Chandran wrote: On Sunday, 3 November 2019 at 16:55:36 UTC, Vinod K Chandran wrote: Hi all, I can do this in C++. #include using namespace std ; #define end }; #define log(x) cout << x << endl #define wait std::cin.get() int main() {

Converting stringized array to dstring[]

2019-02-24 Thread Alexandru Ermicioi via Digitalmars-d-learn
Hi everyone, I've stumbled on another possible bug: - import std.stdio; import std.conv; void main() { "[ \"test\" ]".to!(dstring[]).writeln; } - It gives following error: - /dlang/dmd/linux/bin64/../../src/phobos/std/conv.d(222): Error:

template alias argument accepts only class/interface types.

2019-02-23 Thread Alexandru Ermicioi via Digitalmars-d-learn
Perhaps I missed somewhere, but it seems that you can pass only classes or interfaces to alias argument of a template. Trying to pass another type (such as int, int[]) yields an error. Example of issue: void test(alias T)(int o) { import std.stdio;

Cannot dispose const array?

2018-08-08 Thread Alexandru Ermicioi via Digitalmars-d-learn
Hi Dlang community! I've stumbled on an interesting issue with allocators. It seems that we can't get disposed of arrays with const or immutable data. Consider example below: Link: https://run.dlang.io/is/frnQI8 import std.stdio; import std.range; import

scope variable values assigned to non-scope this.placeholder

2018-08-06 Thread Alexandru Ermicioi via Digitalmars-d-learn
Hi Dlang community! I've been playing with dip1000 and scope storage class and stumbled upon a strange error that I can't to understand yet. Here is minimized version of code that generates the error: The link: https://run.dlang.io/is/rg2Odu -- import std.stdio;

Re: Function signature testing with is expression.

2017-12-17 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Sunday, 17 December 2017 at 16:19:00 UTC, ag0aep6g wrote: On Sunday, 17 December 2017 at 14:44:15 UTC, Alexandru Ermicioi wrote: Suppose: struct F { static void foo(T)(T i, int o) {} } enum bool check(T) = is(F.foo!T == void function(Z, int), Z); enum correct = check!int;

Function signature testing with is expression.

2017-12-17 Thread Alexandru Ermicioi via Digitalmars-d-learn
Hi, Have someone tried to test a function against a signature using is expression? Suppose: struct F { static void foo(T)(T i, int o) {} } enum bool check(T) = is(F.foo!T == void function(Z, int), Z); enum correct = check!int; Upon running it will return false,

ddoc and method overriding.

2017-08-12 Thread Alexandru Ermicioi via Digitalmars-d-learn
Hi all, Having a trivial example such as: ```D class Foo { /** * A documentation info **/ void fancy(); } class Moo { override void fancy(); } ``` Is there a way to tell ddoc to use Foo.fancy documentation block for overriding Moo.fancy method? Thx.

Re: ddoc: Can I escape a colon?

2017-02-26 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Thursday, 23 February 2017 at 21:04:39 UTC, Nick Sabalausky (Abscissa) wrote: Suppose I want ddoc output to include this line: -- Note: Blah blabbety blah -- But the colon causes "Note" to be considered a section header. Is there a way to escape the ":" so that it's

code.dlang.org package readme.md

2017-01-03 Thread Alexandru Ermicioi via Digitalmars-d-learn
Hi all. How it is possible to show readme.md from github repository in code.dlang.org for a particular project? Thanks.

Re: delegate passed in annotation struct cannot be invoked.

2016-12-30 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Thursday, 29 December 2016 at 21:29:47 UTC, Stefan Koch wrote: ldc accepts invalid code there. But it might be that dmd 2.071.2 did that as well. If so It will be fixed as soon as ldc updates the front-end version. Nope, tried with dmd v2.071.2 and it gives same error as v2.072.1. I think

Re: delegate passed in annotation struct cannot be invoked.

2016-12-29 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Thursday, 29 December 2016 at 21:07:00 UTC, Stefan Koch wrote: It's a delegate and not function. Therefore it will get a frame-ptr regardless, without checking if it is needed or not, or if there is a frame to point to. Since there is no frame to point to you get the error. At least this is

delegate passed in annotation struct cannot be invoked.

2016-12-29 Thread Alexandru Ermicioi via Digitalmars-d-learn
Given code below: import std.stdio; struct Annotation { public int delegate(int) dg; } void main() { import std.traits; __traits(getAttributes, Cls)[0].dg(20).writeln; } @Annotation(delegate int(int d) { return d; }) class Cls { void method() { } }

__traits(getOverloads, Type, member) order of elements in tuple.

2016-08-22 Thread Alexandru Ermicioi via Digitalmars-d-learn
Good day. In current implementation of dmd/ldc/gdc, does this trait guarantee, that the order of elements returned in tuple, is same, in several calls of it on same Type and member? Also, is guaranteed that in future versions of dmd, the order of elements won't change?

Re: Sequence separation

2016-08-21 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Wednesday, 17 August 2016 at 19:38:22 UTC, Engine Machine wrote: Well, the is does work and that probably is the best solution. I don't mind the extra type at this point. Of course, a library solution for this type of stuff would be nice. I'd rather not have to even use a type but rather

Re: Rebind template

2016-08-21 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Saturday, 20 August 2016 at 22:18:57 UTC, Engine Machine wrote: On Saturday, 20 August 2016 at 22:11:40 UTC, Engine Machine wrote: Is there a way to rebind the arguments of a template? template foo(X) { // X is like A!(a,b,c) Y = Rebind!(X,d,e,f); // Y is like A!(d,e,f); }

Ascii string literal.

2016-05-06 Thread Alexandru Ermicioi via Digitalmars-d-learn
Good day, Is it possible somehow to convert implicitly a string literal into an ubyte array? For example: void do(immutable(ubyte)[] asciiString) { // Do something with ascii string. } And from another section of code, calling it like: do("Some ascii string"); --- If no, is there

Wrap array into a range.

2016-03-05 Thread Alexandru Ermicioi via Digitalmars-d-learn
I have to pass an array to a function that accepts an input range. Therefore I need to transform somehow array into an input range. Is there a range that wraps an array in standard library?

Re: Why getting private member fails using getMember trait in a template?

2015-09-29 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Saturday, 26 September 2015 at 10:10:39 UTC, Alexandru Ermicioi wrote: Suppose we have, two modules: module testOne; [...] So, is this behavior correct? If yes, then why?

Why getting private member fails using getMember trait in a template?

2015-09-26 Thread Alexandru Ermicioi via Digitalmars-d-learn
Suppose we have, two modules: module testOne; import std.traits; template getMember(alias T, string member) { alias getMember = Identity!(__traits(getMember, T, member)); } module app; import testOne; import std.traits; class TestOne { private { int property; }

Re: Template detection

2015-09-22 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Tuesday, 22 September 2015 at 15:37:32 UTC, Meta wrote: On Sunday, 13 September 2015 at 08:26:55 UTC, Alexandru Ermicioi wrote: Hello, Given: class SomeClass { public { void someSimpleMethod() {} template setOfTemplatedMethods(Type) { void

Re: Template detection

2015-09-22 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Sunday, 13 September 2015 at 09:29:13 UTC, Enamex wrote: On Sunday, 13 September 2015 at 08:26:55 UTC, Alexandru Ermicioi wrote: Hello, Given: class SomeClass { public { void someSimpleMethod() {} template setOfTemplatedMethods(Type) { void

Template detection

2015-09-13 Thread Alexandru Ermicioi via Digitalmars-d-learn
Hello, Given: class SomeClass { public { void someSimpleMethod() {} template setOfTemplatedMethods(Type) { void templatedMethodOne() {} void templatedMethodTwo() {} } } } Is there a way to detect at compile time if a member of