www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Is this intended behavior?

reply Jacob Carlborg <doobnet gmail.com> writes:
Is this intended behavior?

class id
{
     public int id;

     public this ()
     {
     }

     public this (int id)
     {
         this.id = id;
     }
}

class NSObject : id
{
     public this ()
     {
     }

     public this (int id)
     {
         this.id = id;
     }

     id foo () // error here
     {
         return new id(3);
     }
}

The compiler says: "Error: id is used as a type"
using the gdc bundle from the tango website on osx.
Apparently the compiler thinks the return type "id" refers to the member 
variable instead of the class.
Aug 10 2008
next sibling parent reply downs <default_357-line yahoo.de> writes:
Try .id
Aug 10 2008
parent reply Jacob Carlborg <doobnet gmail.com> writes:
downs wrote:
 Try .id
Yeah that works but I'm porting a library from Java to D so it breaks the interface and I have to replace every "id" with ".id". For now I have changed the member variable to "id_" then I can do a global search and replace for "this.id" and replace to "this.id_". I've solved my problem but I was wondering if this was the intended behavior or a bug.
Aug 10 2008
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Jacob Carlborg" <doobnet gmail.com> wrote in message 
news:g7mqd2$2rab$1 digitalmars.com...
 downs wrote:
 Try .id
Yeah that works but I'm porting a library from Java to D so it breaks the interface and I have to replace every "id" with ".id". For now I have changed the member variable to "id_" then I can do a global search and replace for "this.id" and replace to "this.id_". I've solved my problem but I was wondering if this was the intended behavior or a bug.
It's intended behavior. Name lookup happens from the innermost scope outwards, and does not depend upon how the name is used. Since 'id' is a member of the class, it finds that first, rather than the class 'id'. Maybe Java keeps looking for another name 'id' that can be used as a type?
Aug 10 2008
parent Jacob Carlborg <doobnet gmail.com> writes:
Jarrett Billingsley wrote:
 "Jacob Carlborg" <doobnet gmail.com> wrote in message 
 news:g7mqd2$2rab$1 digitalmars.com...
 downs wrote:
 Try .id
Yeah that works but I'm porting a library from Java to D so it breaks the interface and I have to replace every "id" with ".id". For now I have changed the member variable to "id_" then I can do a global search and replace for "this.id" and replace to "this.id_". I've solved my problem but I was wondering if this was the intended behavior or a bug.
It's intended behavior. Name lookup happens from the innermost scope outwards, and does not depend upon how the name is used. Since 'id' is a member of the class, it finds that first, rather than the class 'id'. Maybe Java keeps looking for another name 'id' that can be used as a type?
Tanks for that answer.
Aug 10 2008
prev sibling parent reply Auria <auria.mg gmail.com> writes:
Jacob Carlborg Wrote:

 Is this intended behavior?
 
 class id
 {
      public int id;
 
      public this ()
      {
      }
 
      public this (int id)
      {
          this.id = id;
      }
 }
 
 class NSObject : id
 {
      public this ()
      {
      }
 
      public this (int id)
      {
          this.id = id;
      }
 
      id foo () // error here
      {
          return new id(3);
      }
 }
 
 The compiler says: "Error: id is used as a type"
 using the gdc bundle from the tango website on osx.
 Apparently the compiler thinks the return type "id" refers to the member 
 variable instead of the class.
I would say it's not a good diea to name both a class and its member variable the same name. (nyway, the convention is usually that class names begin with an upper case letter IIRC) Even if it was allowed, i'd discourage it, your use of "id" is very confusing, it's both a class and a variable
Aug 10 2008
parent reply Jacob Carlborg <doobnet gmail.com> writes:
Auria wrote:
 Jacob Carlborg Wrote:
 
 Is this intended behavior?

 class id
 {
      public int id;

      public this ()
      {
      }

      public this (int id)
      {
          this.id = id;
      }
 }

 class NSObject : id
 {
      public this ()
      {
      }

      public this (int id)
      {
          this.id = id;
      }

      id foo () // error here
      {
          return new id(3);
      }
 }

 The compiler says: "Error: id is used as a type"
 using the gdc bundle from the tango website on osx.
 Apparently the compiler thinks the return type "id" refers to the member 
 variable instead of the class.
I would say it's not a good diea to name both a class and its member variable the same name. (nyway, the convention is usually that class names begin with an upper case letter IIRC) Even if it was allowed, i'd discourage it, your use of "id" is very confusing, it's both a class and a variable
I know that the convention is that class names should begin with an uppercase letter and it's not very good to name a member variable the same as the class name but I have not created the library I'm just porting it. In this case "id" refers to the type "id" in Objective-c
Aug 10 2008
parent reply Michel Fortin <michel.fortin michelf.com> writes:
On 2008-08-10 16:54:57 -0400, Jacob Carlborg <doobnet gmail.com> said:

 I know that the convention is that class names should begin with an 
 uppercase letter and it's not very good to name a member variable the 
 same as the class name but I have not created the library I'm just 
 porting it. In this case "id" refers to the type "id" in Objective-c 

What
 
kind of Java library is that you're porting? By the way, in my D/Objective-C bridge, I'm using the root Object class in D as an equivalent to to the Objective-C id type, and it allows any D object to be passed to Objective-C code. <http://michelf.com/projects/d-objc-bridge/> -- Michel Fortin michel.fortin michelf.com http://michelf.com/
Aug 10 2008
parent Jacob Carlborg <doobnet gmail.com> writes:
Michel Fortin wrote:
 On 2008-08-10 16:54:57 -0400, Jacob Carlborg <doobnet gmail.com> said:
 
 I know that the convention is that class names should begin with an 
 uppercase letter and it's not very good to name a member variable the 
 same as the class name but I have not created the library I'm just 
 porting it. In this case "id" refers to the type "id" in Objective-c 

TP30001163-CH11-SW3 
What

 kind of Java library is that you're porting?
 
 By the way, in my D/Objective-C bridge, I'm using the root Object class 
 in D as an equivalent to to the Objective-C id type, and it allows any D 
 object to be passed to Objective-C code.
 <http://michelf.com/projects/d-objc-bridge/>
 
 
SWT Cocoa, http://www.eclipse.org/swt/
Aug 11 2008