Issue 171: Std.format side effect
Status:  WontFix
Owner: ----
Closed:  Jun 2012
Reported by musil.j...@gmail.com, May 1, 2012
What steps will reproduce the problem?

		var obj = {a : {b: 1, c : "a"}};

		for (field in Reflect.fields(obj)) {
			var a = Reflect.field(obj, field);	
			trace(Std.format("${a}"));
			trace(field + " - " + Reflect.fields(a));			
		}

What is the expected output? What do you see instead?
Commenting out and back the line 

trace(Std.format("${a}"));

changes the Reflect.fields(a) result!

What version of the product are you using? On what operating system?
hxcpp svn Revision: 468

Please provide any additional information below.

Jun 12, 2012
#1 tyn...@gmail.com
Can also be reproduced without a loop (more readable generated code):

class  Issue171 
{
        public static function main()
        {
                var obj = {a: {b: 1, c: "a"}};
                var a = Reflect.field(obj, 'a');
                trace(Std.format("${a}"));
                trace("a - " + Reflect.fields(a));
        }
}

This doesn't seems to be linked to hxcpp, though, but to the Haxe runtimes. Here's the different output of haxe -cpp bin -main  Issue171  with the class above.

With the 1st trace commented (no issue, Reflect.fields(a) returns what's expected):

		HX_SOURCE_POS("./ Issue171 .hx",5)
		Dynamic obj = _Function_1_1::Block();
		HX_SOURCE_POS("./ Issue171 .hx",6)
		Dynamic a = ::Reflect_obj::field(obj,HX_CSTRING("a"));
		HX_SOURCE_POS("./ Issue171 .hx",8)
		::haxe::Log_obj::trace((HX_CSTRING("a - ") + ::Std_obj::string(::Reflect_obj::fields(a))),hx::SourceInfo(HX_CSTRING(" Issue171 .hx"),8,HX_CSTRING(" Issue171 "),HX_CSTRING("main")));

With the 1st trace uncommented, reproducing the issue:

		HX_SOURCE_POS("./ Issue171 .hx",5)
		Dynamic obj = _Function_1_1::Block();
		HX_SOURCE_POS("./ Issue171 .hx",6)
		::String a = ::Reflect_obj::field(obj,HX_CSTRING("a"));
		HX_SOURCE_POS("./ Issue171 .hx",7)
		::haxe::Log_obj::trace((HX_CSTRING("") + a),hx::SourceInfo(HX_CSTRING(" Issue171 .hx"),7,HX_CSTRING(" Issue171 "),HX_CSTRING("main")));
		HX_SOURCE_POS("./ Issue171 .hx",8)
		::haxe::Log_obj::trace((HX_CSTRING("a - ") + ::Std_obj::string(::Reflect_obj::fields(a))),hx::SourceInfo(HX_CSTRING(" Issue171 .hx"),8,HX_CSTRING(" Issue171 "),HX_CSTRING("main")));

The generated type of 'a' differs. When the issue doesn't happen, the type is Dynamic (as expected). With the issue, the type becomes ::String, which doesn't make sense in the context.

It seems to be triggered on typing phase. The type of 'a' is inferred to String because of the way it is used in the trace. This whole issue can be completely avoided by hard-typing a : "var a : Dynamic = Reflect.field(obj, field);" works with or without the trace.

I suggest reposting an issue on the Haxe runtimes.
Jun 13, 2012
Project Member #2 gameh...@gmail.com
Ok - well analyzed.  I think this is a haxe issue - I have come across this myself in other forms (tracing a variable causes to be typed as string).  I will close this here, but it is probably worth bringing up on the mailing list and see if there is some consensus on the required action.
Status: WontFix
Jun 20, 2012
#3 tyn...@gmail.com
I have opened a thread on the mailing list on that: https://groups.google.com/forum/#!topic/haxelang/eOzz4j93XUU