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 su

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

2021-06-30 Thread jfondren 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 su

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

2021-06-30 Thread H. S. Teoh via Digitalmars-d-learn
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 sure which is the correct way. [...] Both are correct. :-) It's

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

2021-06-30 Thread someone via Digitalmars-d-learn
On Wednesday, 30 June 2021 at 18:10:52 UTC, Alexandru Ermicioi 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. So this will make sense ONLY for an object's method right ?

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

2021-06-30 Thread H. S. Teoh via Digitalmars-d-learn
On Wed, Jun 30, 2021 at 05:47:05PM +, someone via Digitalmars-d-learn wrote: [...] > ```d > public string getAmountSI( >in float lnumAmount >) const { [...] > } > ``` > > I used to put all attributes BEFORE the function name which now I > understand is completely wrong since they shoul

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.

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

2021-06-30 Thread someone via Digitalmars-d-learn
I do not understand the compiler error when I add the const keyword in the following function which works (and compiles) as expected without the const keyword: ```d public string getAmountSI( in float lnumAmount ) const { /// (1) given amount string lstrAmount; if (lnumAmount

Re: float price; if (price == float.nan) { // initialized } else { // uninitialized } ... valid ?

2021-06-30 Thread someone via Digitalmars-d-learn
On Wednesday, 30 June 2021 at 16:24:38 UTC, Andre Pany wrote: Side note: in case you want to work with money, you may consider using a specific data type like https://code.dlang.org/packages/money instead of float/double. Yes, I've seen it, and in a previous-unrelated post I commented I am p

Re: float price; if (price == float.nan) { // initialized } else { // uninitialized } ... valid ?

2021-06-30 Thread Andre Pany via Digitalmars-d-learn
On Wednesday, 30 June 2021 at 03:15:46 UTC, someone wrote: Is the following code block valid ? ```d float price; /// initialized as float.nan by default ... right ? if (price == float.nan) { /// writeln("initialized"); } else { /// writeln("uninitialized"); } ``` if so, the following

Re: float price; if (price == float.nan) { // initialized } else { // uninitialized } ... valid ?

2021-06-30 Thread someone via Digitalmars-d-learn
On Wednesday, 30 June 2021 at 10:38:05 UTC, jmh530 wrote: You've never given something away for free? ... more often than usual LoL Now, seriously, something for free has not a price = 0, it has NO price, that's what null is for; we use zero for the lack of null.

Re: float price; if (price == float.nan) { // initialized } else { // uninitialized } ... valid ?

2021-06-30 Thread someone via Digitalmars-d-learn
On Wednesday, 30 June 2021 at 09:36:34 UTC, Dennis wrote: A `string` is not a class but an array, an `immutable(char)[]`. You're right. My fault.

Re: float price; if (price == float.nan) { // initialized } else { // uninitialized } ... valid ?

2021-06-30 Thread jmh530 via Digitalmars-d-learn
On Wednesday, 30 June 2021 at 04:17:19 UTC, someone wrote: On Wednesday, 30 June 2021 at 03:55:05 UTC, Vladimir Panteleev wrote: If you want to give any type a "null" value, you could use [`std.typecons.Nullable`](https://dlang.org/library/std/typecons/nullable.html). At LEAST for some thing

Re: float price; if (price == float.nan) { // initialized } else { // uninitialized } ... valid ?

2021-06-30 Thread Dennis via Digitalmars-d-learn
On Wednesday, 30 June 2021 at 03:52:51 UTC, someone wrote: at least I can do nulls with strings since it a class :) A `string` is not a class but an array, an `immutable(char)[]`. For arrays, `null` is equal to an empty array `[]`. ```D void main() { string s0 = null; string s1 = [];