www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - lvalue - opIndexAssign - Tango

reply The Anh Tran <trtheanh gmail.com> writes:
Hi,

When porting from c++ to D, i encounter this strange discrimination:
1. Built-in AA:
	int[int] arr;
	arr[123] += 12345;
	arr[321]++;

2. Tango HashMap:
	auto hm = new HashMap!(int, int)();
	hm[123] += 12345; // error not lvalue
	hm[123]++;	// error

D document says current opIndexAssign does not work as lvalue. But why 
can builtin AA can that? How can i copy builtin AA behaviour?

--------------

Forgive my noob, where is the place to ask question, report bug for Tango?

1. I can't compile D code using tango hashmap in debug mode:

import tango.util.container.HashMap;
void main()
{
	auto hm = new HashMap!(uint, uint)();
}

 dmd -w -g -debug  hello.d // error
2. Compile D code using Tango Regex by GDC emit lots of link errors. 3. Bug in Tango atomicIncrement, atomicDecrement: int task_done = 0; atomicIncrement(task_done); That function is compiled in asm: lock inc byte ptr[task_done]; Which is wrong. It'll wrap to 0 at 255. It should be: lock inc dword ptr[task_done]; 4. There is no atomicAdd(ref original, int newvalue) family. GCC equivalence is __syn_fetch_and_add ...
Mar 13 2009
next sibling parent reply Daniel Keep <daniel.keep.lists gmail.com> writes:
The Anh Tran wrote:
 Hi,
 
 When porting from c++ to D, i encounter this strange discrimination:
 1. Built-in AA:
     int[int] arr;
     arr[123] += 12345;
     arr[321]++;
 
 2. Tango HashMap:
     auto hm = new HashMap!(int, int)();
     hm[123] += 12345; // error not lvalue
     hm[123]++;    // error
 
 D document says current opIndexAssign does not work as lvalue. But why
 can builtin AA can that? How can i copy builtin AA behaviour?
You can't. This is a hole in the language at the moment, hopefully solved by the introduction of ref returns (but that's in D 2.0 which you don't want to use at the moment.)
 Forgive my noob, where is the place to ask question, report bug for Tango?
You could try the Tango IRC channel: irc://irc.freenode.org/#d.tango That, or the Tango forums: http://dsource.org/projects/tango/forums You can report problems with Tango via the ticket system: http://dsource.org/projects/tango/report ("New Ticket" is down the bottom of the page.)
 1. I can't compile D code using tango hashmap in debug mode:
 
 import tango.util.container.HashMap;
 void main()
 {
     auto hm = new HashMap!(uint, uint)();
 }
 
 dmd -w -g -debug  hello.d // error
When posting problems with compiling something, it helps to mention the version of the compiler you're using, your platform, the version of Tango (in this case) and what the error actually is.
 2. Compile D code using Tango Regex by GDC emit lots of link errors.
 
 3. Bug in Tango atomicIncrement, atomicDecrement:
 
 int task_done = 0;
 atomicIncrement(task_done);
 
 That function is compiled in asm:
 lock inc byte ptr[task_done];
 Which is wrong. It'll wrap to 0 at 255.
 It should be: lock inc dword ptr[task_done];
 
 4. There is no atomicAdd(ref original, int newvalue) family. GCC
 equivalence is __syn_fetch_and_add ...
-- Daniel
Mar 13 2009
parent The Anh Tran <trtheanh gmail.com> writes:
Daniel Keep wrote:
 
 You could try the Tango IRC channel:
 That, or the Tango forums:
 http://dsource.org/projects/tango/forums
 
 You can report problems with Tango via the ticket system:
 http://dsource.org/projects/tango/report ("New Ticket" is down the
 bottom of the page.)
