Re: Generic operator overloading for immutable types?

2017-06-13 Thread Steven Schveighoffer via Digitalmars-d-learn
On 6/13/17 7:51 PM, ag0aep6g wrote: On 06/14/2017 12:45 AM, Steven Schveighoffer wrote: No, the fact that immutable implicitly casts to const(inout) is a special property enabled by the knowledge that immutable data can NEVER change, so it's OK to assume it's (at least) const for all

Re: Generic operator overloading for immutable types?

2017-06-13 Thread ag0aep6g via Digitalmars-d-learn
On 06/14/2017 12:45 AM, Steven Schveighoffer wrote: No, the fact that immutable implicitly casts to const(inout) is a special property enabled by the knowledge that immutable data can NEVER change, so it's OK to assume it's (at least) const for all references. The same cannot be true of const

Re: Static Initialization of Struct as UDA

2017-06-13 Thread jmh530 via Digitalmars-d-learn
On Tuesday, 13 June 2017 at 22:16:37 UTC, ag0aep6g wrote: No bug. `static` has no effect on module-level variables. `z` is a normal mutable variable, not at all guaranteed to be constant. Make it an `enum` or `immutable`. Note that immutable doesn't guarantee compile-time constancy,

Re: Generic operator overloading for immutable types?

2017-06-13 Thread Steven Schveighoffer via Digitalmars-d-learn
On 6/13/17 5:58 PM, ag0aep6g wrote: On 06/13/2017 10:50 PM, Steven Schveighoffer wrote: const(inout) actually *is* a thing :) It's a type constructor that can be implicitly cast from immutable. This has advantages in some cases. See (horribly written) table at the bottom if the inout function

Re: Static Initialization of Struct as UDA

2017-06-13 Thread ag0aep6g via Digitalmars-d-learn
On 06/14/2017 12:04 AM, jmh530 wrote: The code below doesn't compile because "static variable z cannot be read at compile time". However, z is a static variable, so I don't see why it wouldn't be available at compile-time. Bug or am I missing something? struct Bar { int x = 2; int

Re: Static Initialization of Struct as UDA

2017-06-13 Thread Basile B. via Digitalmars-d-learn
On Tuesday, 13 June 2017 at 22:04:48 UTC, jmh530 wrote: The code below doesn't compile because "static variable z cannot be read at compile time". However, z is a static variable, so I don't see why it wouldn't be available at compile-time. Bug or am I missing something? struct Bar {

Static Initialization of Struct as UDA

2017-06-13 Thread jmh530 via Digitalmars-d-learn
The code below doesn't compile because "static variable z cannot be read at compile time". However, z is a static variable, so I don't see why it wouldn't be available at compile-time. Bug or am I missing something? struct Bar { int x = 2; int y; } static Bar z = {y:1}; void main()

Re: Generic operator overloading for immutable types?

2017-06-13 Thread ag0aep6g via Digitalmars-d-learn
On 06/13/2017 10:50 PM, Steven Schveighoffer wrote: const(inout) actually *is* a thing :) It's a type constructor that can be implicitly cast from immutable. This has advantages in some cases. See (horribly written) table at the bottom if the inout function section here:

Re: First time user of LDC and getting newbie problems.

2017-06-13 Thread Joseph Rushton Wakeling via Digitalmars-d-learn
On Tuesday, 13 June 2017 at 20:04:46 UTC, WhatMeWorry wrote: Sorry I didn't reply sooner. I just reinstalled everything and it's all good. Something was really screwed up. "Screwed up" is also a fairly good way to describe my responses too, since I also missed your clear statement that you

Re: Generic operator overloading for immutable types?

2017-06-13 Thread Steven Schveighoffer via Digitalmars-d-learn
On 6/13/17 3:58 PM, ag0aep6g wrote: On 06/13/2017 09:29 PM, Gary Willoughby wrote: Is it possible for the `result` variable in the following code to be returned as an immutable type if it's created by adding two immutable types? Qualify the return type as `inout`: inout(Rational)

Re: First time user of LDC and getting newbie problems.

2017-06-13 Thread WhatMeWorry via Digitalmars-d-learn
On Tuesday, 13 June 2017 at 12:39:53 UTC, Joseph Rushton Wakeling wrote: On Tuesday, 13 June 2017 at 12:38:03 UTC, Joseph Rushton Wakeling wrote: On Sunday, 11 June 2017 at 21:58:27 UTC, WhatMeForget wrote: Just trying to compile a "Hello World" using dub and ldc2. I presume from your

Re: Generic operator overloading for immutable types?

2017-06-13 Thread ag0aep6g via Digitalmars-d-learn
On 06/13/2017 09:29 PM, Gary Willoughby wrote: Is it possible for the `result` variable in the following code to be returned as an immutable type if it's created by adding two immutable types? Qualify the return type as `inout`: inout(Rational) opBinary(/*...*/)(/*...*/) inout {/*...*/}

Re: Generic operator overloading for immutable types?

2017-06-13 Thread Gary Willoughby via Digitalmars-d-learn
On Tuesday, 13 June 2017 at 11:36:45 UTC, Steven Schveighoffer wrote: Nope, const works just fine. A clue is in your return type -- it's not inout! This should work: public Rational opBinary(string op)(Rational rhs) const If Rational had any indirections, then inout would be required, and

Re: How to implement opCmp?

