www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Using std.format required std.string?

reply =?iso-8859-1?Q?Robert_M._M=FCnch?= <robert.muench saphirion.com> writes:
Hi, wondering why this happens:

import std.format;

void ods(T...)(auto ref T args){
  format(args).ptr;
  return;
}

ods("%s   %s", mystring, mystring.ptr);

 Error: undefined identifier format
If I add: "import std.string;" everything compiles and works. Since the docs of std.format contains all the format specifier description etc. I would expect that only including "std.format" should be enough. -- Robert M. Münch http://www.saphirion.com smarter | better | faster
Mar 15 2015
parent reply "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
On Sunday, 15 March 2015 at 15:48:34 UTC, Robert M. Münch wrote:
 Hi, wondering why this happens:

 import std.format;

 void ods(T...)(auto ref T args){
  format(args).ptr;
  return;
 }

 ods("%s   %s", mystring, mystring.ptr);

 Error: undefined identifier format
If I add: "import std.string;" everything compiles and works. Since the docs of std.format contains all the format specifier description etc. I would expect that only including "std.format" should be enough.
For whatever reasons, format() used to be defined in std.string. Indeed it's unintuitive to have it there, and it also pulls in lots of other unrelated things like Unicode tables when you import std.string. That's why it was moved into std.format in this PR: https://github.com/D-Programming-Language/phobos/pull/2732 It will be available in 2.067, soon to be released.
Mar 15 2015
parent reply =?iso-8859-1?Q?Robert_M._M=FCnch?= <robert.muench saphirion.com> writes:
next sibling parent reply =?iso-8859-1?Q?Robert_M._M=FCnch?= <robert.muench saphirion.com> writes:
On 2015-03-15 17:36:24 +0000, Robert M. Münch said:
 Is there a way to use "version(...)" to have code sections depending on 
 compiler version? Something like:
 
 version(dmd >= 2.067) or version(dmd < 2.067)?
Answerting myself: static if (__traits(compiles, version_minor < 67)) import std.string; // format() for versions < 2.0.67 else import std.format; // format() for versions >= 2.0.67 -- Robert M. Münch http://www.saphirion.com smarter | better | faster
Mar 15 2015
parent reply "anonymous" <anonymous example.com> writes:
On Sunday, 15 March 2015 at 18:03:55 UTC, Robert M. Münch wrote:
 On 2015-03-15 17:36:24 +0000, Robert M. Münch said:
 Is there a way to use "version(...)" to have code sections 
 depending on compiler version? Something like:
 
 version(dmd >= 2.067) or version(dmd < 2.067)?
Answerting myself: static if (__traits(compiles, version_minor < 67)) import std.string; // format() for versions < 2.0.67 else import std.format; // format() for versions >= 2.0.67
That doesn't do what you want. You need to `import std.compiler;` for version_minor. Without that import, `__traits(compiles, version_minor < 67)` is always false, because version_minor is undefined. And if you add the import, `__traits(compiles, version_minor < 67)` is always true, no matter the value of version_minor. Use `static if(version_minor < 67)` instead. Also, if you check version_minor, it's probably a good idea to check (or static assert) version_major, too. Finally, there's need for this (yet). `std.string.format` is fine with 2.067, too. So unless you're going for (far) future compatiblity, you can just do `import std.string;`.
Mar 15 2015
parent reply =?iso-8859-1?Q?Robert_M._M=FCnch?= <robert.muench saphirion.com> writes:
On 2015-03-15 19:16:52 +0000, anonymous said:

 Answerting myself:
 
 static if (__traits(compiles, version_minor < 67))
 import std.string; // format() for versions < 2.0.67
 else
 import std.format; // format() for versions >= 2.0.67
That doesn't do what you want. You need to `import std.compiler;` for version_minor. Without that import, `__traits(compiles, version_minor < 67)` is always false, because version_minor is undefined.
I have std.compiler imported.
 And if you add the import, `__traits(compiles, version_minor < 67)` is 
 always true, no matter the value of version_minor. Use `static 
 if(version_minor < 67)` instead.
Ah, ok. Got it. so __traits(compiles,...) checks if the code can be compiled, that's it.
 Finally, there's need for this (yet). `std.string.format` is fine with 
 2.067, too. So unless you're going for (far) future compatiblity, you 
 can just do `import std.string;`.
I prefer to move things to the "far future compatibility" path ASAP. Reduce a lot of maintenance headaches. Thanks for the feedback. -- Robert M. Münch http://www.saphirion.com smarter | better | faster
Mar 16 2015
parent ketmar <ketmar ketmar.no-ip.org> writes:
On Mon, 16 Mar 2015 12:32:01 +0100, Robert M. M=C3=BCnch wrote:

 I prefer to move things to the "far future compatibility" path ASAP.
 Reduce a lot of maintenance headaches.
then you can check properties, not versions. ;-) static if (__traits(compiles, {import std.string : format;})) { import std.string : format; } else { import std.format : format; } =
Mar 16 2015
prev sibling parent reply "Mike Parker" <aldacron gmail.com> writes:
On Sunday, 15 March 2015 at 17:36:24 UTC, Robert M. Münch wrote:

 Ok, good to here. Didn't cath/remember this one.

 Is there a way to use "version(...)" to have code sections 
 depending on compiler version? Something like:

 version(dmd >= 2.067) or version(dmd < 2.067)?
static if( __VERSION__ >= 2.067 ) { ... } This works with any D compiler using the DMD frontend.
Mar 15 2015
parent "Mike Parker" <aldacron gmail.com> writes:
On Monday, 16 March 2015 at 04:31:19 UTC, Mike Parker wrote:
 On Sunday, 15 March 2015 at 17:36:24 UTC, Robert M. Münch wrote:

 Ok, good to here. Didn't cath/remember this one.

 Is there a way to use "version(...)" to have code sections 
 depending on compiler version? Something like:

 version(dmd >= 2.067) or version(dmd < 2.067)?
static if( __VERSION__ >= 2.067 ) { ... }
Ugh. 2067, not 2.067.
Mar 15 2015