www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - My ignorance, or a bug - help please

reply Steve Teale <steve.teale britseyeview.com> writes:
I was converting something that was a class to a struct, and ran into this
(minimized as much as possible):

import mysql;
// The import mysql.d for the test contains only:
/*
extern (C):
struct MYSQL{}
MYSQL* mysql_init(MYSQL* mysql);
*/
// If I put this stuff in-line the error does not happen

struct Connection
{
   MYSQL _mysql;

   ~this()
   {
   }

   this(int dummy = 0)
   {
      mysql_init(&_mysql);
   }
}

// dmd -c mysqld.d (2.055 - Ubuntu)
//
// Error: can only initialize const member _mysql inside constructor
// Error: this is not mutable

Does anyone have any idea what's going on?

Thanks
Steve
Oct 07 2011
next sibling parent mta`chrono <chrono mta-international.net> writes:
-------------- mysql.d ----------------
extern (C):
struct MYSQL{}
MYSQL* mysql_init(MYSQL* mysql);


-------------- main.d -----------------
import mysql;

struct Connection
{
   MYSQL _mysql;

   ~this()
   {
   }

   this(int dummy = 0)
   {
      mysql_init(&_mysql);
   }
}

void main()
{
    Connection con1;
    Connection *con2 = new Connection(123);

}


-------------------------------------

dmd main.d mysql.d -L-lmysqlclient


works with DMD64 D Compiler v2.056
Oct 07 2011
prev sibling next sibling parent travert phare.normalesup.org (Christophe) writes:
Steve Teale , dans le message (digitalmars.D:146210), a écrit :
 I was converting something that was a class to a struct, and ran into this
 (minimized as much as possible):
 
 import mysql;
 // The import mysql.d for the test contains only:
 /*
 extern (C):
 struct MYSQL{}
 MYSQL* mysql_init(MYSQL* mysql);
 */
 // If I put this stuff in-line the error does not happen
 
 struct Connection
 {
    MYSQL _mysql;
 
    ~this()
    {
    }
 
    this(int dummy = 0)
    {
       mysql_init(&_mysql);
    }
 }
 
 // dmd -c mysqld.d (2.055 - Ubuntu)
 //
 // Error: can only initialize const member _mysql inside constructor
 // Error: this is not mutable
 
 Does anyone have any idea what's going on?
I don't know what is going one, but putting a MYSQL object in Connection after having defined MYSQL as a dummy empty extern C struct does not seem to be a good idea. -- Christophe
Oct 07 2011
prev sibling parent reply mta`chrono <chrono mta-international.net> writes:
If you don't need the internal data of struct MYSQL and you don't want
to care about. Then just keep some reference in your program.


alias void MYSQL;
alias void MYSQL_RES;

and then only use MYSQL* and pass it to every function.


struct Connection
{
   MYSQL* _mysql;

   ~this()
   {
   }

   this(int dummy = 0)
   {
      mysql_init(_mysql);
   }
}
Oct 07 2011
next sibling parent Steve Teale <steve.teale britseyeview.com> writes:
The empty struct is a red herring. I cut mysql.d down to the bare minimum to
help
isolate the bug.

I get exactly the same error with the complete mysql.d
Oct 07 2011
prev sibling parent Jacob Carlborg <doob me.com> writes:
On 2011-10-07 11:55, mta`chrono wrote:
 If you don't need the internal data of struct MYSQL and you don't want
 to care about. Then just keep some reference in your program.


 alias void MYSQL;
 alias void MYSQL_RES;
The correct way to do this is to declare an opaque struct, just as in C: struct MYSQL; -- /Jacob Carlborg
Oct 07 2011