2017-06-13 Thread Patrick Schluter via Digitalmars-d-learn
On Tuesday, 13 June 2017 at 16:49:14 UTC, H. S. Teoh wrote: On Tue, Jun 13, 2017 at 10:51:40AM -0400, Steven Schveighoffer via Digitalmars-d-learn wrote: [...] I think Andrei has a nice way to do opCmp for integers that's a simple subtraction and negation or something like that. [...] In

Re: How to implement opCmp?

2017-06-13 Thread Honey via Digitalmars-d-learn
On Tuesday, 13 June 2017 at 14:51:40 UTC, Steven Schveighoffer wrote: Yes, I saw that when I was looking (you can see from my reply that you quoted below). Yes, I had missed that point. Yes I think it makes sense to have such a comparison function for non-ranges. Yes it probably belongs

Re: How to implement opCmp?

2017-06-13 Thread H. S. Teoh via Digitalmars-d-learn
On Tue, Jun 13, 2017 at 10:51:40AM -0400, Steven Schveighoffer via Digitalmars-d-learn wrote: [...] > I think Andrei has a nice way to do opCmp for integers that's a simple > subtraction and negation or something like that. [...] In theory, cmp(int x, int y) can be implemented simply as (x - y).

Re: How to implement opCmp?

2017-06-13 Thread Steven Schveighoffer via Digitalmars-d-learn
On 6/11/17 11:24 AM, Honey wrote: On Friday, 9 June 2017 at 17:50:28 UTC, Honey wrote: Looking at the implementation of Tuple.opCmp, I'm not sure I'd bet on existence of opCmp for fundamental types: int opCmp(R)(R rhs) if (areCompatibleTuples!(typeof(this), R, "<")) {

Re: First time user of LDC and getting newbie problems.

2017-06-13 Thread Joseph Rushton Wakeling via Digitalmars-d-learn
On Tuesday, 13 June 2017 at 12:38:03 UTC, Joseph Rushton Wakeling wrote: On Sunday, 11 June 2017 at 21:58:27 UTC, WhatMeForget wrote: Just trying to compile a "Hello World" using dub and ldc2. I presume from your command line you're running Windows? ... I don't know where I got that idea

Re: First time user of LDC and getting newbie problems.

2017-06-13 Thread Joseph Rushton Wakeling via Digitalmars-d-learn
On Sunday, 11 June 2017 at 21:58:27 UTC, WhatMeForget wrote: Just trying to compile a "Hello World" using dub and ldc2. Let's start from the beginning: how did you install LDC? I presume from your command line you're running Windows?

Re: Weird template instantiation problem

2017-06-13 Thread Steven Schveighoffer via Digitalmars-d-learn
On 6/12/17 11:31 AM, Arafel wrote: As you can see, the only change is the type the function returns, but I don't see how it should make any difference. Also, changing from "enum" to "static immutable", or even removing the "enum" and directly embedding the function literal doesn't seem to make

Re: Generic operator overloading for immutable types?

2017-06-13 Thread Steven Schveighoffer via Digitalmars-d-learn
On 6/12/17 3:36 PM, H. S. Teoh via Digitalmars-d-learn wrote: On Mon, Jun 12, 2017 at 07:38:44PM +, Gary Willoughby via Digitalmars-d-learn wrote: In the following code is there any way to make the `opBinary` method generic to be able to accept immutable as well as a standard type? The

Re: Generic operator overloading for immutable types?

2017-06-13 Thread Steven Schveighoffer via Digitalmars-d-learn
On 6/12/17 3:51 PM, Gary Willoughby wrote: I don't know how H. S. Teoh managed to answer 'before' I posted but thanks guys! :) D programmers are *that* good. Seriously though, for NNTP connections, timestamp is taken from the submitter's PC. -Steve

Re: How to check whether a struct is templated?

2017-06-13 Thread Ali Çehreli via Digitalmars-d-learn
On 06/13/2017 02:00 AM, Andre Pany wrote: I can not find a traits "isTemplateOf". You're close. :) https://dlang.org/phobos/std_traits.html#isInstanceOf Ali

Re: How to check whether a struct is templated?

2017-06-13 Thread Balagopal Komarath via Digitalmars-d-learn
Are you looking for something like this? import std.typecons; import std.traits; alias yes = Nullable!int; struct no {} template isNullable(T : Nullable!X, X) { enum isNullable = true; } template isNullable(T) { enum isNullable = false; } void main() { static

How to check whether a struct is templated?

2017-06-13 Thread Andre Pany via Digitalmars-d-learn
Hi, I loop through a structure during compile time and want to check whether a field of the structure is of type Nullable. Therefore I use the TemplateOf traits which works for Nullable fields but raises an error for fields which are structures and not templated. static if(is(T == struct)

Re: Generic operator overloading for immutable types?

2017-06-13 Thread Gary Willoughby via Digitalmars-d-learn
On Monday, 12 June 2017 at 20:10:17 UTC, H. S. Teoh wrote: Therefore, nowadays I always recommend writing parenthesis with type modifiers, so that the intent it unambiguous, i.e., always write `inout(Rational)` rather than `inout Rational`, unless you intend for `inout` to apply to the

Re: Weird template instantiation problem

2017-06-13 Thread Arafel via Digitalmars-d-learn
Well, I had kind of found a workaround (changing the return type to return the element and not the index) which I didn't like too much (what if there are duplicates?). Now that I've found a "proper" workaround well, I'm still interested in knowing the reason, if possible, or if it's a