Thread overview
Using std.format required std.string?
Mar 15, 2015
Robert M. Münch
Mar 15, 2015
Marc Schütz
Mar 15, 2015
Robert M. Münch
Mar 15, 2015
Robert M. Münch
Mar 15, 2015
anonymous
Mar 16, 2015
Robert M. Münch
Mar 16, 2015
ketmar
Mar 16, 2015
Mike Parker
Mar 16, 2015
Mike Parker
March 15, 2015
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

March 15, 2015
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.
March 15, 2015
On 2015-03-15 16:22:03 +0000, Marc Schütz said:

> 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.

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)?

-- 
Robert M. Münch
http://www.saphirion.com
smarter | better | faster

March 15, 2015
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

March 15, 2015
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;`.
March 16, 2015
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.
March 16, 2015
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.
March 16, 2015
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

March 16, 2015
On Mon, 16 Mar 2015 12:32:01 +0100, Robert M. Münch 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;
}