🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

1 total messages Started by John MAX Skaller Sun, 03 Jan 1993 04:47
Variants
#1200
Author: John MAX Skaller
Date: Sun, 03 Jan 1993 04:47
98 lines
2652 bytes
Variants, Part II.
------------------

The 'type' clauses of a select statement specify an implict
variant:

	variant Num [float, complex, int] x;

	select(x)
	{
		type(double d) { ... }
		type(long l)   { ... }
		type(complex c) { ...}
	}

In this case the implicit variant is

	variant Implicit [double,long,complex];

This clearly indicates how variants interconvert,

	variant X [X1, X2] x;
	variant Y [Y1, Y2] y;

	Y y=x;

The conversion is well defined if each of X1 and X2 unambiguously
converts to either Y1 or Y2, where 'unambiguously converts' is
used in the same sense as the normal overload matching rules.

The select statement can have multiple arguments:

	variant V [V1, V2] v;
	variant W [W1, W2] w;

	select(v,w)
	{
		type(V1 *v1, W1 *w1) { ... }
		type(V2 *v2, W1 *w1) { ... }
		type(V1 *v1, W2 *w2) { ... }
		type(V2 *v2, W2 *w2) { ... }
	}

The select statement can have a 'default' (none-of-the-above)

	select(v)
	{
		type(V1 v1) { ... }
		type(...) { cout << "Type Not Handled"; }
	}

Where variants share a common base, it would be more usual
to do:

	class B;
	class D1 : B;
	class D2 : B;

	variant D [B,D1,D2] *d;

	select(d)
	{
		type(D1 *d1) { ...}  // special case
		type(B *b)   { ... } // default to base class
	}

Where dynamic downcasting is required, it can be constrained
to a one off function:

	D* downcast(B* b)
	{
		D *d;
		if (d=dynamic_cast<D1*>b);
		else if (d=dynamic_cast<D2*>b);
		else d=b;
		return d;
	}

allowing the downcast to be performed exactly once in one lexically
enclosed place, and thereafter guarranting secure access to
the required exact types.

(Note: this example requires variant assignment)

(Note 2: If you are a bit worried about the way the * symbol
got shifted about:

	variant V [V1, V2] *vp;
	variant pV [V1*, V2*] vp; // same as above??

show it makes sense and deduce a variant is not a type,
or wait for part III)

-- 
;----------------------------------------------------------------------
        JOHN (MAX) SKALLER,         maxtal@extro.ucc.su.oz.au
	Maxtal Pty Ltd, 6 MacKay St ASHFIELD, NSW 2131, AUSTRALIA
;--------------- SCIENTIFIC AND ENGINEERING SOFTWARE ------------------
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads