www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why is this code returning the wrong type?

reply "Gary Willoughby" <dev kalekold.net> writes:
Why won't the following code compile? Here's the error:

filewatcher.d(21): Error: cannot implicitly convert expression 
(new File(file, "r")) of type File* to shared(_iobuf)*

/**
  * Imports.
  */
import std.stdio;

/**
  * A class to watch for changes in a file.
  */
class Example
{
	/**
	 * Member variables.
	 */
	private FILE* _file;

	/**
	 * Constructor.
	 */
	public this(string file)
	{
		this._file = new File(file, "r");
	}
}
May 23 2013
next sibling parent David <d dav1d.de> writes:
Am 23.05.2013 18:27, schrieb Gary Willoughby:
 Why won't the following code compile? Here's the error:
 
 filewatcher.d(21): Error: cannot implicitly convert expression (new
 File(file, "r")) of type File* to shared(_iobuf)*
 
 /**
  * Imports.
  */
 import std.stdio;
 
 /**
  * A class to watch for changes in a file.
  */
 class Example
 {
     /**
      * Member variables.
      */
     private FILE* _file;
 
     /**
      * Constructor.
      */
     public this(string file)
     {
         this._file = new File(file, "r");
     }
 }
Because that's not the same: File != FILE you probably want the .handle
May 23 2013
prev sibling parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
On Thursday, 23 May 2013 at 16:27:19 UTC, Gary Willoughby wrote:
 Why won't the following code compile? Here's the error:

 filewatcher.d(21): Error: cannot implicitly convert expression 
 (new File(file, "r")) of type File* to shared(_iobuf)*

 /**
  * Imports.
  */
 import std.stdio;

 /**
  * A class to watch for changes in a file.
  */
 class Example
 {
 	/**
 	 * Member variables.
 	 */
 	private FILE* _file;

 	/**
 	 * Constructor.
 	 */
 	public this(string file)
 	{
 		this._file = new File(file, "r");
 	}
 }
/** * Imports. */ import std.stdio; /** * A class to watch for changes in a file. */ class Example { /** * Member variables. */ private File _file; /** * Constructor. */ public this(string file) { _file = File(file, "r"); } } File is a wrapper around a FILE*, it's not the same as a FILE* No need for new, File is a struct, new is (normally) for classes. No need for "this.", although there's no harm in it.
May 23 2013
next sibling parent "Gary Willoughby" <dev kalekold.net> writes:
 File is a wrapper around a FILE*, it's not the same as a FILE*

 No need for new, File is a struct, new is (normally) for 
 classes. No need for "this.", although there's no harm in it.
Ah yes, thanks.
May 23 2013
prev sibling parent reply "Gary Willoughby" <dev kalekold.net> writes:
Hmmm... Following your example i'm still having problems 
compiling this simple snippet:

import std.stdio;

class Example
{
	private FILE _file;

	public this(string file)
	{
		this._file = File(file, "r");
	}
}

Error:

test.d(9): Error: cannot implicitly convert expression ((File 
__ctmp1220 = 0;
  , __ctmp1220).this(file, "r")) of type File to shared(_iobuf)
May 23 2013
parent reply dennis luehring <dl.soluz gmx.net> writes:
Am 23.05.2013 21:45, schrieb Gary Willoughby:
 Hmmm... Following your example i'm still having problems
 compiling this simple snippet:

 import std.stdio;

 class Example
 {
 	private FILE _file;

 	public this(string file)
 	{
 		this._file = File(file, "r");
 	}
 }

 Error:

 test.d(9): Error: cannot implicitly convert expression ((File
 __ctmp1220 = 0;
    , __ctmp1220).this(file, "r")) of type File to shared(_iobuf)
you former private FILE* _file wasn't an File and your current private FILE _file is still not File because FILE and File is something differnt (case sensitive) why not write private File _file
May 23 2013
parent "Gary Willoughby" <dev kalekold.net> writes:
 you former

 private FILE* _file wasn't an File

 and your current

 private FILE _file is still not File

 because FILE and File is something differnt (case sensitive)

 why not write

 private File _file
Gah! of course. Thanks. I think i better get some sleep...
May 23 2013