I tried to register. But there is no confirmation mail sent to me for 2 days. That's why i post here :( ------------------------- Linux Ubuntu 8.10. DMD v1.041 from digitalmars.com GDC 4.2, download from ubuntu synaptic. Tango 0.99.7. Zip package from http://www.dsource.org/projects/tango/wiki/SourceDownloads ----------------------- import tango.io.Stdout; import tango.util.container.HashMap; void main() { auto hm = new HashMap!(uint, uint)(); } dmd -w -g -debug test.d Error: warning - ../import/tango/util/container/Slink.d(352): Error: statement is not reachable ../import/tango/util/container/HashMap.d(13): template instance tango.util.container.HashMap.HashMap!(uint,uint) error instantiating ------------------------------------ GDC tango regex has been fixed myself. It's just a compile flag. ------------------------------------ import tango.io.Stdout; import tango.core.Atomic; void main() { uint x = 255; uint y = atomicIncrement(x); Atomic!(uint) at; at.store(255); uint c = at.increment(); } gdc ./test.d -o ./test-tango-gdc -frelease -g -O3 -msse2 -march=native -mfpmath=sse -femit-templates=auto -fversion=Posix -fversion=Tango -lgtango ./hello.d:21: template tango.core.Atomic.Atomic!(uint).Atomic.store(msync ms = msync.seq) does not match any template declaration ./hello.d:21: template tango.core.Atomic.Atomic!(uint).Atomic.store(msync ms = msync.seq) cannot deduce template function from argument types (int) dmd -g -release -O -inline ./test.d -of./test-tango-dmd Here is disasm of atomicIncrement(): push ebp mov ebp, esp push eax mov eax, [ebp+var_4] lock inc byte ptr [eax] <--- wrong, must be dword ptr mov eax, [eax] mov esp, ebp pop ebp retn
Mar 14 2009
prev sibling next sibling parent "Denis Koroskin" <2korden gmail.com> writes:
On Sat, 14 Mar 2009 06:41:52 +0300, The Anh Tran <trtheanh gmail.com> wrote:

 Hi,

 When porting from c++ to D, i encounter this strange discrimination:
 1. Built-in AA:
 	int[int] arr;
 	arr[123] += 12345;
 	arr[321]++;

 2. Tango HashMap:
 	auto hm = new HashMap!(int, int)();
 	hm[123] += 12345; // error not lvalue
 	hm[123]++;	// error

 D document says current opIndexAssign does not work as lvalue. But why  
 can builtin AA can that? How can i copy builtin AA behaviour?

 --------------

 Forgive my noob, where is the place to ask question, report bug for  
 Tango?

 1. I can't compile D code using tango hashmap in debug mode:

 import tango.util.container.HashMap;
 void main()
 {
 	auto hm = new HashMap!(uint, uint)();
 }

  > dmd -w -g -debug  hello.d // error

 2. Compile D code using Tango Regex by GDC emit lots of link errors.

 3. Bug in Tango atomicIncrement, atomicDecrement:

 int task_done = 0;
 atomicIncrement(task_done);

 That function is compiled in asm:
 lock inc byte ptr[task_done];
 Which is wrong. It'll wrap to 0 at 255.
 It should be: lock inc dword ptr[task_done];
This one is fixed long ago, try updating your Tango installation.
 4. There is no atomicAdd(ref original, int newvalue) family. GCC  
 equivalence is __syn_fetch_and_add ...
Mar 13 2009
prev sibling parent reply downs <default_357-line yahoo.de> writes:
The Anh Tran wrote:
 Hi,
 
 When porting from c++ to D, i encounter this strange discrimination:
 1. Built-in AA:
     int[int] arr;
     arr[123] += 12345;
     arr[321]++;
 
 2. Tango HashMap:
     auto hm = new HashMap!(int, int)();
     hm[123] += 12345; // error not lvalue
     hm[123]++;    // error
 
 D document says current opIndexAssign does not work as lvalue. But why
 can builtin AA can that? How can i copy builtin AA behaviour?
 
Take a look at tools.behave_as (for phobos). It only works properly with built-in types though. http://dsource.org/projects/scrapple/browser/trunk/tools/tools/behave_as.d It should let you implement a HashMap that supports += and similar, at least for built-ins.
Mar 15 2009
parent reply The Anh Tran <trtheanh gmail.com> writes:
downs wrote:
 Take a look at tools.behave_as (for phobos).
 It only works properly with built-in types though.
 http://dsource.org/projects/scrapple/browser/trunk/tools/tools/behave_as.d
 It should let you implement a HashMap that supports += and similar, at least
for built-ins.
Many thanks. No offense, but for a beginner like me, those mixin are similar to C macro hacks years ago. :)
Mar 15 2009
parent bearophile <bearophileHUGS lycos.com> writes:
The Anh Tran:
 No offense, but for a beginner like me, those mixin are similar to C 
 macro hacks years ago. :)
They share part of the same purposes. But D string mixing are scoped, while C macros cut across scopes. This makes mixins quite safer to use. Bye, bearophile
Mar 15 2009