www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Inconsistencies between global imports and function local imports

reply "Tyro[a.c.edwards]" <nospam home.com> writes:
Methinks function local imports (introduced in 2.054) is a great idea, 
however if it is to be allowed, I believe it should provide all the 
functionality of global imports: which it currently does not.

import std.stdio;
import std.string;
import std.conv;

// Note: all of these import formats work if imported here but only
// the first work if imported locally to the function.

//import std.utf;
//import std.utf: toUTF16z;
//import std.utf: wcp = toUTF16z;

void main()
{
	auto s = "漢字を書くのはどうかな~?";
	auto s1 = genText(s);
	writeln(to!string(typeid(s1)));
}

auto genText(string t)
{
	import std.utf;	// This works
	//import std.utf: toUTF16z; // This doesn't
	//import std.utf: wcp = toUTF16z; // Neither does this
	
	version(Unicode)
	{
		// Note: Everything here works with global import
		// but only the first line works with function local imports
		
		return toUTF16z(t);   // This works
		//return t.toUTF16z;  // This doesn't
		//return wcp(t);      // Neither does this
		//return t.wcp;       // Or this
	}
	else
	{
		return t.toStringz;
	}
}


[OT - toUTFz]

Wasn't there discussion about adding toUTFz to the std.utf? For some 
reason I thought that was forthcoming in 2.054... whatever happened there?
Jul 12 2011
next sibling parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Wednesday 13 July 2011 11:56:10 Tyro[a.c.edwards] wrote:
 Methinks function local imports (introduced in 2.054) is a great idea=
,
 however if it is to be allowed, I believe it should provide all the
 functionality of global imports: which it currently does not.
=20
 import std.stdio;
 import std.string;
 import std.conv;
=20
 // Note: all of these import formats work if imported here but only
 // the first work if imported locally to the function.
=20
 //import std.utf;
 //import std.utf: toUTF16z;
 //import std.utf: wcp =3D toUTF16z;
=20
 void main()
 {
 =09auto s =3D "=E6=BC=A2=E5=AD=97=E3=82=92=E6=9B=B8=E3=81=8F=E3=81=AE=
=E3=81=AF=E3=81=A9=E3=81=86=E3=81=8B=E3=81=AA=EF=BD=9E=EF=BC=9F";
 =09auto s1 =3D genText(s);
 =09writeln(to!string(typeid(s1)));
 }
=20
 auto genText(string t)
 {
 =09import std.utf;=09// This works
 =09//import std.utf: toUTF16z; // This doesn't
 =09//import std.utf: wcp =3D toUTF16z; // Neither does this
=20
 =09version(Unicode)
 =09{
 =09=09// Note: Everything here works with global import
 =09=09// but only the first line works with function local imports
=20
 =09=09return toUTF16z(t);   // This works
 =09=09//return t.toUTF16z;  // This doesn't
 =09=09//return wcp(t);      // Neither does this
 =09=09//return t.wcp;       // Or this
 =09}
 =09else
 =09{
 =09=09return t.toStringz;
 =09}
 }
import std.utf: toUTF16z; is broken to begin with: http://d.puremagic.com/issues/show_bug.cgi?id=3D314 http://d.puremagic.com/issues/show_bug.cgi?id=3D5161 Rather than just importing the symbol like it should, a selective impor= t=20 essentially create a new symbol. So, that's probably why it doesn't wor= k when=20 importing inside of a function.
 [OT - toUTFz]
=20
 Wasn't there discussion about adding toUTFz to the std.utf? For some
 reason I thought that was forthcoming in 2.054... whatever happened t=
here? It hasn't been merged in yet. It should be in 2.055. https://github.com/D-Programming-Language/phobos/pull/123 - Jonathan M Davis
Jul 12 2011
parent "Tyro[a.c.edwards]" <nospam home.com> writes:
On 7/13/2011 12:07 PM, Jonathan M Davis wrote:
 On Wednesday 13 July 2011 11:56:10 Tyro[a.c.edwards] wrote:
 Methinks function local imports (introduced in 2.054) is a great idea,
 however if it is to be allowed, I believe it should provide all the
 functionality of global imports: which it currently does not.

 import std.stdio;
 import std.string;
 import std.conv;

 // Note: all of these import formats work if imported here but only
 // the first work if imported locally to the function.

 //import std.utf;
 //import std.utf: toUTF16z;
 //import std.utf: wcp = toUTF16z;

 void main()
 {
 	auto s = "漢字を書くのはどうかな~?";
 	auto s1 = genText(s);
 	writeln(to!string(typeid(s1)));
 }

 auto genText(string t)
 {
 	import std.utf;	// This works
 	//import std.utf: toUTF16z; // This doesn't
 	//import std.utf: wcp = toUTF16z; // Neither does this

 	version(Unicode)
 	{
 		// Note: Everything here works with global import
 		// but only the first line works with function local imports

 		return toUTF16z(t);   // This works
 		//return t.toUTF16z;  // This doesn't
 		//return wcp(t);      // Neither does this
 		//return t.wcp;       // Or this
 	}
 	else
 	{
 		return t.toStringz;
 	}
 }
import std.utf: toUTF16z; is broken to begin with: http://d.puremagic.com/issues/show_bug.cgi?id=314 http://d.puremagic.com/issues/show_bug.cgi?id=5161 Rather than just importing the symbol like it should, a selective import essentially create a new symbol. So, that's probably why it doesn't work when importing inside of a function.
IC, thanks for the response/info... Now I know!
 [OT - toUTFz]

 Wasn't there discussion about adding toUTFz to the std.utf? For some
 reason I thought that was forthcoming in 2.054... whatever happened there?
It hasn't been merged in yet. It should be in 2.055. https://github.com/D-Programming-Language/phobos/pull/123 - Jonathan M Davis
Got it. Thanks again.
Jul 13 2011
prev sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
I've reported the UFCS issue with function imports to bugzilla a few weeks ago.
Jul 12 2011