Hex Artifact Content
Not logged in

Artifact 4c996bb5859ed5ea358a84945ba4e0d5862f10a2:


0000: 2f 2a 2a 0a 20 2a 20 41 75 74 68 6f 72 73 3a 20  /**. * Authors: 
0010: 6b 2e 69 6e 61 62 61 0a 20 2a 20 4c 69 63 65 6e  k.inaba. * Licen
0020: 73 65 3a 20 4e 59 53 4c 20 30 2e 39 39 38 32 20  se: NYSL 0.9982 
0030: 68 74 74 70 3a 2f 2f 77 77 77 2e 6b 6d 6f 6e 6f  http://www.kmono
0040: 73 2e 6e 65 74 2f 6e 79 73 6c 2f 0a 20 2a 0a 20  s.net/nysl/. *. 
0050: 2a 20 43 6f 6d 6d 6f 6e 20 74 72 69 63 6b 73 20  * Common tricks 
0060: 61 6e 64 20 75 74 69 6c 69 74 69 65 73 20 66 6f  and utilities fo
0070: 72 20 70 72 6f 67 72 61 6d 6d 69 6e 67 20 69 6e  r programming in
0080: 20 44 2e 0a 20 2a 2f 0a 6d 6f 64 75 6c 65 20 74   D.. */.module t
0090: 72 69 63 6b 73 2e 74 72 69 63 6b 73 3b 0a 69 6d  ricks.tricks;.im
00a0: 70 6f 72 74 20 74 72 69 63 6b 73 2e 74 65 73 74  port tricks.test
00b0: 3b 0a 69 6d 70 6f 72 74 20 73 74 64 2e 61 72 72  ;.import std.arr
00c0: 61 79 20 20 20 20 20 20 3a 20 61 70 70 65 6e 64  ay      : append
00d0: 65 72 3b 0a 69 6d 70 6f 72 74 20 73 74 64 2e 66  er;.import std.f
00e0: 6f 72 6d 61 74 20 20 20 20 20 3a 20 66 6f 72 6d  ormat     : form
00f0: 61 74 74 65 64 57 72 69 74 65 3b 0d 0a 69 6d 70  attedWrite;..imp
0100: 6f 72 74 20 63 6f 72 65 2e 65 78 63 65 70 74 69  ort core.excepti
0110: 6f 6e 20 3a 20 41 73 73 65 72 74 45 72 72 6f 72  on : AssertError
0120: 3b 0a 0a 2f 2f 2f 20 53 69 6d 70 6c 65 20 57 72  ;../// Simple Wr
0130: 61 70 70 65 72 20 66 6f 72 20 73 74 64 2e 66 6f  apper for std.fo
0140: 72 6d 61 74 2e 64 6f 46 6f 72 6d 61 74 0a 0a 73  rmat.doFormat..s
0150: 74 72 69 6e 67 20 73 70 72 69 6e 74 66 28 73 74  tring sprintf(st
0160: 72 69 6e 67 20 66 6d 74 2c 20 54 2e 2e 2e 29 28  ring fmt, T...)(
0170: 54 20 70 61 72 61 6d 73 29 0a 7b 0a 09 61 75 74  T params).{..aut
0180: 6f 20 77 72 69 74 65 72 20 3d 20 61 70 70 65 6e  o writer = appen
0190: 64 65 72 21 73 74 72 69 6e 67 28 29 3b 0a 09 66  der!string();..f
01a0: 6f 72 6d 61 74 74 65 64 57 72 69 74 65 28 77 72  ormattedWrite(wr
01b0: 69 74 65 72 2c 20 66 6d 74 2c 20 70 61 72 61 6d  iter, fmt, param
01c0: 73 29 3b 0a 09 72 65 74 75 72 6e 20 77 72 69 74  s);..return writ
01d0: 65 72 2e 64 61 74 61 3b 0a 7d 0a 0a 75 6e 69 74  er.data;.}..unit
01e0: 74 65 73 74 0a 7b 0a 09 61 73 73 65 72 74 5f 65  test.{..assert_e
01f0: 71 28 20 73 70 72 69 6e 74 66 21 22 25 73 20 3d  q( sprintf!"%s =
0200: 3d 20 25 30 34 64 22 28 22 31 2b 32 22 2c 20 33  = %04d"("1+2", 3
0210: 29 2c 20 22 31 2b 32 20 3d 3d 20 30 30 30 33 22  ), "1+2 == 0003"
0220: 20 29 3b 0d 0a 09 61 73 73 65 72 74 5f 65 71 28   );...assert_eq(
0230: 20 73 70 72 69 6e 74 66 21 22 25 32 24 73 20 3d   sprintf!"%2$s =
0240: 3d 20 25 31 24 73 22 28 22 31 2b 32 22 2c 20 35  = %1$s"("1+2", 5
0250: 2c 20 38 29 2c 20 22 35 20 3d 3d 20 31 2b 32 22  , 8), "5 == 1+2"
0260: 20 29 3b 0d 0a 09 61 73 73 65 72 74 5f 74 68 72   );...assert_thr
0270: 6f 77 21 45 72 72 6f 72 28 20 73 70 72 69 6e 74  ow!Error( sprint
0280: 66 21 22 25 73 25 73 22 28 31 29 20 29 3b 0d 0a  f!"%s%s"(1) );..
0290: 7d 0a 0a 2f 2f 2f 20 43 72 65 61 74 65 20 61 6e  }../// Create an
02a0: 20 65 78 63 65 70 74 69 6f 6e 20 77 69 74 68 20   exception with 
02b0: 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63 6f  automatically co
02c0: 6d 70 6c 65 74 65 64 20 66 69 6c 65 6e 61 6d 65  mpleted filename
02d0: 20 61 6e 64 20 6c 69 6e 65 6e 6f 20 69 6e 66 6f   and lineno info
02e0: 72 6d 61 74 69 6f 6e 0a 0a 61 75 74 6f 20 67 65  rmation..auto ge
02f0: 6e 65 78 28 45 78 63 65 70 74 69 6f 6e 54 79 70  nex(ExceptionTyp
0300: 65 2c 20 73 74 72 69 6e 67 20 66 6e 3d 5f 5f 46  e, string fn=__F
0310: 49 4c 45 5f 5f 2c 20 69 6e 74 20 6c 6e 3d 5f 5f  ILE__, int ln=__
0320: 4c 49 4e 45 5f 5f 2c 20 54 2e 2e 2e 29 28 54 20  LINE__, T...)(T 
0330: 70 61 72 61 6d 73 29 0a 7b 0a 09 73 74 61 74 69  params).{..stati
0340: 63 20 69 66 28 20 54 2e 6c 65 6e 67 74 68 20 3e  c if( T.length >
0350: 20 30 20 26 26 20 69 73 28 54 5b 24 2d 31 5d 20   0 && is(T[$-1] 
0360: 3a 20 54 68 72 6f 77 61 62 6c 65 29 20 29 0a 09  : Throwable) )..
0370: 09 72 65 74 75 72 6e 20 6e 65 77 20 45 78 63 65  .return new Exce
0380: 70 74 69 6f 6e 54 79 70 65 28 70 61 72 61 6d 73  ptionType(params
0390: 5b 30 2e 2e 24 2d 31 5d 2c 20 66 6e 2c 20 6c 6e  [0..$-1], fn, ln
03a0: 2c 20 70 61 72 61 6d 73 5b 24 2d 31 5d 29 3b 0a  , params[$-1]);.
03b0: 09 65 6c 73 65 0a 09 09 72 65 74 75 72 6e 20 6e  .else...return n
03c0: 65 77 20 45 78 63 65 70 74 69 6f 6e 54 79 70 65  ew ExceptionType
03d0: 28 70 61 72 61 6d 73 2c 20 66 6e 2c 20 6c 6e 29  (params, fn, ln)
03e0: 3b 0a 7d 0a 0a 75 6e 69 74 74 65 73 74 0a 7b 0a  ;.}..unittest.{.
03f0: 09 61 73 73 65 72 74 5f 6e 65 28 20 67 65 6e 65  .assert_ne( gene
0400: 78 21 45 78 63 65 70 74 69 6f 6e 28 22 6d 73 67  x!Exception("msg
0410: 22 29 2e 66 69 6c 65 2c 20 22 22 20 29 3b 0a 09  ").file, "" );..
0420: 61 73 73 65 72 74 5f 6e 65 28 20 67 65 6e 65 78  assert_ne( genex
0430: 21 45 78 63 65 70 74 69 6f 6e 28 22 6d 73 67 22  !Exception("msg"
0440: 29 2e 6c 69 6e 65 2c 20 30 20 29 3b 0a 09 61 73  ).line, 0 );..as
0450: 73 65 72 74 5f 6e 65 28 20 67 65 6e 65 78 21 45  sert_ne( genex!E
0460: 78 63 65 70 74 69 6f 6e 28 22 6d 73 67 22 2c 6e  xception("msg",n
0470: 65 77 20 45 78 63 65 70 74 69 6f 6e 28 22 62 61  ew Exception("ba
0480: 72 22 29 29 2e 6e 65 78 74 2c 20 45 78 63 65 70  r")).next, Excep
0490: 74 69 6f 6e 2e 69 6e 69 74 20 29 3b 0a 7d 0a 0a  tion.init );.}..
04a0: 2f 2f 2f 20 4d 69 78 69 6e 67 2d 69 6e 20 74 68  /// Mixing-in th
04b0: 65 20 62 65 61 6e 20 63 6f 6e 73 74 72 75 63 74  e bean construct
04c0: 6f 72 20 66 6f 72 20 61 20 63 6c 61 73 73 0a 0a  or for a class..
04d0: 2f 2a 6d 69 78 69 6e 2a 2f 0a 74 65 6d 70 6c 61  /*mixin*/.templa
04e0: 74 65 20 53 69 6d 70 6c 65 43 6f 6e 73 74 72 75  te SimpleConstru
04f0: 63 74 6f 72 28 29 0a 7b 0a 09 73 74 61 74 69 63  ctor().{..static
0500: 20 69 66 28 20 69 73 28 74 79 70 65 6f 66 28 73   if( is(typeof(s
0510: 75 70 65 72 29 20 3d 3d 20 4f 62 6a 65 63 74 29  uper) == Object)
0520: 20 7c 7c 20 73 75 70 65 72 2e 74 75 70 6c 65 6f   || super.tupleo
0530: 66 2e 6c 65 6e 67 74 68 3d 3d 30 20 29 0a 09 09  f.length==0 )...
0540: 74 68 69 73 28 20 74 79 70 65 6f 66 28 74 68 69  this( typeof(thi
0550: 73 2e 74 75 70 6c 65 6f 66 29 20 70 61 72 61 6d  s.tupleof) param
0560: 73 20 29 0a 09 09 7b 0a 09 09 09 73 74 61 74 69  s )...{....stati
0570: 63 20 69 66 28 74 68 69 73 2e 74 75 70 6c 65 6f  c if(this.tupleo
0580: 66 2e 6c 65 6e 67 74 68 3e 30 29 0a 09 09 09 09  f.length>0).....
0590: 74 68 69 73 2e 74 75 70 6c 65 6f 66 20 3d 20 70  this.tupleof = p
05a0: 61 72 61 6d 73 3b 0a 09 09 7d 0a 09 65 6c 73 65  arams;...}..else
05b0: 0a 09 09 74 68 69 73 28 20 74 79 70 65 6f 66 28  ...this( typeof(
05c0: 73 75 70 65 72 2e 74 75 70 6c 65 6f 66 29 20 70  super.tupleof) p
05d0: 73 2c 20 74 79 70 65 6f 66 28 74 68 69 73 2e 74  s, typeof(this.t
05e0: 75 70 6c 65 6f 66 29 20 70 61 72 61 6d 73 20 29  upleof) params )
05f0: 0a 09 09 7b 0a 09 09 09 2f 2f 20 69 6e 63 6c 75  ...{....// inclu
0600: 64 69 6e 67 20 28 6f 6e 6c 79 29 20 74 68 65 20  ding (only) the 
0610: 64 69 72 65 63 74 20 73 75 70 65 72 20 63 6c 61  direct super cla
0620: 73 73 20 6d 65 6d 62 65 72 73 0a 09 09 09 2f 2f  ss members....//
0630: 20 6d 61 79 20 6e 6f 74 20 61 6c 77 61 79 73 20   may not always 
0640: 62 65 20 61 20 64 65 73 69 72 61 62 6c 65 20 63  be a desirable c
0650: 68 6f 69 63 65 2c 20 62 75 74 20 73 68 6f 75 6c  hoice, but shoul
0660: 64 20 77 6f 72 6b 20 66 6f 72 20 6d 61 6e 79 20  d work for many 
0670: 63 61 73 65 73 0a 09 09 09 73 75 70 65 72 28 70  cases....super(p
0680: 73 29 3b 0a 09 09 09 73 74 61 74 69 63 20 69 66  s);....static if
0690: 28 74 68 69 73 2e 74 75 70 6c 65 6f 66 2e 6c 65  (this.tupleof.le
06a0: 6e 67 74 68 3e 30 29 0a 09 09 09 09 74 68 69 73  ngth>0).....this
06b0: 2e 74 75 70 6c 65 6f 66 20 3d 20 70 61 72 61 6d  .tupleof = param
06c0: 73 3b 0a 09 09 7d 0a 7d 0a 0a 75 6e 69 74 74 65  s;...}.}..unitte
06d0: 73 74 0a 7b 0a 09 63 6c 61 73 73 20 54 65 6d 70  st.{..class Temp
06e0: 0a 09 7b 0a 09 09 69 6e 74 20 78 3b 0a 09 09 73  ..{...int x;...s
06f0: 74 72 69 6e 67 20 79 3b 0a 09 09 6d 69 78 69 6e  tring y;...mixin
0700: 20 53 69 6d 70 6c 65 43 6f 6e 73 74 72 75 63 74   SimpleConstruct
0710: 6f 72 3b 0a 09 7d 0a 09 61 73 73 65 72 74 5f 65  or;..}..assert_e
0720: 71 28 20 28 6e 65 77 20 54 65 6d 70 28 31 2c 22  q( (new Temp(1,"
0730: 66 6f 6f 22 29 29 2e 78 2c 20 31 20 29 3b 0a 09  foo")).x, 1 );..
0740: 61 73 73 65 72 74 5f 65 71 28 20 28 6e 65 77 20  assert_eq( (new 
0750: 54 65 6d 70 28 31 2c 22 66 6f 6f 22 29 29 2e 79  Temp(1,"foo")).y
0760: 2c 20 22 66 6f 6f 22 20 29 3b 0a 09 61 73 73 65  , "foo" );..asse
0770: 72 74 28 20 21 5f 5f 74 72 61 69 74 73 28 63 6f  rt( !__traits(co
0780: 6d 70 69 6c 65 73 2c 20 6e 65 77 20 54 65 6d 70  mpiles, new Temp
0790: 29 20 29 3b 0a 09 61 73 73 65 72 74 28 20 21 5f  ) );..assert( !_
07a0: 5f 74 72 61 69 74 73 28 63 6f 6d 70 69 6c 65 73  _traits(compiles
07b0: 2c 20 6e 65 77 20 54 65 6d 70 28 31 29 29 20 29  , new Temp(1)) )
07c0: 3b 0a 09 61 73 73 65 72 74 28 20 21 5f 5f 74 72  ;..assert( !__tr
07d0: 61 69 74 73 28 63 6f 6d 70 69 6c 65 73 2c 20 6e  aits(compiles, n
07e0: 65 77 20 54 65 6d 70 28 22 66 6f 6f 22 2c 31 29  ew Temp("foo",1)
07f0: 29 20 29 3b 0a 0a 09 63 6c 61 73 73 20 54 6f 6d  ) );...class Tom
0800: 70 20 3a 20 54 65 6d 70 0a 09 7b 0a 09 09 72 65  p : Temp..{...re
0810: 61 6c 20 7a 3b 0a 09 09 6d 69 78 69 6e 20 53 69  al z;...mixin Si
0820: 6d 70 6c 65 43 6f 6e 73 74 72 75 63 74 6f 72 3b  mpleConstructor;
0830: 0a 09 7d 0a 09 61 73 73 65 72 74 5f 65 71 28 20  ..}..assert_eq( 
0840: 28 6e 65 77 20 54 6f 6d 70 28 31 2c 22 66 6f 6f  (new Tomp(1,"foo
0850: 22 2c 32 2e 35 29 29 2e 78 2c 20 31 20 29 3b 0a  ",2.5)).x, 1 );.
0860: 09 61 73 73 65 72 74 5f 65 71 28 20 28 6e 65 77  .assert_eq( (new
0870: 20 54 6f 6d 70 28 31 2c 22 66 6f 6f 22 2c 32 2e   Tomp(1,"foo",2.
0880: 35 29 29 2e 79 2c 20 22 66 6f 6f 22 20 29 3b 0a  5)).y, "foo" );.
0890: 09 61 73 73 65 72 74 5f 65 71 28 20 28 6e 65 77  .assert_eq( (new
08a0: 20 54 6f 6d 70 28 31 2c 22 66 6f 6f 22 2c 32 2e   Tomp(1,"foo",2.
08b0: 35 29 29 2e 7a 2c 20 32 2e 35 20 29 3b 0a 09 61  5)).z, 2.5 );..a
08c0: 73 73 65 72 74 28 20 21 5f 5f 74 72 61 69 74 73  ssert( !__traits
08d0: 28 63 6f 6d 70 69 6c 65 73 2c 20 6e 65 77 20 54  (compiles, new T
08e0: 6f 6d 70 28 33 2e 31 34 29 29 20 29 3b 0a 0a 09  omp(3.14)) );...
08f0: 2f 2f 20 73 68 69 79 6f 2d 20 64 65 73 75 2e 20  // shiyo- desu. 
0900: 44 6f 6e 27 74 20 75 73 65 20 69 6e 20 74 68 69  Don't use in thi
0910: 73 20 77 61 79 2e 0a 09 2f 2f 20 20 20 54 61 6d  s way...//   Tam
0920: 70 20 74 72 69 65 73 20 74 6f 20 63 61 6c 6c 20  p tries to call 
0930: 6e 65 77 20 54 6f 6d 70 28 72 65 61 6c 29 20 28  new Tomp(real) (
0940: 62 65 63 61 75 73 65 20 69 74 20 6f 6e 6c 79 20  because it only 
0950: 73 65 65 73 20 54 6f 6d 70 27 73 20 6d 65 6d 62  sees Tomp's memb
0960: 65 72 73 29 2c 0a 09 2f 2f 20 20 20 62 75 74 20  ers),..//   but 
0970: 69 74 20 66 61 69 6c 73 20 62 65 63 61 75 73 65  it fails because
0980: 20 54 6f 6d 70 20 74 61 6b 65 73 20 28 69 6e 74   Tomp takes (int
0990: 2c 73 74 72 69 6e 67 2c 72 65 61 6c 29 2e 0a 09  ,string,real)...
09a0: 61 73 73 65 72 74 28 20 21 5f 5f 74 72 61 69 74  assert( !__trait
09b0: 73 28 63 6f 6d 70 69 6c 65 73 2c 20 7b 0a 09 09  s(compiles, {...
09c0: 63 6c 61 73 73 20 54 61 6d 70 20 3a 20 54 6f 6d  class Tamp : Tom
09d0: 70 0a 09 09 7b 0a 09 09 09 6d 69 78 69 6e 20 53  p...{....mixin S
09e0: 69 6d 70 6c 65 43 6f 6e 73 74 72 75 63 74 6f 72  impleConstructor
09f0: 3b 0a 09 09 7d 0a 09 7d 29 20 29 3b 0a 7d 0a 0a  ;...}..}) );.}..
0a00: 2f 2f 2f 20 4d 69 78 69 6e 67 2d 69 6e 20 74 68  /// Mixing-in th
0a10: 65 20 4d 4f 53 54 2d 44 45 52 49 56 45 44 2d 6d  e MOST-DERIVED-m
0a20: 65 6d 62 65 72 2d 77 69 73 65 20 63 6f 6d 70 61  ember-wise compa
0a30: 72 61 74 6f 72 20 66 6f 72 20 61 20 63 6c 61 73  rator for a clas
0a40: 73 0a 0a 2f 2a 6d 69 78 69 6e 2a 2f 0a 74 65 6d  s../*mixin*/.tem
0a50: 70 6c 61 74 65 20 53 69 6d 70 6c 65 43 6f 6d 70  plate SimpleComp
0a60: 61 72 65 28 29 0a 7b 0a 09 6f 76 65 72 72 69 64  are().{..overrid
0a70: 65 20 62 6f 6f 6c 20 6f 70 45 71 75 61 6c 73 28  e bool opEquals(
0a80: 4f 62 6a 65 63 74 20 72 68 73 5f 29 20 63 6f 6e  Object rhs_) con
0a90: 73 74 0a 09 7b 0a 09 09 69 66 28 20 61 75 74 6f  st..{...if( auto
0aa0: 20 72 68 73 20 3d 20 63 61 73 74 28 74 79 70 65   rhs = cast(type
0ab0: 6f 66 28 74 68 69 73 29 29 72 68 73 5f 20 29 0a  of(this))rhs_ ).
0ac0: 09 09 7b 0a 09 09 09 66 6f 72 65 61 63 68 28 69  ..{....foreach(i
0ad0: 2c 5f 3b 20 74 68 69 73 2e 74 75 70 6c 65 6f 66  ,_; this.tupleof
0ae0: 29 0a 09 09 09 09 69 66 28 20 74 68 69 73 2e 74  ).....if( this.t
0af0: 75 70 6c 65 6f 66 5b 69 5d 20 21 3d 20 72 68 73  upleof[i] != rhs
0b00: 2e 74 75 70 6c 65 6f 66 5b 69 5d 20 29 0a 09 09  .tupleof[i] )...
0b10: 09 09 09 72 65 74 75 72 6e 20 66 61 6c 73 65 3b  ...return false;
0b20: 0a 09 09 09 72 65 74 75 72 6e 20 74 72 75 65 3b  ....return true;
0b30: 0a 09 09 7d 0a 09 09 61 73 73 65 72 74 28 66 61  ...}...assert(fa
0b40: 6c 73 65 2c 20 73 70 72 69 6e 74 66 21 22 43 61  lse, sprintf!"Ca
0b50: 6e 6e 6f 74 20 63 6f 6d 70 61 72 65 20 25 73 20  nnot compare %s 
0b60: 77 69 74 68 20 25 73 22 28 74 79 70 65 69 64 28  with %s"(typeid(
0b70: 74 68 69 73 29 2c 20 74 79 70 65 69 64 28 72 68  this), typeid(rh
0b80: 73 5f 29 29 29 3b 0a 09 7d 0a 0a 09 6f 76 65 72  s_)));..}...over
0b90: 72 69 64 65 20 68 61 73 68 5f 74 20 74 6f 48 61  ride hash_t toHa
0ba0: 73 68 28 29 20 63 6f 6e 73 74 0a 09 7b 0a 09 09  sh() const..{...
0bb0: 68 61 73 68 5f 74 20 68 20 3d 20 30 3b 0a 09 09  hash_t h = 0;...
0bc0: 66 6f 72 65 61 63 68 28 6d 65 6d 3b 20 74 68 69  foreach(mem; thi
0bd0: 73 2e 74 75 70 6c 65 6f 66 29 0a 09 09 09 68 20  s.tupleof)....h 
0be0: 2b 3d 20 74 79 70 65 69 64 28 6d 65 6d 29 2e 67  += typeid(mem).g
0bf0: 65 74 48 61 73 68 28 26 6d 65 6d 29 3b 0a 09 09  etHash(&mem);...
0c00: 72 65 74 75 72 6e 20 68 3b 0a 09 7d 0a 0a 09 6f  return h;..}...o
0c10: 76 65 72 72 69 64 65 20 69 6e 74 20 6f 70 43 6d  verride int opCm
0c20: 70 28 4f 62 6a 65 63 74 20 72 68 73 5f 29 20 63  p(Object rhs_) c
0c30: 6f 6e 73 74 0a 09 7b 0a 09 09 69 66 28 20 61 75  onst..{...if( au
0c40: 74 6f 20 72 68 73 20 3d 20 63 61 73 74 28 74 79  to rhs = cast(ty
0c50: 70 65 6f 66 28 74 68 69 73 29 29 72 68 73 5f 20  peof(this))rhs_ 
0c60: 29 0a 09 09 7b 0a 09 09 09 66 6f 72 65 61 63 68  )...{....foreach
0c70: 28 69 2c 5f 3b 20 74 68 69 73 2e 74 75 70 6c 65  (i,_; this.tuple
0c80: 6f 66 29 0a 09 09 09 09 69 66 28 61 75 74 6f 20  of).....if(auto 
0c90: 63 20 3d 20 74 79 70 65 69 64 28 5f 29 2e 63 6f  c = typeid(_).co
0ca0: 6d 70 61 72 65 28 26 74 68 69 73 2e 74 75 70 6c  mpare(&this.tupl
0cb0: 65 6f 66 5b 69 5d 2c 26 72 68 73 2e 74 75 70 6c  eof[i],&rhs.tupl
0cc0: 65 6f 66 5b 69 5d 29 29 0a 09 09 09 09 09 72 65  eof[i]))......re
0cd0: 74 75 72 6e 20 63 3b 0a 09 09 09 72 65 74 75 72  turn c;....retur
0ce0: 6e 20 30 3b 0a 09 09 7d 0a 09 09 61 73 73 65 72  n 0;...}...asser
0cf0: 74 28 66 61 6c 73 65 2c 20 73 70 72 69 6e 74 66  t(false, sprintf
0d00: 21 22 43 61 6e 6e 6f 74 20 63 6f 6d 70 61 72 65  !"Cannot compare
0d10: 20 25 73 20 77 69 74 68 20 25 73 22 28 74 79 70   %s with %s"(typ
0d20: 65 69 64 28 74 68 69 73 29 2c 20 74 79 70 65 69  eid(this), typei
0d30: 64 28 72 68 73 5f 29 29 29 3b 0a 09 7d 0a 7d 0a  d(rhs_)));..}.}.
0d40: 0a 75 6e 69 74 74 65 73 74 0a 7b 0a 09 63 6c 61  .unittest.{..cla
0d50: 73 73 20 54 65 6d 70 0a 09 7b 0a 09 09 69 6e 74  ss Temp..{...int
0d60: 20 78 3b 0a 09 09 73 74 72 69 6e 67 20 79 3b 0a   x;...string y;.
0d70: 09 09 6d 69 78 69 6e 20 53 69 6d 70 6c 65 43 6f  ..mixin SimpleCo
0d80: 6e 73 74 72 75 63 74 6f 72 3b 0a 09 09 6d 69 78  nstructor;...mix
0d90: 69 6e 20 53 69 6d 70 6c 65 43 6f 6d 70 61 72 65  in SimpleCompare
0da0: 3b 0a 09 7d 0a 09 61 73 73 65 72 74 5f 65 71 28  ;..}..assert_eq(
0db0: 20 6e 65 77 20 54 65 6d 70 28 31 2c 22 66 6f 6f   new Temp(1,"foo
0dc0: 22 29 2c 20 6e 65 77 20 54 65 6d 70 28 31 2c 22  "), new Temp(1,"
0dd0: 66 6f 6f 22 29 20 29 3b 0a 09 61 73 73 65 72 74  foo") );..assert
0de0: 5f 65 71 28 20 28 6e 65 77 20 54 65 6d 70 28 31  _eq( (new Temp(1
0df0: 2c 22 66 6f 6f 22 29 29 2e 74 6f 48 61 73 68 2c  ,"foo")).toHash,
0e00: 20 28 6e 65 77 20 54 65 6d 70 28 31 2c 22 66 6f   (new Temp(1,"fo
0e10: 6f 22 29 29 2e 74 6f 48 61 73 68 20 29 3b 0a 09  o")).toHash );..
0e20: 61 73 73 65 72 74 5f 6e 65 28 20 6e 65 77 20 54  assert_ne( new T
0e30: 65 6d 70 28 31 2c 22 66 6f 6f 22 29 2c 20 6e 65  emp(1,"foo"), ne
0e40: 77 20 54 65 6d 70 28 32 2c 22 66 6f 6f 22 29 20  w Temp(2,"foo") 
0e50: 29 3b 0a 09 61 73 73 65 72 74 5f 6e 65 28 20 6e  );..assert_ne( n
0e60: 65 77 20 54 65 6d 70 28 31 2c 22 66 6f 6f 22 29  ew Temp(1,"foo")
0e70: 2c 20 6e 65 77 20 54 65 6d 70 28 31 2c 22 62 61  , new Temp(1,"ba
0e80: 72 22 29 20 29 3b 0a 09 61 73 73 65 72 74 5f 67  r") );..assert_g
0e90: 74 28 20 6e 65 77 20 54 65 6d 70 28 31 2c 22 66  t( new Temp(1,"f
0ea0: 6f 6f 22 29 2c 20 6e 65 77 20 54 65 6d 70 28 31  oo"), new Temp(1
0eb0: 2c 22 62 61 72 22 29 20 29 3b 0a 09 61 73 73 65  ,"bar") );..asse
0ec0: 72 74 5f 6c 74 28 20 6e 65 77 20 54 65 6d 70 28  rt_lt( new Temp(
0ed0: 31 2c 22 66 6f 6f 22 29 2c 20 6e 65 77 20 54 65  1,"foo"), new Te
0ee0: 6d 70 28 32 2c 22 62 61 72 22 29 20 29 3b 0a 09  mp(2,"bar") );..
0ef0: 61 73 73 65 72 74 5f 67 65 28 20 6e 65 77 20 54  assert_ge( new T
0f00: 65 6d 70 28 31 2c 22 66 6f 6f 22 29 2c 20 6e 65  emp(1,"foo"), ne
0f10: 77 20 54 65 6d 70 28 31 2c 22 66 6f 6f 22 29 20  w Temp(1,"foo") 
0f20: 29 3b 0a 0a 09 63 6c 61 73 73 20 54 65 6d 70 44  );...class TempD
0f30: 75 6d 6d 79 0a 09 7b 0a 09 09 69 6e 74 20 78 3b  ummy..{...int x;
0f40: 0a 09 09 73 74 72 69 6e 67 20 79 3b 0a 09 09 6d  ...string y;...m
0f50: 69 78 69 6e 20 53 69 6d 70 6c 65 43 6f 6e 73 74  ixin SimpleConst
0f60: 72 75 63 74 6f 72 3b 0a 09 09 6d 69 78 69 6e 20  ructor;...mixin 
0f70: 53 69 6d 70 6c 65 43 6f 6d 70 61 72 65 3b 0a 09  SimpleCompare;..
0f80: 7d 0a 09 61 73 73 65 72 74 5f 74 68 72 6f 77 21  }..assert_throw!
0f90: 41 73 73 65 72 74 45 72 72 6f 72 28 20 6e 65 77  AssertError( new
0fa0: 20 54 65 6d 70 28 31 2c 22 66 6f 6f 22 29 20 3d   Temp(1,"foo") =
0fb0: 3d 20 6e 65 77 20 54 65 6d 70 44 75 6d 6d 79 28  = new TempDummy(
0fc0: 31 2c 22 66 6f 6f 22 29 20 29 3b 0a 09 61 73 73  1,"foo") );..ass
0fd0: 65 72 74 5f 74 68 72 6f 77 21 41 73 73 65 72 74  ert_throw!Assert
0fe0: 45 72 72 6f 72 28 20 6e 65 77 20 54 65 6d 70 28  Error( new Temp(
0ff0: 31 2c 22 66 6f 6f 22 29 20 3c 3d 20 6e 65 77 20  1,"foo") <= new 
1000: 54 65 6d 70 44 75 6d 6d 79 28 31 2c 22 66 6f 6f  TempDummy(1,"foo
1010: 22 29 20 29 3b 0a 7d 0a 0a 2f 2f 2f 20 4d 69 78  ") );.}../// Mix
1020: 69 6e 67 2d 69 6e 20 61 20 73 69 6d 70 6c 65 20  ing-in a simple 
1030: 74 6f 53 74 72 69 6e 67 20 6d 65 74 68 6f 64 0a  toString method.
1040: 0a 2f 2a 6d 69 78 69 6e 2a 2f 0a 74 65 6d 70 6c  ./*mixin*/.templ
1050: 61 74 65 20 53 69 6d 70 6c 65 54 6f 53 74 72 69  ate SimpleToStri
1060: 6e 67 28 29 0a 7b 0a 09 6f 76 65 72 72 69 64 65  ng().{..override
1070: 20 73 74 72 69 6e 67 20 74 6f 53 74 72 69 6e 67   string toString
1080: 28 29 0a 09 7b 0a 09 09 73 74 72 69 6e 67 20 73  ()..{...string s
1090: 74 72 20 3d 20 73 70 72 69 6e 74 66 21 22 25 73  tr = sprintf!"%s
10a0: 28 22 28 74 79 70 65 6f 66 28 74 68 69 73 29 2e  ("(typeof(this).
10b0: 73 74 72 69 6e 67 6f 66 29 3b 0a 09 09 66 6f 72  stringof);...for
10c0: 65 61 63 68 28 69 2c 6d 65 6d 3b 20 74 68 69 73  each(i,mem; this
10d0: 2e 74 75 70 6c 65 6f 66 29 0a 09 09 7b 0a 09 09  .tupleof)...{...
10e0: 09 69 66 28 69 29 20 73 74 72 20 7e 3d 20 22 2c  .if(i) str ~= ",
10f0: 22 3b 0a 09 09 09 73 74 61 74 69 63 20 69 66 28  ";....static if(
1100: 20 69 73 28 74 79 70 65 6f 66 28 6d 65 6d 29 20   is(typeof(mem) 
1110: 3d 3d 20 73 74 64 2e 62 69 67 69 6e 74 2e 42 69  == std.bigint.Bi
1120: 67 49 6e 74 29 20 29 0a 09 09 09 09 73 74 72 20  gInt) ).....str 
1130: 7e 3d 20 73 74 64 2e 62 69 67 69 6e 74 2e 74 6f  ~= std.bigint.to
1140: 44 65 63 69 6d 61 6c 53 74 72 69 6e 67 28 6d 65  DecimalString(me
1150: 6d 29 3b 0a 09 09 09 65 6c 73 65 0a 09 09 09 09  m);....else.....
1160: 73 74 72 20 7e 3d 20 73 70 72 69 6e 74 66 21 22  str ~= sprintf!"
1170: 25 73 22 28 6d 65 6d 29 3b 0a 09 09 7d 0a 09 09  %s"(mem);...}...
1180: 72 65 74 75 72 6e 20 73 74 72 20 7e 20 22 29 22  return str ~ ")"
1190: 3b 0a 09 7d 0a 7d 0a 0a 76 65 72 73 69 6f 6e 28  ;..}.}..version(
11a0: 75 6e 69 74 74 65 73 74 29 20 69 6d 70 6f 72 74  unittest) import
11b0: 20 73 74 64 2e 62 69 67 69 6e 74 3b 0a 75 6e 69   std.bigint;.uni
11c0: 74 74 65 73 74 0a 7b 0a 09 63 6c 61 73 73 20 54  ttest.{..class T
11d0: 65 6d 70 0a 09 7b 0a 09 09 69 6e 74 20 78 3b 0a  emp..{...int x;.
11e0: 09 09 73 74 72 69 6e 67 20 79 3b 0a 09 09 42 69  ..string y;...Bi
11f0: 67 49 6e 74 20 7a 3b 0a 09 09 6d 69 78 69 6e 20  gInt z;...mixin 
1200: 53 69 6d 70 6c 65 43 6f 6e 73 74 72 75 63 74 6f  SimpleConstructo
1210: 72 3b 0a 09 09 6d 69 78 69 6e 20 53 69 6d 70 6c  r;...mixin Simpl
1220: 65 54 6f 53 74 72 69 6e 67 3b 0a 09 7d 0a 09 61  eToString;..}..a
1230: 73 73 65 72 74 5f 65 71 28 20 28 6e 65 77 20 54  ssert_eq( (new T
1240: 65 6d 70 28 31 2c 22 66 6f 6f 22 2c 42 69 67 49  emp(1,"foo",BigI
1250: 6e 74 28 34 32 29 29 29 2e 74 6f 53 74 72 69 6e  nt(42))).toStrin
1260: 67 28 29 2c 20 22 54 65 6d 70 28 31 2c 66 6f 6f  g(), "Temp(1,foo
1270: 2c 34 32 29 22 20 29 3b 0a 7d 0a 0a 2f 2f 2f 20  ,42)" );.}../// 
1280: 45 76 65 72 79 74 68 69 6e 67 20 69 73 20 69 6e  Everything is in
1290: 0a 0a 2f 2a 6d 69 78 69 6e 2a 2f 0a 74 65 6d 70  ../*mixin*/.temp
12a0: 6c 61 74 65 20 53 69 6d 70 6c 65 43 6c 61 73 73  late SimpleClass
12b0: 28 29 0a 7b 0a 09 6d 69 78 69 6e 20 53 69 6d 70  ().{..mixin Simp
12c0: 6c 65 43 6f 6e 73 74 72 75 63 74 6f 72 3b 0a 09  leConstructor;..
12d0: 6d 69 78 69 6e 20 53 69 6d 70 6c 65 43 6f 6d 70  mixin SimpleComp
12e0: 61 72 65 3b 0a 09 6d 69 78 69 6e 20 53 69 6d 70  are;..mixin Simp
12f0: 6c 65 54 6f 53 74 72 69 6e 67 3b 0a 7d 0a        leToString;.}.