Re: Proper way to handle "alias this" deprecation for classes

2023-05-17 Thread Salih Dincer via Digitalmars-d-learn
On Wednesday, 17 May 2023 at 08:00:17 UTC, Dom DiSc wrote: If you want auto-conversion, you should be more explicit: ```d float opCast() { } ``` because if you return "auto" it is not the highest-prio fit and therefore not chosen. If you have multiple choices, you still don't need to use "auto"

Re: Proper way to handle "alias this" deprecation for classes

2023-05-17 Thread Dom DiSc via Digitalmars-d-learn
On Friday, 12 May 2023 at 15:00:48 UTC, Salih Dincer wrote: ```d struct Fraction { int num, den; this(int n, int d) { num = n; den = d; } // Cast Expression : convert float value of fraction auto opCast(T : float)(

Re: Proper way to handle "alias this" deprecation for classes

2023-05-12 Thread Salih Dincer via Digitalmars-d-learn
On Sunday, 7 May 2023 at 21:04:05 UTC, Inkrementator wrote: Open question to everybody: What you're opinion on using opCast for this? Since it's a type conversion, it seems fitting to me. Can't converting without explicitly specifying in D is a big shortcoming in my opinion. There is such a th

Re: Proper way to handle "alias this" deprecation for classes

2023-05-10 Thread H. S. Teoh via Digitalmars-d-learn
On Wed, May 10, 2023 at 10:57:13PM +, Chris Piker via Digitalmars-d-learn wrote: > On Wednesday, 10 May 2023 at 20:25:48 UTC, H. S. Teoh wrote: > > On Wed, May 10, 2023 at 07:56:10PM +, Chris Piker via > > Digitalmars-d-learn wrote: [...] > > I also suffer from left/right confusion, and al

Re: Proper way to handle "alias this" deprecation for classes

2023-05-10 Thread Chris Piker via Digitalmars-d-learn
On Wednesday, 10 May 2023 at 20:25:48 UTC, H. S. Teoh wrote: On Wed, May 10, 2023 at 07:56:10PM +, Chris Piker via Digitalmars-d-learn wrote: [...] I also suffer from left/right confusion, and always have to pause to think about which is the right(!) word before uttering it. Oh, I though wa

Re: Proper way to handle "alias this" deprecation for classes

2023-05-10 Thread H. S. Teoh via Digitalmars-d-learn
On Wed, May 10, 2023 at 07:56:10PM +, Chris Piker via Digitalmars-d-learn wrote: [...] > My problem with the terms lvalue and rvalue is much more basic, and is > just a personal one that only affects probably 0.1% of people. I just > can't keep left vs. right straight in real life. "Right" i

Re: Proper way to handle "alias this" deprecation for classes

2023-05-10 Thread Chris Piker via Digitalmars-d-learn
On Wednesday, 10 May 2023 at 16:01:40 UTC, H. S. Teoh wrote: x = y; ^ ^ | | lvalue rvalue ... // This is OK: x = y + 1; // This is not OK: (y + 1) = x; Thanks for the clear explanation. My problem with the ter

Re: Proper way to handle "alias this" deprecation for classes

2023-05-10 Thread H. S. Teoh via Digitalmars-d-learn
On Wed, May 10, 2023 at 03:24:48PM +, Chris Piker via Digitalmars-d-learn wrote: [...] > It's off topic, but I forget why managing memory for rvalues* was > pushed onto the programmer and not handled by the compiler. I'm sure > there is a good reason but it does seem like a symmetry breaking

Re: Proper way to handle "alias this" deprecation for classes

2023-05-10 Thread Chris Piker via Digitalmars-d-learn
On Wednesday, 10 May 2023 at 14:42:50 UTC, Inkrementator wrote: On Sunday, 7 May 2023 at 21:12:22 UTC, Chris Piker wrote: https://gist.github.com/run-dlang/9b7aec72710b1108fc8277789776962a Thanks for posting that. Reading over the code I'm reminded that I never cared whether something was an

Re: Proper way to handle "alias this" deprecation for classes

2023-05-10 Thread Inkrementator via Digitalmars-d-learn
On Sunday, 7 May 2023 at 21:12:22 UTC, Chris Piker wrote: On the other hand, your first suggestion of using opCast() does seem like a reasonable choice to me. Can you provide a short code snippet using opCast to achieve the same result? I've never used it, and particularly I know that I don't

Re: Proper way to handle "alias this" deprecation for classes

2023-05-07 Thread Ali Çehreli via Digitalmars-d-learn
On 5/7/23 13:44, Chris Piker wrote: > to fix the problem I > just delete the alias this line from dpq2, see what unit tests and app > code it breaks, then fix each of those. Yes but I neglected the lvalue/rvalue issue. In some cases the code won't compile if the return typ

Re: Proper way to handle "alias this" deprecation for classes

2023-05-07 Thread Chris Piker via Digitalmars-d-learn
On Sunday, 7 May 2023 at 21:04:05 UTC, Inkrementator wrote: On Sunday, 7 May 2023 at 18:19:04 UTC, Ali Çehreli wrote: alias this is for implicit type conversions, which can be achieved explicitly as well. Open question to everybody: What you're opinion on using opCast for this? Since i

Re: Proper way to handle "alias this" deprecation for classes

2023-05-07 Thread Inkrementator via Digitalmars-d-learn
On Sunday, 7 May 2023 at 18:19:04 UTC, Ali Çehreli wrote: alias this is for implicit type conversions, which can be achieved explicitly as well. Open question to everybody: What you're opinion on using opCast for this? Since it's a type conversion, it seems fitting to me. A

Re: Proper way to handle "alias this" deprecation for classes

2023-05-07 Thread Chris Piker via Digitalmars-d-learn
On Sunday, 7 May 2023 at 18:19:04 UTC, Ali Çehreli wrote: auto main() { auto c = new C(); // The same type conversion is now explicit: foo(c.asIntPtr); } Hi Ali Ah, very clear explanation, thanks! So basically to fix the problem I just delete the alias this line from dpq2, see

Re: Proper way to handle "alias this" deprecation for classes

2023-05-07 Thread Ali Çehreli via Digitalmars-d-learn
On 5/7/23 10:55, Chris Piker wrote: > According to dmd 2.103, alias this is > deprecated for classes, so I'd like to correct the problem. alias this is for implicit type conversions, which can be achieved explicitly as well. Given the following old code: class C { int* result;

Proper way to handle "alias this" deprecation for classes

2023-05-07 Thread Chris Piker via Digitalmars-d-learn
Hi D One of the dependencies for my project has a class that makes use of the `alias x this` construct. According to dmd 2.103, alias this is deprecated for classes, so I'd like to correct the problem. Is there a specific paragraph or two that I can read to find out what i

Re: public vs private alias this

2022-11-01 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 1 November 2022 at 23:01:57 UTC, Per Nordlöw wrote: When is it preferrable to use ```d struct S { private T t; alias t this; } ``` instead of ```d struct S { public T t; alias t this; } ``` for any given type `T`? If the `alias this` needs to work outside the module where `S

