www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - functions cannot be const or auto

reply popka <popka dodgit.com> writes:
Hi all,

I'm new to D and I've encountered an error message which is confusing me. I am
trying to write a 'const' member function, i.e. one which does not modify the
object.

If I understand correctly, the D syntax for this is:

class Foo {                                                                    
                                                                               
         
  const void f() {}                                                            
                                                                               
         
}                                                                              
                                                                               
        

However, this gives me the following error:

test.d(2): function test.Foo.f functions cannot be const or auto

I have tried this using both DMD and GDC. What am I doing wrong?

Many thanks.
Dec 13 2007
next sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"popka" <popka dodgit.com> wrote in message 
news:fjrshb$1j2j$1 digitalmars.com...
 Hi all,

 I'm new to D and I've encountered an error message which is confusing me. 
 I am trying to write a 'const' member function, i.e. one which does not 
 modify the object.

 If I understand correctly, the D syntax for this is:

 class Foo {
  const void f() {}
 }

 However, this gives me the following error:

 test.d(2): function test.Foo.f functions cannot be const or auto

 I have tried this using both DMD and GDC. What am I doing wrong?

You're using the D1 compiler, not the D2 compiler. Only D2 has constness. As far as I know there is no D2-compatible GDC, only DMD.
Dec 13 2007
parent reply "Janice Caron" <caron800 googlemail.com> writes:
On 12/13/07, Jarrett Billingsley <kb3ctd2 yahoo.com> wrote:
 Only D2 has constness.

D1 still has the "const" keyword, which I'm sure is what's confusing popka. In D1, the const keyword is used for invariant data. (I'm not making this up! :-) ) I guess Walter was thinking ahead. Anyway, I'm sure that the real problem here is - as /many/ people have pointed out - the default documentation is for D2, but the default download is D1. That's gotta confuse /any/ visitor. (It confused me!)
Dec 13 2007
parent reply popka <popka dodgit.com> writes:
Janice Caron Wrote:

 On 12/13/07, Jarrett Billingsley <kb3ctd2 yahoo.com> wrote:
 Only D2 has constness.

D1 still has the "const" keyword, which I'm sure is what's confusing popka. In D1, the const keyword is used for invariant data. (I'm not making this up! :-) ) I guess Walter was thinking ahead. Anyway, I'm sure that the real problem here is - as /many/ people have pointed out - the default documentation is for D2, but the default download is D1. That's gotta confuse /any/ visitor. (It confused me!)

Thanks, both of you. I'll continue using version 1 without this feature as it seems Derelict does not support 2.0.
Dec 14 2007
parent Pedro Ferreira <ara sdf.asd> writes:
popka Wrote:

 Janice Caron Wrote:
 
 On 12/13/07, Jarrett Billingsley <kb3ctd2 yahoo.com> wrote:
 Only D2 has constness.

D1 still has the "const" keyword, which I'm sure is what's confusing popka. In D1, the const keyword is used for invariant data. (I'm not making this up! :-) ) I guess Walter was thinking ahead. Anyway, I'm sure that the real problem here is - as /many/ people have pointed out - the default documentation is for D2, but the default download is D1. That's gotta confuse /any/ visitor. (It confused me!)

Thanks, both of you. I'll continue using version 1 without this feature as it seems Derelict does not support 2.0.

Wrong. Check the forums, someone has already ported Derelict to DMD 2.
Dec 14 2007
prev sibling parent "Janice Caron" <caron800 googlemail.com> writes:
On 12/13/07, popka <popka dodgit.com> wrote:
 test.d(2): function test.Foo.f functions cannot be const or auto

 I have tried this using both DMD and GDC. What am I doing wrong?

If I had to guess, I'd say you're probably using D version 1, wheras const proper is only available in D version 2.
Dec 13 2007