Jump to page: 1 2
Thread overview
Conflict between std.file write() and std.stdio write()
Oct 02, 2013
Craig Dillabaugh
Oct 02, 2013
Craig Dillabaugh
Oct 03, 2013
Jonathan M Davis
Oct 03, 2013
Craig Dillabaugh
Oct 03, 2013
Craig Dillabaugh
Oct 03, 2013
Andrej Mitrovic
Oct 03, 2013
Craig Dillabaugh
Oct 03, 2013
Jonathan M Davis
Oct 03, 2013
1100110
Oct 03, 2013
Craig Dillabaugh
Oct 03, 2013
bearophile
Oct 03, 2013
Jonathan M Davis
Oct 03, 2013
Craig Dillabaugh
Oct 03, 2013
Jonathan M Davis
Oct 04, 2013
Craig Dillabaugh
Oct 05, 2013
Jonathan M Davis
Oct 06, 2013
Craig Dillabaugh
October 02, 2013
Hello,
I have the following program:

import std.file;
import std.stdio;

void main( string[] args ) {
   string str = "Hello";
   write( "file.txt", str );

   string hello_file = readText("file.txt");

   writeln( hello_file );
}

When I try to compile this I get:

test.d(6): Error: std.stdio.write!(string, string).write at
/usr/include/dmd/phobos/std/stdio.d(1656) conflicts with
std.file.write at /usr/include/dmd/phobos/std/file.d(318)

I think this should work.  The example at the end of (D file I/0):

http://www.docwiki.net/view.php?pageid=145

Uses write() exactly the way I am using it here.

Cheers,

Craig
October 02, 2013
On Wednesday, 2 October 2013 at 23:39:39 UTC, Craig Dillabaugh
wrote:
> Hello,
> I have the following program:
>
> import std.file;
> import std.stdio;
>
> void main( string[] args ) {
>    string str = "Hello";
>    write( "file.txt", str );
>
>    string hello_file = readText("file.txt");
>
>    writeln( hello_file );
> }
>
> When I try to compile this I get:
>
> test.d(6): Error: std.stdio.write!(string, string).write at
> /usr/include/dmd/phobos/std/stdio.d(1656) conflicts with
> std.file.write at /usr/include/dmd/phobos/std/file.d(318)
>
> I think this should work.  The example at the end of (D file I/0):
>
> http://www.docwiki.net/view.php?pageid=145
>
> Uses write() exactly the way I am using it here.
>
> Cheers,
>
> Craig

D compiler DMD 2.063.2
October 03, 2013
On Thursday, October 03, 2013 01:39:38 Craig Dillabaugh wrote:
> Hello,
> I have the following program:
> 
> import std.file;
> import std.stdio;
> 
> void main( string[] args ) {
> string str = "Hello";
> write( "file.txt", str );
> 
> string hello_file = readText("file.txt");
> 
> writeln( hello_file );
> }
> 
> When I try to compile this I get:
> 
> test.d(6): Error: std.stdio.write!(string, string).write at
> /usr/include/dmd/phobos/std/stdio.d(1656) conflicts with
> std.file.write at /usr/include/dmd/phobos/std/file.d(318)
> 
> I think this should work. The example at the end of (D file I/0):
> 
> http://www.docwiki.net/view.php?pageid=145
> 
> Uses write() exactly the way I am using it here.

You have to give the full path - std.file.write. As both functions can take the same arguments, and you've imported both, the compiler has no way of knowing which you mean. So, you have to disambiguate for it. It's only a problem because you imported both modules.