public vs private alias this

2022-11-01 Thread Per Nordlöw via Digitalmars-d-learn
When is it preferrable to use ```d struct S { private T t; alias t this; } ``` instead of ```d struct S { public T t; alias t this; } ``` for any given type `T`?

Re: problem with alias this and Tuple

2021-10-12 Thread Mike Parker via Digitalmars-d-learn
On Tuesday, 12 October 2021 at 15:55:40 UTC, Johann Lermer wrote: Thanks, understood. But isn't this a deficiency in the library that should be fixed? The problem extends to more than just `toHash`. Take a look at this DConf 2019 presentation by Eduard Staniloiu on ProtoObject (as proposed by

Re: problem with alias this and Tuple

2021-10-12 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 12 October 2021 at 15:55:40 UTC, Johann Lermer wrote: Thanks, understood. But isn't this a deficiency in the library that should be fixed? You mean the part about how `Object.toHash` doesn't work with `const`? Yes, it is a deficiency in the library. The problem is, it cannot be fi

Re: problem with alias this and Tuple

2021-10-12 Thread Johann Lermer via Digitalmars-d-learn
Thanks, understood. But isn't this a deficiency in the library that should be fixed?

Re: problem with alias this and Tuple

2021-10-12 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 12 October 2021 at 09:30:57 UTC, Johann Lermer wrote: Hi all, I have a problem with Tuples and struct templates that contain an alias this: ```d import std; struct Element(T) { T payload; alias payload this; this (T p) { payload

problem with alias this and Tuple

2021-10-12 Thread Johann Lermer via Digitalmars-d-learn
Hi all, I have a problem with Tuples and struct templates that contain an alias this: ```d import std; struct Element(T) { T payload; alias payload this; this (T p) { payload = p; } } class Item {} void main () { auto e

Re: alias this - am I using it wrong?

2021-08-25 Thread Johann Lermer via Digitalmars-d-learn
, that should solve my current problem. The reason for having an alias this was that I had a C data type (cairo_surface_t) that I transferred into a class and I'm using the alias so that I don't need to rewrite the whole application at once. Unfortunately I overlooked some code that res

Re: alias this - am I using it wrong?

2021-08-25 Thread jmh530 via Digitalmars-d-learn
On Wednesday, 25 August 2021 at 12:23:06 UTC, Adam D Ruppe wrote: [snip] That's a lot about alias this that I didn't know. Thanks.

Re: alias this - am I using it wrong?

2021-08-25 Thread FeepingCreature via Digitalmars-d-learn
On Wednesday, 25 August 2021 at 12:11:01 UTC, Johann Lermer wrote: Hi all, I have a little problem understanding alias this. I always thought, that alias this only makes implicit conversions from the aliased object to this. Then, why do lines 18 and 22 compile in the code below? And, btw

Re: alias this - am I using it wrong?

2021-08-25 Thread Adam D Ruppe via Digitalmars-d-learn
On Wednesday, 25 August 2021 at 12:11:01 UTC, Johann Lermer wrote: I have a little problem understanding alias this. I always thought, that alias this only makes implicit conversions from the aliased object to this. What it does is if "a SOMETHING b" doesn't compile, it

Re: alias this - am I using it wrong?

2021-08-25 Thread jfondren via Digitalmars-d-learn
On Wednesday, 25 August 2021 at 12:11:01 UTC, Johann Lermer wrote: ```d 14 void main () 15 { 16 auto ac = new Alias_Class; 17 Test_Struct ts = ac; // compiles 18 ac = ts; // compiles as well - why? 19 20 auto tc = new Test_Class; 21 ts = tc.ac; // compi

alias this - am I using it wrong?

2021-08-25 Thread Johann Lermer via Digitalmars-d-learn
Hi all, I have a little problem understanding alias this. I always thought, that alias this only makes implicit conversions from the aliased object to this. Then, why do lines 18 and 22 compile in the code below? And, btw, line 22 crashes with a segmentation fault. ```d 01 struct

Re: seeking advice: possible new @attribute to mark class' default property to avoid alias this ?

2021-08-07 Thread someone via Digitalmars-d-learn
default property can be another object altogether. The best use case I can think of is a default collection for a class such as lobjComputers.computers in my example. That really isn't what alias this is used for commonly. I.e. I didn't know alias this even existed a month ago s

Re: seeking advice: possible new @attribute to mark class' default property to avoid alias this ?

2021-08-07 Thread jfondren via Digitalmars-d-learn
best use case I can think of is a default collection for a class such as lobjComputers.computers in my example. That really isn't what alias this is used for commonly. I.e. I didn't know alias this even existed a month ago so I cannot comment for what's oftenly used; I just st

Re: seeking advice: possible new @attribute to mark class' default property to avoid alias this ?

2021-08-07 Thread someone via Digitalmars-d-learn
class such as lobjComputers.computers in my example. That really isn't what alias this is used for commonly. I.e. I didn't know alias this even existed a month ago so I cannot comment for what's oftenly used; I just stated that I was pointed to alias this when I asked for def

Re: seeking advice: possible new @attribute to mark class' default property to avoid alias this ?

2021-08-07 Thread rikki cattermole via Digitalmars-d-learn
So a field that will automatically be resolved to as part of the behavior of generated toString methods. That really isn't what alias this is used for commonly. I.e. struct ValueReference { private { SomethingElse* impl; } bool isNull() { return im

Re: seeking advice: possible new @attribute to mark class' default property to avoid alias this ?

2021-08-07 Thread someone via Digitalmars-d-learn
On Sunday, 8 August 2021 at 00:57:47 UTC, Paul Backus wrote: On Sunday, 8 August 2021 at 00:52:43 UTC, someone wrote: Now that I am aware of Walter's stance on alias this: "alias this has turned out to be a mistake" @ https://news.ycombinator.com/item?id=28029184 ... would

Re: seeking advice: possible new @attribute to mark class' default property to avoid alias this ?

2021-08-07 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 8 August 2021 at 00:52:43 UTC, someone wrote: Now that I am aware of Walter's stance on alias this: "alias this has turned out to be a mistake" @ https://news.ycombinator.com/item?id=28029184 ... would you, I mean the community, think is it a good idea to file a DI

seeking advice: possible new @attribute to mark class' default property to avoid alias this ?

2021-08-07 Thread someone via Digitalmars-d-learn
Now that I am aware of Walter's stance on alias this: "alias this has turned out to be a mistake" @ https://news.ycombinator.com/item?id=28029184 ... would you, I mean the community, think is it a good idea to file a DIP to eventually get a new attribute to unambiguously

Re: array inside a class + alias this + filter -> clears the array.

2021-07-07 Thread realhet via Digitalmars-d-learn
On Wednesday, 7 July 2021 at 17:10:01 UTC, Paul Backus wrote: On Wednesday, 7 July 2021 at 16:20:29 UTC, realhet wrote: int[] opIndex() { return array; } Thx, I didn't know about this type of opSlice override. It works nicely. Now I have these choices: - write [] everywhere to access

Re: array inside a class + alias this + filter -> clears the array.

2021-07-07 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 7 July 2021 at 16:20:29 UTC, realhet wrote: Hi, I wanted to make a container class that exposes its elements using a simple "alias this", but getting weird errors: I test with the std.algorithm.filter template function. 1. when I use "alias this" on a fun

array inside a class + alias this + filter -> clears the array.

2021-07-07 Thread realhet via Digitalmars-d-learn
Hi, I wanted to make a container class that exposes its elements using a simple "alias this", but getting weird errors: I test with the std.algorithm.filter template function. 1. when I use "alias this" on a function that returns a slice, making the internal array unre

Re: static alias this and if/do/while/for

2020-10-26 Thread Q. Schroll via Digitalmars-d-learn
On Thursday, 22 October 2020 at 21:55:59 UTC, Jack Applegame wrote: There is a funny feature (or bug) in the D language: static alias this and static operator overloading. For example interface Foo { static { int value; void opAssign(int v) { value = v; } int get

Re: static alias this and if/do/while/for

2020-10-26 Thread Vladimirs Nordholm via Digitalmars-d-learn
On Thursday, 22 October 2020 at 21:55:59 UTC, Jack Applegame wrote: Now we can use type Foo as if it were an lvalue/rvalue: Foo = 5; int a = Foo; int b = Foo + a; Haha, that's pretty neat!

static alias this and if/do/while/for

2020-10-22 Thread Jack Applegame via Digitalmars-d-learn
There is a funny feature (or bug) in the D language: static alias this and static operator overloading. For example interface Foo { static { int value; void opAssign(int v) { value = v; } int get() { return value; } alias get this; } } Now we can use

Re: Subtyping with alias this

2020-08-17 Thread novice3 via Digitalmars-d-learn
On Tuesday, 18 August 2020 at 05:54:16 UTC, H. S. Teoh wrote: Here's a working example: Thank you, it works!

Re: Subtyping with alias this

2020-08-17 Thread H. S. Teoh via Digitalmars-d-learn
On Tue, Aug 18, 2020 at 05:34:58AM +, novice3 via Digitalmars-d-learn wrote: [...] > The problem is: > if i use fmt.spec in overloaded toString(), > when i get error "phobos/std/format.d(2243): Error: no property ip for > type onlineapp.IpV4Address" Here's a working example: -

Re: Subtyping with alias this

2020-08-17 Thread novice3 via Digitalmars-d-learn
On Monday, 17 August 2020 at 14:43:27 UTC, H. S. Teoh wrote: What you need is to create an overload of toString that takes a FormatSpec parameter, so that you can decide what should be output for which format spec. Something along these lines: Sorry, i can't make it works. I tried ti read for

Re: Subtyping with alias this

2020-08-17 Thread H. S. Teoh via Digitalmars-d-learn
On Mon, Aug 17, 2020 at 02:29:47PM +, novice3 via Digitalmars-d-learn wrote: [...] > ``` > struct IpV4Address > { > private uint ip; > alias ip this; > > string toString() > { > import std.conv: to; > return to!string((ip >>> 24) & 0xFF) ~ "." ~ >to!string((ip >>> 1

Subtyping with alias this

2020-08-17 Thread novice3 via Digitalmars-d-learn
Hello. I need subtype uint to store ipv4 address. It should be like ordinary uint, except conversion to string with %s format. My try https://run.dlang.io/is/fwTc0H failed on last assert: ``` struct IpV4Address { private uint ip; alias ip this; string toString() { import std.conv:

Re: I need an Easy example to understand Alias This

2020-07-07 Thread Ferhat Kurtulmuş via Digitalmars-d-learn
On Tuesday, 7 July 2020 at 00:35:38 UTC, Marcone wrote: Hi, I study Dlang for one year, and I can't understand alias this. I need an Easy example to understand Alias This. I used it for widget inheritance in my experimental code here, https://github.com/aferust/noobgui/blob/master/s

Re: I need an Easy example to understand Alias This

2020-07-07 Thread Cym13 via Digitalmars-d-learn
On Tuesday, 7 July 2020 at 00:44:32 UTC, Marcone wrote: On Tuesday, 7 July 2020 at 00:42:40 UTC, Ali Çehreli wrote: On 7/6/20 5:35 PM, Marcone wrote: Hi, I study Dlang for one year, and I can't understand alias this. I need an Easy example to understand Alias This. Is the following ex

Re: I need an Easy example to understand Alias This

2020-07-06 Thread Ali Çehreli via Digitalmars-d-learn
On 7/6/20 5:44 PM, Marcone wrote: On Tuesday, 7 July 2020 at 00:42:40 UTC, Ali Çehreli wrote: On 7/6/20 5:35 PM, Marcone wrote: Hi, I study Dlang for one year, and I can't understand alias this. I need an Easy example to understand Alias This. Is the following example useful?  

Re: I need an Easy example to understand Alias This

2020-07-06 Thread Ali Çehreli via Digitalmars-d-learn
On 7/6/20 5:35 PM, Marcone wrote: Hi, I study Dlang for one year, and I can't understand alias this. I need an Easy example to understand Alias This. Is the following example useful? http://ddili.org/ders/d.en/alias_this.html Ali

Re: I need an Easy example to understand Alias This

2020-07-06 Thread Marcone via Digitalmars-d-learn
On Tuesday, 7 July 2020 at 00:42:40 UTC, Ali Çehreli wrote: On 7/6/20 5:35 PM, Marcone wrote: Hi, I study Dlang for one year, and I can't understand alias this. I need an Easy example to understand Alias This. Is the following example useful? http://ddili.org/ders/d.en/alias_this.html

I need an Easy example to understand Alias This

2020-07-06 Thread Marcone via Digitalmars-d-learn
Hi, I study Dlang for one year, and I can't understand alias this. I need an Easy example to understand Alias This.

Re: alias this and initialisation

2020-05-25 Thread lalit.singhh via Digitalmars-d-learn
On Monday, 25 May 2020 at 01:35:47 UTC, Danni Coy wrote: can anybody tell me why struct S { int x; alias x this; } void test() { S s; s = 8; // this works S s = 8 // but this does not? }

Re: alias this and initialisation

2020-05-25 Thread Adam D. Ruppe via Digitalmars-d-learn
On Monday, 25 May 2020 at 01:35:47 UTC, Danni Coy wrote: s = 8; // this works S s = 8 // but this does not? } alias this only applies if you already have an object. Construction is too soon. You can add a constructor to make that work though.

Re: alias this and initialisation

2020-05-25 Thread Ali Çehreli via Digitalmars-d-learn
On 5/24/20 6:35 PM, Danni Coy wrote:> can anybody tell me why > > struct S > { > int x; > alias x this; > } > > void test() > { > S s; > s = 8; // this works > S s = 8 // but this does not? > } alias this is for implicit conver

alias this and initialisation

2020-05-24 Thread Danni Coy via Digitalmars-d-learn
can anybody tell me why struct S { int x; alias x this; } void test() { S s; s = 8; // this works S s = 8 // but this does not? }

Re: Trying to alias this a grapheme range + making it a forward range

2019-07-09 Thread aliak via Digitalmars-d-learn
On Monday, 8 July 2019 at 23:01:49 UTC, ag0aep6g wrote: On 08.07.19 23:55, aliak wrote: [...] `source.front` is a temporary `Grapheme` and you're calling `opSlice` on it. The documentation for `Grapheme.opSlice` warns: "Invalidates when this Grapheme leaves the scope, attempts to use it the

Re: Trying to alias this a grapheme range + making it a forward range

2019-07-08 Thread ag0aep6g via Digitalmars-d-learn
mory corruption." [1] So you can't return `source.front[]` from your `front`. You'll have to store the current `front` in your struct, I guess. Also, returning a fresh range on every `alias this` call is asking for trouble. This is an infinite loop: auto u = "hello&quo

Trying to alias this a grapheme range + making it a forward range

2019-07-08 Thread aliak via Digitalmars-d-learn
Problem 1: I'm trying to get a string to behave as a .byGrapheme range by default, but I can't figure out Grapheme. I'm trying to replicate this behavior: foreach (g; "hello".byGrapheme) { write(g[]); } In a custom type: struct ustring { string data; this(string data) {

Re: alias this and struct allocation

2019-05-06 Thread Adam D. Ruppe via Digitalmars-d-learn
On Monday, 6 May 2019 at 14:48:56 UTC, faissaloo wrote: misunderstood how allocation works when instantiating a struct that uses alias this: alias this has no effect on allocation at all. All it does is if x.y doesn't compile, it rewrites it to x.alias_this.y (or if f(x) doesn'

Re: alias this and struct allocation

2019-05-06 Thread Alex via Digitalmars-d-learn
On Monday, 6 May 2019 at 14:48:56 UTC, faissaloo wrote: I've been having some memory issues (referenced objects turning to nulls for no apparent reason) and I was wondering if I've misunderstood how allocation works when instantiating a struct that uses alias this: import

Re: alias this and struct allocation

2019-05-06 Thread faissaloo via Digitalmars-d-learn
On Monday, 6 May 2019 at 15:17:37 UTC, aliak wrote: Do you have an example of a referenced object turning to null? We may be able to spot something Unfortunately I haven't managed to produce an example any smaller than my entire codebase

Re: alias this and struct allocation

2019-05-06 Thread aliak via Digitalmars-d-learn
On Monday, 6 May 2019 at 14:48:56 UTC, faissaloo wrote: I've been having some memory issues (referenced objects turning to nulls for no apparent reason) and I was wondering if I've misunderstood how allocation works when instantiating a struct that uses alias this: import

alias this and struct allocation

2019-05-06 Thread faissaloo via Digitalmars-d-learn
I've been having some memory issues (referenced objects turning to nulls for no apparent reason) and I was wondering if I've misunderstood how allocation works when instantiating a struct that uses alias this: import std.stdio; struct Parent {

Re: difficulties with const structs and alias this / template functions

2018-11-19 Thread Stanislav Blinov via Digitalmars-d-learn
; of this, you can't stop it when it really doesn't make sense (as in this case). Sure. At first I was perplexed why Dennis' a /= 2 even compiled. Then I saw the alias this. I have long wanted a way to direct IFTI how to define its parameters base on the arguments. We have a simpl

Re: difficulties with const structs and alias this / template functions

2018-11-19 Thread Stanislav Blinov via Digitalmars-d-learn
On Monday, 19 November 2018 at 14:04:29 UTC, Dennis wrote: On Monday, 19 November 2018 at 13:34:50 UTC, Stanislav Blinov wrote: Because it's not mutation, it's initialization. Oh. That's an epiphany for me. :) When a ctor is `pure`, the compiler knows it doesn't mutate any state other

Re: difficulties with const structs and alias this / template functions

2018-11-19 Thread Steven Schveighoffer via Digitalmars-d-learn
On 11/18/18 1:17 PM, Stanislav Blinov wrote: On Sunday, 18 November 2018 at 17:30:18 UTC, Dennis wrote: I'm making a fixed point numeric type and want it to work correctly with const. First problem: ``` const q16 a = 6; a /= 2;  // compiles! despite `a` being const. Ouch. That's actu

Re: difficulties with const structs and alias this / template functions

2018-11-19 Thread Dennis via Digitalmars-d-learn
On Monday, 19 November 2018 at 13:34:50 UTC, Stanislav Blinov wrote: Because it's not mutation, it's initialization. Oh. That's an epiphany for me. When a ctor is `pure`, the compiler knows it doesn't mutate any state other than the object's, so it allows conversion to all three qualifie

Re: difficulties with const structs and alias this / template functions

2018-11-19 Thread Stanislav Blinov via Digitalmars-d-learn
On Monday, 19 November 2018 at 12:28:43 UTC, Dennis wrote: On Monday, 19 November 2018 at 02:39:32 UTC, Stanislav Blinov wrote: You're skimming the examples ;) I'm not saying your examples don't work, I'm trying to understand the minimum requirements. You said: "That's [constructors needing

Re: difficulties with const structs and alias this / template functions

2018-11-19 Thread Dennis via Digitalmars-d-learn
On Monday, 19 November 2018 at 02:39:32 UTC, Stanislav Blinov wrote: You're skimming the examples ;) I'm not saying your examples don't work, I'm trying to understand the minimum requirements. You said: "That's [constructors needing to be pure is] only for types with indirections (pointers)

Re: difficulties with const structs and alias this / template functions

2018-11-18 Thread Stanislav Blinov via Digitalmars-d-learn
On Monday, 19 November 2018 at 02:08:14 UTC, Dennis wrote: On Monday, 19 November 2018 at 01:24:02 UTC, Stanislav Blinov wrote: Yup, that's because, like Rubn said, copying value types is trivial. Where it all comes to bite you is when you start having pointers, because you can't copy a const(T

Re: difficulties with const structs and alias this / template functions

2018-11-18 Thread Stanislav Blinov via Digitalmars-d-learn
On Monday, 19 November 2018 at 02:03:18 UTC, Dennis wrote: On Monday, 19 November 2018 at 01:13:29 UTC, Stanislav Blinov wrote: You just dismissed that second to last sentence, did you? :) I don't know what you mean with it. It's not that I'm trying to be sneaky or lazy really trying to modif

Re: difficulties with const structs and alias this / template functions

2018-11-18 Thread Dennis via Digitalmars-d-learn
On Monday, 19 November 2018 at 01:24:02 UTC, Stanislav Blinov wrote: Yup, that's because, like Rubn said, copying value types is trivial. Where it all comes to bite you is when you start having pointers, because you can't copy a const(T)* into a T*. I'm not using reference types, but still: `

Re: difficulties with const structs and alias this / template functions

2018-11-18 Thread Dennis via Digitalmars-d-learn
On Monday, 19 November 2018 at 01:13:29 UTC, Stanislav Blinov wrote: You just dismissed that second to last sentence, did you? :) I don't know what you mean with it. It's not that I'm trying to be sneaky or lazy really trying to modify the const parameter when I should treat it properly. And

Re: difficulties with const structs and alias this / template functions

2018-11-18 Thread Stanislav Blinov via Digitalmars-d-learn
On Monday, 19 November 2018 at 00:50:28 UTC, Dennis wrote: I'm also trying to make it work with immutable, and from BigInt [2] I learned that constructors need to be `pure` for creating immutable objects. (I don't know why.) That's only for types with indirections (pointers), since `pure` gu

Re: difficulties with const structs and alias this / template functions

2018-11-18 Thread Stanislav Blinov via Digitalmars-d-learn
On Sunday, 18 November 2018 at 20:10:52 UTC, Dennis wrote: On Sunday, 18 November 2018 at 18:17:54 UTC, Stanislav Blinov wrote: Q log2(Q)(inout Q num) if (is(Q : q16) || is(Q : q32)) { /* ... */ } Being able to jam mutable/const/immutable implementation in one function like that should tell

Re: difficulties with const structs and alias this / template functions

2018-11-18 Thread Dennis via Digitalmars-d-learn
On Sunday, 18 November 2018 at 22:30:52 UTC, Rubn wrote: Yah most people tend to avoid const for this reason. It only really works for basic types, if you have a "const int" you can convert it to an "int" by copy. But if you have a type like Vector!(const int) that won't work, you can't even co

Re: difficulties with const structs and alias this / template functions

2018-11-18 Thread Rubn via Digitalmars-d-learn
On Sunday, 18 November 2018 at 17:30:18 UTC, Dennis wrote: I'm making a fixed point numeric type and want it to work correctly with const. First problem: ``` const q16 a = 6; a /= 2; // compiles! despite `a` being const. writeln(a); // still 6 a.toQ32 /= 2;// what's actually h

Re: difficulties with const structs and alias this / template functions

2018-11-18 Thread Dennis via Digitalmars-d-learn
On Sunday, 18 November 2018 at 18:17:54 UTC, Stanislav Blinov wrote: // implement separate methods for mutable/const/immutable Thanks. I should have tried that, but I assumed it wouldn't work since you can't overload on return-type only. However, the const / non-const makes it allowed

Re: difficulties with const structs and alias this / template functions

2018-11-18 Thread Stanislav Blinov via Digitalmars-d-learn
On Sunday, 18 November 2018 at 17:30:18 UTC, Dennis wrote: I'm making a fixed point numeric type and want it to work correctly with const. First problem: ``` const q16 a = 6; a /= 2; // compiles! despite `a` being const. Ouch. That's actually kind of nasty. writeln(a); // stil

difficulties with const structs and alias this / template functions

2018-11-18 Thread Dennis via Digitalmars-d-learn
I'm making a fixed point numeric type and want it to work correctly with const. First problem: ``` const q16 a = 6; a /= 2; // compiles! despite `a` being const. writeln(a); // still 6 a.toQ32 /= 2;// what's actually happening ``` My q16 type has an implicit conversion to q32

Re: How can I induce implicit type convesion with alias this on calling template function?

2018-10-15 Thread Sobaya via Digitalmars-d-learn
On Monday, 15 October 2018 at 06:16:34 UTC, Alex wrote: On Monday, 15 October 2018 at 04:51:39 UTC, Sobaya wrote: [...] Removing constraint, but retaining specialization should be enough, no? Then, func is still a template, requiring the argument to be convertible to an int. When S is passed

Re: How can I induce implicit type convesion with alias this on calling template function?

2018-10-14 Thread Alex via Digitalmars-d-learn
On Monday, 15 October 2018 at 04:51:39 UTC, Sobaya wrote: void func(T : int)(T value) if (is(T == int)) { } struct S { int x; alias x this; } void main() { func(S()); // error } In above code, 'func' can accept only int as its argument type, so when 'S', which can be implicitly co

How can I induce implicit type convesion with alias this on calling template function?

2018-10-14 Thread Sobaya via Digitalmars-d-learn
void func(T : int)(T value) if (is(T == int)) { } struct S { int x; alias x this; } void main() { func(S()); // error } In above code, 'func' can accept only int as its argument type, so when 'S', which can be implicitly convertible into int, is passed on 'func', I expect S.x is p

Re: Alias this and opDispatch override

2018-09-06 Thread Domain via Digitalmars-d-learn
On Friday, 7 September 2018 at 02:22:58 UTC, Domain wrote: The following code fail to compile: enum KeyMod : int { LCtrl = 1 << 0, RCtrl = 1 << 1, Ctrl = LCtrl | RCtrl, } struct Flags(E) { public: BitFlags!(E, Yes.unsafe) flags; alias flags this; bool opDispatch(str

Alias this and opDispatch override

2018-09-06 Thread Domain via Digitalmars-d-learn
The following code fail to compile: enum KeyMod : int { LCtrl = 1 << 0, RCtrl = 1 << 1, Ctrl = LCtrl | RCtrl, } struct Flags(E) { public: BitFlags!(E, Yes.unsafe) flags; alias flags this; bool opDispatch(string name)() const if (__traits(hasMember, E, name))

Re: Question about template argument matching with alias this

2018-07-30 Thread Alex via Digitalmars-d-learn
On Sunday, 29 July 2018 at 23:03:27 UTC, Johannes Loher wrote: Yeah, I know that it possible to implement the template like this, but that is not the point here. I would like to know why it does not work the way I described it. To me it seems very strange, that `S : T` has different semantics i

Re: Question about template argument matching with alias this

2018-07-29 Thread Johannes Loher via Digitalmars-d-learn
, MockedType)().existingInstance(myMock); /* ... */ } ``` The problem with this is that register has the signature described above, i.e. register(T, S : T)() and that the mock template from unit-threaded is actually implemented by a struct which is "alias this"ed (how do you call that.

Re: Question about template argument matching with alias this

2018-07-29 Thread Alex via Digitalmars-d-learn
On Sunday, 29 July 2018 at 16:43:08 UTC, Johannes Loher wrote: I have a question about template argument matching in combination with implicit conversion and alias this. Consider the following code: interface SomeInterface { } class SomeClass : SomeInterface { } struct SomeStruct

Question about template argument matching with alias this

2018-07-29 Thread Johannes Loher via Digitalmars-d-learn
I have a question about template argument matching in combination with implicit conversion and alias this. Consider the following code: interface SomeInterface { } class SomeClass : SomeInterface { } struct SomeStruct { SomeClass someClass; alias someClass this; } template

Re: Functor alias this

2018-06-05 Thread Simen Kjærås via Digitalmars-d-learn
eated. Alternatively I could just pass the function directly around without all the weight of the class. This led me to wonder if there is a way to combine the two methods? With D's alias this would it be possible to have the user code treat the function as a delegate but define the functions

Functor alias this

2018-06-05 Thread DaggetJones via Digitalmars-d-learn
round without all the weight of the class. This led me to wonder if there is a way to combine the two methods? With D's alias this would it be possible to have the user code treat the function as a delegate but define the functions actually in a class without any restrictions? import

Re: Get the type of 'alias this ='

2018-04-04 Thread Vladimirs Nordholm via Digitalmars-d-learn
On Wednesday, 4 April 2018 at 15:57:19 UTC, Simen Kjærås wrote: On Wednesday, 4 April 2018 at 15:49:31 UTC, Vladimirs Nordholm wrote: if (is(T == A) || is(T == B) || is(T == Enum)) if (is(T : Enum)) -- Simen Ah! Thanks a lot!

Re: Get the type of 'alias this ='

2018-04-04 Thread Vladimirs Nordholm via Digitalmars-d-learn
On Wednesday, 4 April 2018 at 15:49:31 UTC, Vladimirs Nordholm wrote: private template Mix() { this(Enum ee) { e = ee; } Enum e; alias this = e; } Oops, typo in the alias. Should be `alias e this`

Re: Get the type of 'alias this ='

2018-04-04 Thread Simen Kjærås via Digitalmars-d-learn
On Wednesday, 4 April 2018 at 15:49:31 UTC, Vladimirs Nordholm wrote: if (is(T == A) || is(T == B) || is(T == Enum)) if (is(T : Enum)) -- Simen

Get the type of 'alias this ='

2018-04-04 Thread Vladimirs Nordholm via Digitalmars-d-learn
Hello people from D-land. Short question: Can get the type of a struct that has `alias this = ` ? See this example, where a struct is aliased to an enum: enum Enum { one, two, three, fourtytwo } private template Mix() { this(Enum ee) { e = ee; } Enum e

Re: Trying to forward unwrapped opDispatch names to alias this

2018-02-20 Thread aliak via Digitalmars-d-learn
s that built in types don't have "members" per se, they have "magic". The built in properties don't count to `hasMember`. You could probably do `if(__traits(compiles, mixin("a." ~ member))` though. I assume this should work because rules for alias th

Re: Trying to forward unwrapped opDispatch names to alias this

2018-02-20 Thread aliak via Digitalmars-d-learn
On Tuesday, 20 February 2018 at 11:27:23 UTC, Alex wrote: There is a related ticket, https://issues.dlang.org/show_bug.cgi?id=6434 However, not exactly facing this question. Should that ticket be marked as resolved? The issue is for alias this to be considered before opDispatch but there were

Re: Trying to forward unwrapped opDispatch names to alias this

2018-02-20 Thread Adam D. Ruppe via Digitalmars-d-learn
hey have "magic". The built in properties don't count to `hasMember`. You could probably do `if(__traits(compiles, mixin("a." ~ member))` though. I assume this should work because rules for alias this (as I understand) are to basically try if there's a member name

  1   2   3   4   5   >