unwrap

Extract object previously wrapped by wrap.

version(StdDdoc)
inout(Target)
unwrap
(
Target
Source
)
(
inout Source src
)

Parameters

Target

type of wrapped object

src Source

wrapper object returned by wrap

Return Value

Type: inout(Target)

the wrapped object, or null if src is not a wrapper created by wrap and Target is a class

Throws

std.conv.ConvException when attempting to extract a struct which is not the wrapped type

Examples

1 interface Quack
2 {
3     int quack();
4     @property int height();
5 }
6 interface Flyer
7 {
8     @property int height();
9 }
10 class Duck : Quack
11 {
12     int quack() { return 1; }
13     @property int height() { return 10; }
14 }
15 class Human
16 {
17     int quack() { return 2; }
18     @property int height() { return 20; }
19 }
20 struct HumanStructure
21 {
22     int quack() { return 3; }
23     @property int height() { return 30; }
24 }
25 
26 Duck d1 = new Duck();
27 Human h1 = new Human();
28 HumanStructure hs1;
29 
30 interface Refreshable
31 {
32     int refresh();
33 }
34 // does not have structural conformance
35 static assert(!__traits(compiles, d1.wrap!Refreshable));
36 static assert(!__traits(compiles, h1.wrap!Refreshable));
37 static assert(!__traits(compiles, hs1.wrap!Refreshable));
38 
39 // strict upcast
40 Quack qd = d1.wrap!Quack;
41 assert(qd is d1);
42 assert(qd.quack() == 1);    // calls Duck.quack
43 // strict downcast
44 Duck d2 = qd.unwrap!Duck;
45 assert(d2 is d1);
46 
47 // structural upcast
48 Quack qh = h1.wrap!Quack;
49 Quack qhs = hs1.wrap!Quack;
50 assert(qh.quack() == 2);    // calls Human.quack
51 assert(qhs.quack() == 3);    // calls HumanStructure.quack
52 // structural downcast
53 Human h2 = qh.unwrap!Human;
54 HumanStructure hs2 = qhs.unwrap!HumanStructure;
55 assert(h2 is h1);
56 assert(hs2 is hs1);
57 
58 // structural upcast (two steps)
59 Quack qx = h1.wrap!Quack;   // Human -> Quack
60 Quack qxs = hs1.wrap!Quack;   // HumanStructure -> Quack
61 Flyer fx = qx.wrap!Flyer;   // Quack -> Flyer
62 Flyer fxs = qxs.wrap!Flyer;   // Quack -> Flyer
63 assert(fx.height == 20);    // calls Human.height
64 assert(fxs.height == 30);    // calls HumanStructure.height
65 // strucural downcast (two steps)
66 Quack qy = fx.unwrap!Quack; // Flyer -> Quack
67 Quack qys = fxs.unwrap!Quack; // Flyer -> Quack
68 Human hy = qy.unwrap!Human; // Quack -> Human
69 HumanStructure hys = qys.unwrap!HumanStructure; // Quack -> HumanStructure
70 assert(hy is h1);
71 assert(hys is hs1);
72 // strucural downcast (one step)
73 Human hz = fx.unwrap!Human; // Flyer -> Human
74 HumanStructure hzs = fxs.unwrap!HumanStructure; // Flyer -> HumanStructure
75 assert(hz is h1);
76 assert(hzs is hs1);
import std.traits : functionAttributes, FunctionAttribute;
interface A { int run(); }
interface B { int stop(); @property int status(); }
class X
{
    int run() { return 1; }
    int stop() { return 2; }
    @property int status() { return 3; }
}

auto x = new X();
auto ab = x.wrap!(A, B);
A a = ab;
B b = ab;
assert(a.run() == 1);
assert(b.stop() == 2);
assert(b.status == 3);
static assert(functionAttributes!(typeof(ab).status) & FunctionAttribute.property);

See Also

Suggestion Box / Bug Report