www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Error: function `...` without `this` cannot be `const`

reply someone <someone somewhere.com> writes:
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.
Jun 30
next sibling parent reply Alexandru Ermicioi <alexandru.ermicioi gmail.com> writes:
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.
Jun 30
parent reply someone <someone somewhere.com> writes:
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
Jun 30
next sibling parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Wed, Jun 30, 2021 at 07:40:40PM +0000, 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.
Jun 30
next sibling parent jfondren <julian.fondren gmail.com> writes:
On Wednesday, 30 June 2021 at 20:12:29 UTC, H. S. Teoh wrote:
 On Wed, Jun 30, 2021 at 07:40:40PM +0000, 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.
Jun 30
prev sibling parent Alexandru Ermicioi <alexandru.ermicioi gmail.com> writes:
On Wednesday, 30 June 2021 at 20:12:29 UTC, H. S. Teoh wrote:
 On Wed, Jun 30, 2021 at 07:40:40PM +0000, 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.
Jun 30
prev sibling parent reply Johan Lermer <johann.lermer elvin.eu> writes:
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?
Jul 01
parent someone <someone somewhere.com> writes:
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.
Jul 01
prev sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Wed, Jun 30, 2021 at 05:47:05PM +0000, 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.
Jun 30