- Jonathan M Davis
October 03, 2013
On Thursday, 3 October 2013 at 00:04:31 UTC, Jonathan M Davis
wrote:
> On Thursday, October 03, 2013 01:39:38 Craig Dillabaugh wrote:
>> Hello,
>> I have the following program:
>> 
>> import std.file;
>> import std.stdio;
>> 
>> void main( string[] args ) {
>> string str = "Hello";
>> write( "file.txt", str );
>> 
>> string hello_file = readText("file.txt");
>> 
>> writeln( hello_file );
>> }
>> 
>> When I try to compile this I get:
>> 
>> test.d(6): Error: std.stdio.write!(string, string).write at
>> /usr/include/dmd/phobos/std/stdio.d(1656) conflicts with
>> std.file.write at /usr/include/dmd/phobos/std/file.d(318)
>> 
>> I think this should work. The example at the end of (D file I/0):
>> 
>> http://www.docwiki.net/view.php?pageid=145
>> 
>> Uses write() exactly the way I am using it here.
>
> You have to give the full path - std.file.write. As both functions can take the
> same arguments, and you've imported both, the compiler has no way of knowing
> which you mean. So, you have to disambiguate for it. It's only a problem
> because you imported both modules.
>
> - Jonathan M Davis

Thanks.  Seems kind of an odd design decision (or oversight) that
two commonly used functions in the standard library would clash
in this manner, but I guess it is no big deal.

Cheers,

Craig

October 03, 2013
On Thursday, 3 October 2013 at 00:04:31 UTC, Jonathan M Davis
wrote:
> On Thursday, October 03, 2013 01:39:38 Craig Dillabaugh wrote:
>> Hello,
>> I have the following program:
>> 
>> import std.file;
>> import std.stdio;
>> 
>> void main( string[] args ) {
>> string str = "Hello";
>> write( "file.txt", str );
>> 
>> string hello_file = readText("file.txt");
>> 
>> writeln( hello_file );
>> }
>> 
>> When I try to compile this I get:
>> 
>> test.d(6): Error: std.stdio.write!(string, string).write at
>> /usr/include/dmd/phobos/std/stdio.d(1656) conflicts with
>> std.file.write at /usr/include/dmd/phobos/std/file.d(318)
>> 
>> I think this should work. The example at the end of (D file I/0):
>> 
>> http://www.docwiki.net/view.php?pageid=145
>> 
>> Uses write() exactly the way I am using it here.
>
> You have to give the full path - std.file.write. As both functions can take the
> same arguments, and you've imported both, the compiler has no way of knowing
> which you mean. So, you have to disambiguate for it. It's only a problem
> because you imported both modules.
>
> - Jonathan M Davis

Thanks.  Seems kind of an odd design decision (or oversight) that
two commonly used functions in the standard library would clash
in this manner, but I guess it is no big deal.

Cheers,

Craig

October 03, 2013
On 10/3/13, Craig Dillabaugh <cdillaba@cg.scs.carleton.ca> wrote:
> void main( string[] args ) {
>     string str = "Hello";
>     write( "file.txt", str );
>
>     string hello_file = readText("file.txt");
>
>     writeln( hello_file );
> }

You can also disambiguate by preferring one symbol over another with an alias:

alias write = std.file.write;

void main( string[] args ) {
    string str = "Hello";
    write( "file.txt", str );
}

You can also put the alias inside the function.
October 03, 2013
On Thursday, 3 October 2013 at 02:57:50 UTC, Andrej Mitrovic
wrote:
> On 10/3/13, Craig Dillabaugh <cdillaba@cg.scs.carleton.ca> wrote:
>> void main( string[] args ) {
>>     string str = "Hello";
>>     write( "file.txt", str );
>>
>>     string hello_file = readText("file.txt");
>>
>>     writeln( hello_file );
>> }
>
> You can also disambiguate by preferring one symbol over another with an alias:
>
> alias write = std.file.write;
>
> void main( string[] args ) {
>     string str = "Hello";
>     write( "file.txt", str );
> }
>
> You can also put the alias inside the function.

Thanks.

It seems that std.file should include a writeText() function for
the sake of consistency that is the above alias.  When you come
across readText() in the documentation you sort of expect that
such a function would exist, and then you spot write() below it,
and think hey that does what I need.  Then you hit upon the
syntax error if you are also using std.stdio (which is a very
commonly used module).

Adding writeText() doesn't really add much to the library, but
having to jump through hoops (as minor as they may be) to perform
such a simple op is a bit of a pain for people new to the
language.

