Getting around forward references in templates?

Tristam MacDonald swiftcoder at gmail.com
Wed May 2 08:23:33 PDT 2007


So I have a setup like the following (simplified) example:

module A;

import B;

struct A(T)
{
	T x, y;
	
	void set(B o) {x = cast(T)o.a; y = cast(T)o.b;}
}

alias A!(float) Af;

//=========================

module B;

import A;

struct B
{
	float a, b;
	
	static B opCall(float x, float y) {B t; t.a = x; t.b = y; return t;}
	
	void set(Af o) {a = o.x; b = o.y;}
}

//=============================

import A;
import B;

int main()
{
	B b = B(1.0, 2.0);
	
	Af a;
	
	a.set(b);
	
	return 0;
}

//------------------------------

Now the compiler says:

B.d:12: Error: forward reference to 'A!(float)'
B.d:12: Error: Af is used as a type
B.d:12: Error: cannot have parameter of type void
B.d:12: Error: forward reference to 'A!(float)'
B.d:12: Error: Af is used as a type
B.d:12: Error: cannot have parameter of type void

Obviously this is a contrived example, but it is not uncommon to have a module containing a template which must import itself in a circular manner. Is there any way (easy or not) to get around this, other than passing a pointer to float* into A.set(B)?
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test.zip
Type: application/zip
Size: 2294 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20070502/b9d929c4/attachment.zip>


More information about the Digitalmars-d mailing list