🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #1200

Variants

#1200
From: 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 ------------------
Technical Details
Message-ID: <1993Jan3.044702.24443@ucc.su.OZ.AU>
Path: arc1-ca.pugleaf.net!sparky!uunet!munnari.oz.au!metro!extro.ucc.su.OZ.AU!maxtal

Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c++
Path: arc1-ca.pugleaf.net!sparky!uunet!munnari.oz.au!metro!extro.ucc.su.OZ.AU!maxtal
From: maxtal@extro.ucc.su.OZ.AU (John MAX Skaller)
Subject: Variants
Message-ID: <1993Jan3.044702.24443@ucc.su.OZ.AU>
Sender: news@ucc.su.OZ.AU
Nntp-Posting-Host: extro.ucc.su.oz.au
Organization: MAXTAL P/L C/- University Computing Centre, Sydney
Date: Sun, 3 Jan 1993 04:47:02 GMT
Lines: 98