Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Ticket #600 (closed wishlist: fixed)

Opened 17 years ago

Last modified 17 years ago

make some commonly used features more accessible (File.read, FilePath.exists)

Reported by: CyberShadow Assigned to: kris
Priority: normal Milestone: 1.0
Component: Core Functionality Version: trunk
Keywords: Cc:

Description

This is a suggestion to make common operations more accessible.

For example, Tango's equivalent of Phobos's std.file.read (function) is tango.io.File.read (class method). This means that to read a file, you must instantiate a File object. In most cases, this leads to unnecessarily bloated or ugly code :( Compare:

Phobos:

import std.file;
...
auto data = read("mydata");

Tango:

import tango.io.file.File;
...
auto data = (new File("mydata")).read;
// or
void[] data;
with (new File("mydata"))
    data = read();
// or
File f = new File("mydata");
auto data = f.read;

As you can see, neither variant is as simple/elegant as Phobos's.

My suggestion is to add static methods to classes, which mirror instance methods. For instance, File can have a static read(char[] filename) method, which does the same functionality. Since the current File.read() does not use any other class field/property, File.read() can just call the static method with path_ (the string given to the constructor) as the parameter. Furthermore, in some cases this will improve performance by eliminating the need of a temporary object.

Classes/methods to which this can be applied is File.read/write/append, FilePath.exists/create/etc...

P.S. This is actually one of my pet peeves with Tango - all the code ending up too verbose (and from what I heard it's a common complaint about Tango). It should be simple to do simple things, and, IMO, instantiating an object to check for a file's existence is pretty silly.

Change History

08/29/07 23:07:02 changed by sean

  • owner changed from sean to kris.

09/29/07 20:07:05 changed by kris

  • status changed from new to assigned.

We're waiting for struct ctors to make this happen

10/12/07 06:25:29 changed by kris

  • status changed from assigned to closed.
  • resolution set to fixed.

In the meantime, I tried to make classes instantiate like a struct ctor:

auto data = File("mydata").read;

if (FilePath("mypath").exists) ...

The encapsulation will likely remain, but implementation and accessibility will likely change over time.

10/12/07 06:27:37 changed by CyberShadow

Sweet, thanks!