Changeset 36

Show
Ignore:
Timestamp:
08/14/06 16:33:21 (2 years ago)
Author:
KirkMcDonald
Message:

html_doc: Automatic operator overloading documented

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/html_doc/class_wrapping.html

    r35 r36  
    6363<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> 
    6464 
    65 <h3>Automatic operator overloading</h3> 
     65<h3><a class="anchor" name="opwrap">Automatic operator overloading</a></h3> 
    6666 
    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> 
    6868 
    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> 
    7081 
    7182<p>Suppose we have the following simple class:</p> 
     
    7889    <span class="keyword">this</span>() { m_i = <span class="number">0</span>; } 
    7990    <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; } 
    8092 
    8193    <span class="keyword">int</span> i() { <span class="keyword">return</span> m_i; } 
     
    8496    <span class="keyword">void</span> foo(<span class="keyword">char</span>[] s) { 
    8597        writefln(s, m_i); 
     98    } 
     99 
     100    Foo opAdd(Foo rhs) { 
     101        <span class="keyword">return new</span> Foo(m_i + rhs.m_i); 
    86102    } 
    87103}</pre> 
     
    95111<span class="comment">// Wrap the "i" property</span> 
    96112f.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> 
     114f.init!(tuple!(<span class="keyword">int</span>), tuple!(<span class="keyword">int</span>, <span class="keyword">int</span>)); 
    99115finalize_class(f);</pre> 
    100116 
     
    108124&gt;&gt;&gt; f.foo("Hello! i is ") 
    109125Hello! i is 20 
     126&gt;&gt;&gt; f = Foo(10, 10) 
     127&gt;&gt;&gt; f.i 
     12820 
    110129&gt;&gt;&gt; g = Foo(30) 
    111130&gt;&gt;&gt; g.i 
    11213130 
     132&gt;&gt;&gt; e = f + g 
     133&gt;&gt;&gt; e.i 
     13450 
    113135&gt;&gt;&gt; # We can even subclass our D type 
    114136&gt;&gt;&gt; class MyFoo(Foo): 
  • trunk/html_doc/pyd.css

    r35 r36  
    2626    width: 150px; 
    2727} 
     28/* navbar links */ 
    2829a.nav { 
    2930    text-decoration: none; 
     
    4142    background-color: #d8d8d8; 
    4243} 
     44/* the current navbar link */ 
    4345a.navcur { 
    4446    text-decoration: none; 
     
    5759    background-color: #305880; 
    5860} 
     61/* section header anchors */ 
     62a.anchor { 
     63    color: black; 
     64} 
     65a.anchor:active { 
     66    color: black; 
     67} 
     68a.anchor:hover { 
     69    text-decoration: none; 
     70    background-color: #d8d8d8; 
     71} 
     72/* normal links */ 
    5973a { 
    6074    color: #103860;