Why is this code returning the wrong type?

John Colvin john.loughran.colvin at gmail.com
Thu May 23 09:33:27 PDT 2013


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.


More information about the Digitalmars-d-learn mailing list