Changeset 36
- Timestamp:
- 08/14/06 16:33:21 (2 years ago)
- Files:
-
- trunk/html_doc/class_wrapping.html (modified) (5 diffs)
- trunk/html_doc/pyd.css (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/html_doc/class_wrapping.html
r35 r36 63 63 <p>If you have a class <code>Foo</code>, you can check whether it is wrapped by simply checking whether <code>is_wrapped!(Foo)</code> is true. It is important to note that this is <em>not</em> a <code>const bool</code>, it is a <em>runtime</em> check.</p> 64 64 65 <h3> Automatic operator overloading</h3>65 <h3><a class="anchor" name="opwrap">Automatic operator overloading</a></h3> 66 66 67 <p> <i>Docs coming soon...</i></p>67 <p>Pyd will automatically wrap most of D's operator overload functions with appropriate Python operator overloads. There are some caveats:</p> 68 68 69 <h3>Examples</h3> 69 <ul> 70 <li>Pyd will only automatically wrap the lexically first <i>opFunc</i> defined for a given <i>opFunc</i>. <i>(In the future, I may add a mechanism allowing a user to specifiy a specific overload of an opFunc.)</i></li> 71 <li>The usual rules for function wrapping apply: Only an <i>opFunc</i> whose return type and arguments are <a href="conversion.html">convertable</a> may be wrapped. <i>(The current implementation is pretty dumb: If the lexically first opFunc has an unconvertable return type or argument, the operator overload will still be wrapped, but won't work.)</i></li> 72 </ul> 73 74 <p>At the moment, only the following operator overloads are supported:</p> 75 76 <p><code>opAdd, opSub, opMul, opDiv, opMod, opAnd, opOr, opXor, opShl, opShr, opCat, opAddAssign, opSubAssign, opMulAssign, opDivAssign, opModAssign, opAndAssign, opOrAssign, opXorAssign, opShlAssign, opShrAssign, opCatAssign, opIn_r</code></p> 77 78 <p>Most notably missing from this list are <code>opIndex, opIndexAssign, opSlice, opSliceAssign, opCall</code>, and <code>opApply</code>. Support for these operators is forthcoming. Also missing from the list are <code>opUShr</code> and <code>opUShrAssign</code>. Python does not have an unsigned right-shift operator, so these operator overloads are not supported. (You may still wrap them with a normal method using <code>wrapped_class.def</code>, of course.)</p> 79 80 <h3><a class="anchor" name="examples">Examples</a></h3> 70 81 71 82 <p>Suppose we have the following simple class:</p> … … 78 89 <span class="keyword">this</span>() { m_i = <span class="number">0</span>; } 79 90 <span class="keyword">this</span>(<span class="keyword">int</span> j) { m_i = j; } 91 <span class="keyword">this</span>(<span class="keyword">int</span> j, <span class="keyword">int</span> k) { m_i = j + k; } 80 92 81 93 <span class="keyword">int</span> i() { <span class="keyword">return</span> m_i; } … … 84 96 <span class="keyword">void</span> foo(<span class="keyword">char</span>[] s) { 85 97 writefln(s, m_i); 98 } 99 100 Foo opAdd(Foo rhs) { 101 <span class="keyword">return new</span> Foo(m_i + rhs.m_i); 86 102 } 87 103 }</pre> … … 95 111 <span class="comment">// Wrap the "i" property</span> 96 112 f.prop!(Foo.i, <span class="string">"i"</span>); 97 <span class="comment">// Wrap the constructor .</span>98 f.init!(tuple!(<span class="keyword">int</span>) );113 <span class="comment">// Wrap the constructors.</span> 114 f.init!(tuple!(<span class="keyword">int</span>), tuple!(<span class="keyword">int</span>, <span class="keyword">int</span>)); 99 115 finalize_class(f);</pre> 100 116 … … 108 124 >>> f.foo("Hello! i is ") 109 125 Hello! i is 20 126 >>> f = Foo(10, 10) 127 >>> f.i 128 20 110 129 >>> g = Foo(30) 111 130 >>> g.i 112 131 30 132 >>> e = f + g 133 >>> e.i 134 50 113 135 >>> # We can even subclass our D type 114 136 >>> class MyFoo(Foo): trunk/html_doc/pyd.css
r35 r36 26 26 width: 150px; 27 27 } 28 /* navbar links */ 28 29 a.nav { 29 30 text-decoration: none; … … 41 42 background-color: #d8d8d8; 42 43 } 44 /* the current navbar link */ 43 45 a.navcur { 44 46 text-decoration: none; … … 57 59 background-color: #305880; 58 60 } 61 /* section header anchors */ 62 a.anchor { 63 color: black; 64 } 65 a.anchor:active { 66 color: black; 67 } 68 a.anchor:hover { 69 text-decoration: none; 70 background-color: #d8d8d8; 71 } 72 /* normal links */ 59 73 a { 60 74 color: #103860;
