Re: Error: function `...` without `this` cannot be `const`
On Thursday, 1 July 2021 at 19:15:50 UTC, Johan Lermer wrote: Isn't @property kind of deprecated? The docs state "experimental" not deprecated ... but what this means today I don't know.
Re: Error: function `...` without `this` cannot be `const`
On Wednesday, 30 June 2021 at 19:40:40 UTC, someone wrote: 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; } Isn't @property kind of deprecated?
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: function without "this" cannot be const?
On Monday, 21 December 2015 at 02:03:14 UTC, Shriramana Sharma wrote: I'm trying to interface to a C function: extern(C) const char * textAttrN(const char * specString, size_t n); and getting the error: Error: function .textAttrN without 'this' cannot be const Please advise as to what I'm doing wrong?! :-( without the parens, 'const' means that the function doesn't mutate the state of the object or of the struct it's declared in. So it's meaningless for a global function. To avoid the confusion, take the habit to put 'const' at the right of the function declaration when it's related to a member function and only to the left when it's related to the type, and then to the left <=> always with parens.
Re: function without "this" cannot be const?
Basile B. wrote: > without the parens, 'const' means that the function doesn't > mutate the state of the object or of the struct it's declared in. > So it's meaningless for a global function. Thank you people. -- Shriramana Sharma, Penguin #395953
function without "this" cannot be const?
I'm trying to interface to a C function: extern(C) const char * textAttrN(const char * specString, size_t n); and getting the error: Error: function .textAttrN without 'this' cannot be const Please advise as to what I'm doing wrong?! :-( -- Shriramana Sharma, Penguin #395953
Re: function without "this" cannot be const?
On Monday, 21 December 2015 at 02:03:14 UTC, Shriramana Sharma wrote: I'm trying to interface to a C function: extern(C) const char * textAttrN(const char * specString, size_t n); and getting the error: Error: function .textAttrN without 'this' cannot be const Please advise as to what I'm doing wrong?! :-( Change it to: extern(C) const(char)* textAttrN(const char * specString, size_t n); ^^