/* polymorphic linked lists with a mixture of generics and inheritance */ using System; // want: linked list with both natural and ad-hoc polymorphic functions public interface addable { T add(T other); } public class integer : addable { internal int x; // internal means public within same assembly public integer(int y) {x=y;} // constructor public integer add(integer other) { return new integer(other.x+x); } public override string ToString() { return ""+x; } } public class rational : addable { internal int n, d; // numerator and denominator public rational(int a, int b) { n=a; d=b; } public rational add(rational R) { int sd = R.d * d; int sn = n*R.d + R.n*d; return new rational(sn,sd); } public override string ToString() { return n+"/"+d; } } // polymorphic linked list class public class cell where T : addable { internal T car; internal cell cdr; public cell(T a, cell b) {car=a; cdr=b;} // constructor // naturally polymorphic function: public int length() { int ax = 0; for(cell i=this; i!=null; i=i.cdr) ax++; return ax; } // ad-hoc polymorphic function public T sum() { T ax = car; for(cell i=cdr; i!=null; i=i.cdr) ax = ax.add(i.car); return ax; } }// cell // compile to .dll with csc /t:library polylists.cs /* generated .Net intermediate language (.Net byte code) ==================================================================== // Microsoft (R) .NET Framework IL Disassembler. Version 2.0.50727.42 // Copyright (c) Microsoft Corporation. All rights reserved. // Metadata version: v2.0.50727 .assembly extern mscorlib { .publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4.. .ver 2:0:0:0 } .assembly polylists { .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( 01 00 08 00 00 00 00 00 ) .custom instance void [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78 // ....T..WrapNonEx 63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01 ) // ceptionThrows. .hash algorithm 0x00008004 .ver 0:0:0:0 } .module polylists.dll // MVID: {88F29DEA-83EA-4885-BC8C-0A05D5C1DB9C} .imagebase 0x00400000 .file alignment 0x00000200 .stackreserve 0x00100000 .subsystem 0x0003 // WINDOWS_CUI .corflags 0x00000001 // ILONLY // Image base: 0x01130000 // =============== CLASS MEMBERS DECLARATION =================== .class interface public abstract auto ansi addable`1 { .method public hidebysig newslot abstract virtual instance !T 'add'(!T other) cil managed { } // end of method addable`1::'add' } // end of class addable`1 .class public auto ansi beforefieldinit integer extends [mscorlib]System.Object implements class addable`1 { .field assembly int32 x .method public hidebysig specialname rtspecialname instance void .ctor(int32 y) cil managed { // Code size 17 (0x11) .maxstack 8 IL_0000: ldarg.0 IL_0001: call instance void [mscorlib]System.Object::.ctor() IL_0006: nop IL_0007: nop IL_0008: ldarg.0 IL_0009: ldarg.1 IL_000a: stfld int32 integer::x IL_000f: nop IL_0010: ret } // end of method integer::.ctor .method public hidebysig newslot virtual final instance class integer 'add'(class integer other) cil managed { // Code size 24 (0x18) .maxstack 2 .locals init (class integer V_0) IL_0000: nop IL_0001: ldarg.1 IL_0002: ldfld int32 integer::x IL_0007: ldarg.0 IL_0008: ldfld int32 integer::x IL_000d: add IL_000e: newobj instance void integer::.ctor(int32) IL_0013: stloc.0 IL_0014: br.s IL_0016 IL_0016: ldloc.0 IL_0017: ret } // end of method integer::'add' .method public hidebysig virtual instance string ToString() cil managed { // Code size 27 (0x1b) .maxstack 2 .locals init (string V_0) IL_0000: nop IL_0001: ldstr "" IL_0006: ldarg.0 IL_0007: ldfld int32 integer::x IL_000c: box [mscorlib]System.Int32 IL_0011: call string [mscorlib]System.String::Concat(object, object) IL_0016: stloc.0 IL_0017: br.s IL_0019 IL_0019: ldloc.0 IL_001a: ret } // end of method integer::ToString } // end of class integer .class public auto ansi beforefieldinit rational extends [mscorlib]System.Object implements class addable`1 { .field assembly int32 n .field assembly int32 d .method public hidebysig specialname rtspecialname instance void .ctor(int32 a, int32 b) cil managed { // Code size 24 (0x18) .maxstack 8 IL_0000: ldarg.0 IL_0001: call instance void [mscorlib]System.Object::.ctor() IL_0006: nop IL_0007: nop IL_0008: ldarg.0 IL_0009: ldarg.1 IL_000a: stfld int32 rational::n IL_000f: ldarg.0 IL_0010: ldarg.2 IL_0011: stfld int32 rational::d IL_0016: nop IL_0017: ret } // end of method rational::.ctor .method public hidebysig newslot virtual final instance class rational 'add'(class rational R) cil managed { // Code size 55 (0x37) .maxstack 3 .locals init (int32 V_0, int32 V_1, class rational V_2) IL_0000: nop IL_0001: ldarg.1 IL_0002: ldfld int32 rational::d IL_0007: ldarg.0 IL_0008: ldfld int32 rational::d IL_000d: mul IL_000e: stloc.0 IL_000f: ldarg.0 IL_0010: ldfld int32 rational::n IL_0015: ldarg.1 IL_0016: ldfld int32 rational::d IL_001b: mul IL_001c: ldarg.1 IL_001d: ldfld int32 rational::n IL_0022: ldarg.0 IL_0023: ldfld int32 rational::d IL_0028: mul IL_0029: add IL_002a: stloc.1 IL_002b: ldloc.1 IL_002c: ldloc.0 IL_002d: newobj instance void rational::.ctor(int32, int32) IL_0032: stloc.2 IL_0033: br.s IL_0035 IL_0035: ldloc.2 IL_0036: ret } // end of method rational::'add' .method public hidebysig virtual instance string ToString() cil managed { // Code size 38 (0x26) .maxstack 3 .locals init (string V_0) IL_0000: nop IL_0001: ldarg.0 IL_0002: ldfld int32 rational::n IL_0007: box [mscorlib]System.Int32 IL_000c: ldstr "/" IL_0011: ldarg.0 IL_0012: ldfld int32 rational::d IL_0017: box [mscorlib]System.Int32 IL_001c: call string [mscorlib]System.String::Concat(object, object, object) IL_0021: stloc.0 IL_0022: br.s IL_0024 IL_0024: ldloc.0 IL_0025: ret } // end of method rational::ToString } // end of class rational .class public auto ansi beforefieldinit cell`1<(class addable`1) T> extends [mscorlib]System.Object { .field assembly !T car .field assembly class cell`1 cdr .method public hidebysig specialname rtspecialname instance void .ctor(!T a, class cell`1 b) cil managed { // Code size 24 (0x18) .maxstack 8 IL_0000: ldarg.0 IL_0001: call instance void [mscorlib]System.Object::.ctor() IL_0006: nop IL_0007: nop IL_0008: ldarg.0 IL_0009: ldarg.1 IL_000a: stfld !0 class cell`1::car IL_000f: ldarg.0 IL_0010: ldarg.2 IL_0011: stfld class cell`1 class cell`1::cdr IL_0016: nop IL_0017: ret } // end of method cell`1::.ctor .method public hidebysig instance int32 length() cil managed { // Code size 35 (0x23) .maxstack 2 .locals init (int32 V_0, class cell`1 V_1, int32 V_2, bool V_3) IL_0000: nop IL_0001: ldc.i4.0 IL_0002: stloc.0 IL_0003: ldarg.0 IL_0004: stloc.1 IL_0005: br.s IL_0012 IL_0007: ldloc.0 IL_0008: ldc.i4.1 IL_0009: add IL_000a: stloc.0 IL_000b: ldloc.1 IL_000c: ldfld class cell`1 class cell`1::cdr IL_0011: stloc.1 IL_0012: ldloc.1 IL_0013: ldnull IL_0014: ceq IL_0016: ldc.i4.0 IL_0017: ceq IL_0019: stloc.3 IL_001a: ldloc.3 IL_001b: brtrue.s IL_0007 IL_001d: ldloc.0 IL_001e: stloc.2 IL_001f: br.s IL_0021 IL_0021: ldloc.2 IL_0022: ret } // end of method cell`1::length .method public hidebysig instance !T sum() cil managed { // Code size 61 (0x3d) .maxstack 2 .locals init (!T V_0, class cell`1 V_1, !T V_2, bool V_3) IL_0000: nop IL_0001: ldarg.0 IL_0002: ldfld !0 class cell`1::car IL_0007: stloc.0 IL_0008: ldarg.0 IL_0009: ldfld class cell`1 class cell`1::cdr IL_000e: stloc.1 IL_000f: br.s IL_002c IL_0011: ldloca.s V_0 IL_0013: ldloc.1 IL_0014: ldfld !0 class cell`1::car IL_0019: constrained. !T IL_001f: callvirt instance !0 class addable`1::'add'(!0) IL_0024: stloc.0 IL_0025: ldloc.1 IL_0026: ldfld class cell`1 class cell`1::cdr IL_002b: stloc.1 IL_002c: ldloc.1 IL_002d: ldnull IL_002e: ceq IL_0030: ldc.i4.0 IL_0031: ceq IL_0033: stloc.3 IL_0034: ldloc.3 IL_0035: brtrue.s IL_0011 IL_0037: ldloc.0 IL_0038: stloc.2 IL_0039: br.s IL_003b IL_003b: ldloc.2 IL_003c: ret } // end of method cell`1::sum } // end of class cell`1 // ============================================================= // *********** DISASSEMBLY COMPLETE *********************** // WARNING: Created Win32 resource file C:\cygwin\home\cliang\csc123\pl.res */