Craig
October 03, 2013
On Thursday, October 03, 2013 15:22:28 Craig Dillabaugh wrote:
> It seems that std.file should include a writeText() function for
> the sake of consistency that is the above alias. When you come
> across readText() in the documentation you sort of expect that
> such a function would exist, and then you spot write() below it,
> and think hey that does what I need. Then you hit upon the
> syntax error if you are also using std.stdio (which is a very
> commonly used module).
> 
> Adding writeText() doesn't really add much to the library, but
> having to jump through hoops (as minor as they may be) to perform
> such a simple op is a bit of a pain for people new to the
> language.

writeText would be redundant. write will already write arbitrary data to a file - including strings. writeText would add no functionality. Functions should add actual value, or they just clutter up the library.

Conflicting functions is just part of life. The module system is designed to let you disambiguate them. We're not going to try and make all of the function names unique just because you might import a module with a conflicting function. If write is the best name for the function, then that's what we'll use, even if it conflicts with a function in another module. To do otherwise would be to ignore the features of the module system and force us to come up with worse names just to avoid conflicts.

- Jonathan M Davis
October 03, 2013
On 10/03/2013 01:11 PM, Jonathan M Davis wrote:
> On Thursday, October 03, 2013 15:22:28 Craig Dillabaugh wrote:
>> It seems that std.file should include a writeText() function for
>> the sake of consistency that is the above alias. When you come
>> across readText() in the documentation you sort of expect that
>> such a function would exist, and then you spot write() below it,
>> and think hey that does what I need. Then you hit upon the
>> syntax error if you are also using std.stdio (which is a very
>> commonly used module).
>>
>> Adding writeText() doesn't really add much to the library, but
>> having to jump through hoops (as minor as they may be) to perform
>> such a simple op is a bit of a pain for people new to the
>> language.
>
> writeText would be redundant. write will already write arbitrary data to a file
> - including strings. writeText would add no functionality. Functions should
> add actual value, or they just clutter up the library.
>
> Conflicting functions is just part of life. The module system is designed to
> let you disambiguate them. We're not going to try and make all of the function
> names unique just because you might import a module with a conflicting
> function. If write is the best name for the function, then that's what we'll
> use, even if it conflicts with a function in another module. To do otherwise
> would be to ignore the features of the module system and force us to come up
> with worse names just to avoid conflicts.
>
> - Jonathan M Davis
>

I *like* the fact I only have to remember one API.
A simple alias or disambiguation every once in a while is well worth it IMHO.
October 03, 2013
On Thursday, 3 October 2013 at 18:12:01 UTC, Jonathan M Davis wrote:
> On Thursday, October 03, 2013 15:22:28 Craig Dillabaugh wrote:
>> It seems that std.file should include a writeText() function for
>> the sake of consistency that is the above alias. When you come
>> across readText() in the documentation you sort of expect that
>> such a function would exist, and then you spot write() below it,
>> and think hey that does what I need. Then you hit upon the
>> syntax error if you are also using std.stdio (which is a very
>> commonly used module).
>> 
>> Adding writeText() doesn't really add much to the library, but
>> having to jump through hoops (as minor as they may be) to perform
>> such a simple op is a bit of a pain for people new to the
>> language.
>
> writeText would be redundant. write will already write arbitrary data to a file
> - including strings. writeText would add no functionality. Functions should
> add actual value, or they just clutter up the library.
>
> Conflicting functions is just part of life. The module system is designed to
> let you disambiguate them. We're not going to try and make all of the function
> names unique just because you might import a module with a conflicting
> function. If write is the best name for the function, then that's what we'll
> use, even if it conflicts with a function in another module. To do otherwise
> would be to ignore the features of the module system and force us to come up
> with worse names just to avoid conflicts.
>
> - Jonathan M Davis

Fair enough. As you point out the fix is pretty simple.

However, I can't seem to remember in C++ or any other language (not that I know all that many other languages) coming across a function in the standard library that conflicted with another function in the standard library in this way.  I am likely to get corrected on that claim though :o)

Maybe it would be worth noting this conflict in the documentations for newbies.

Craig
« First   ‹ Prev
1 2