Re: Error: function `...` without `this` cannot be `const`
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 sure which is the correct way. [...] Both are correct. :-) It's up to personal style preference. T Just to remark here, if you want to apply const to a return type put it inside brackets like: const(MyClass) foo(); otherwise compiler will try to apply it to 'this' parameter. Best regards, Alexandru.
Re: Error: function `...` without `this` cannot be `const`
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 sure which is the correct way. [...] Both are correct. :-) It's up to personal style preference. T @nogc unittest { } // works unittest @nogc { } // doesn't work https://dlang.org/dstyle.html doesn't suggest either way, but does suggest alphabetical ordering(!) which I hadn't noticed.
Re: Error: function `...` without `this` cannot be `const`
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 up to personal style preference. T -- 2+2=4. 2*2=4. 2^2=4. Therefore, +, *, and ^ are the same operation.
Re: Error: function `...` without `this` cannot be `const`
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 ? It doesn't make sense to have a const modifier on a simple function. What will that const mean then in context of that function? To what it will be applied? I think of all the code I have I choose the worst example for asking advice on attribute placement ! Let's use @property instead of const: ```d struct Foo { @property int data() { return m_data; } // read property @property int data(int value) { return m_data = value; } // write property private: int m_data; } ``` In the above example (https://dlang.org/spec/function.html#property-functions) @property is placed before the return type of the property and not after the parameters section. At first I started to do the same, say, by intuition, but then I saw lots of examples like: ```d string something() @property { return this.whatever; } ``` ... and changed them accordingly. Now I am not sure which is the correct way. Thanks alexandri
Re: Error: function `...` without `this` cannot be `const`
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 should follow the parameter > declaration section because putting them before affects the function > in other ways. The `const` here is being applied to the implicit `this` parameter to your function. If your function is not a member function (does not have an implicit `this` parameter), then the attribute is meaningless. T -- Give me some fresh salted fish, please.
Re: Error: function `...` without `this` cannot be `const`
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. What will that const mean then in context of that function? To what it will be applied? Best regards, alexandri.
Error: function `...` without `this` cannot be `const`
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 < 1_000f) { lstrAmount = r"1K"c; } else { if (lnumAmount < 1_000_000f) { lstrAmount = format(r"%.0fK"c, lnumAmount / 1_000f); } else { lstrAmount = format(r"%.0fM"c, lnumAmount / 1_000_000f); } } return lstrAmount; } ``` I used to put all attributes BEFORE the function name which now I understand is completely wrong since they should follow the parameter declaration section because putting them before affects the function in other ways. I first noted this while browsing the DUB package DB and came accross https://code.dlang.org/packages/dscanner which states (among a lot of checks): - Placement of const, immutable, or inout before a function return type instead of after the parameters Is it because const for a function is intended to be used ONLY within a structure/class (ie: a method) and thus the this error (http://ddili.org/ders/d.en/const_member_functions.html) ? Can anyone explain please ? PS: I think I should re-check all the code I've written so far for things like this that obviously I quite not completely understand yet.
Re: float price; if (price == float.nan) { // initialized } else { // uninitialized } ... valid ?
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 planning to use it (or something similar) because floats and currency are a horrible combo. I am not using it right now because I want to learn the language and encountering situations like this one helps me a lot, otherwise, I would have never noted such NaN behavior -to me, there are a lots of things that could fly under the radar at this moment. And by the way, looking at the code, money seems a quite simple non-nonsense implementation making it a solid candidate :) Thanks for the tip Andre !
Re: float price; if (price == float.nan) { // initialized } else { // uninitialized } ... valid ?
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 one should be valid too ... right ? ```d float price; if (price != float.nan) { /// writeln("initialized"); } ``` 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. Kind regards Andre
Re: float price; if (price == float.nan) { // initialized } else { // uninitialized } ... valid ?
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 ?
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 ?
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 things with currency types like prices which cannot be zero because 0 makes no sense for a price: [snip] You've never given something away for free?
Re: float price; if (price == float.nan) { // initialized } else { // uninitialized } ... valid ?
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 = []; assert(s0 == s1); assert(s0.length == 0); // no null dereference here } ```