[Mono-list] C# changes

Jonathan Pryor jonpryor@vt.edu
11 Nov 2002 10:03:50 -0500


The Modula-3 example you gave seems similar to how Ada and C++ (sort-of)
handle things: you declare the public interface to the class, and define
the implementation "elsewhere".  (Granted, in C++ the "public interface"
also contains the implementation details, but you don't need to have the
*whole* class implementation in the class declaration, unless it's a
template.)

C# partial types are different.  There is no separation of interface
from implementation.  Instead, the implementation is split up into
separate "partial class" definitions.  For example, the following
partial class definitions:

	partial class Test {
	  public void Foo () {}
	}

	partial class Test {
	  public static void Bar () {}
	}

would be identical to the "normal" class definition:

	class Test {
	  public void Foo () {}
	  public static void Bar () {}
	}

This syntax was taken from the Powerpoint presentation:
http://www.gotdotnet.com/team/csharp/conferences/OOPSLA%202002%20CSharp.ppt

Each partial class can appear in its own file.  I would imagine that the
restriction is that all files that make up a given partial class must be
compiled at the same time, e.g. `csc Test_Foo.cs Test_Bar.cs'.  I doubt
that you could put a partial class into a .netmodule file, and have each
partial class in a separate .netmodule/.dll/.exe combination.

Examples where this would be useful are when the class definition is
just getting too big to be easily manageable (I've had Java class
definitions that were over 30kb in size that couldn't be easily
re-factored), or if you want part of the class interface to be machine
generated.  I would imagine that the VS.NET interface builder will use
partial classes, so that all the machine-modifiable code is within one
file, instead of needing the code generator to be "intelligent" so it
doesn't clobber the changes developers made.

The one thing I dislike about this approach is that they added a new
keyword.  Why didn't they use an Attribute?  Attributes are already used
for both custom attributes (e.g. System.Diagnostics.DebuggableAttribute)
and CIL attributes (e.g. Serializable, MethodImplOptions, ...).  Why not
have a compiler-intrinsic [Partial] attribute?  Still reasonably
consistent, and doesn't need a new keyword.

 - Jon

On Mon, 2002-11-11 at 09:48, Michel Dagenais wrote:
> 
> > Generics = polymorphism or parametric polymorphism in functional
> > languages.
> 
> Like templates in C++ or generic modules/intefaces in Modula-3.
> Interestingly, it looks like it will come out about at the same time as
> the long awaited Java generics.
> 
> > Partial types = one type does not have to be defined in just one file
> 
> The only other example I know of partial types is in Modula-3. You have
> something like:
> 
> (* Interface *)
> TYPE 
>   HashTable <: HashTablePublic;
>   HashTablePublic = OBJECT METHODS
>     (* all public methods here *)
>   END;
> END;
> 
> (* Implementation *)
> REVEAL
>   HashTable = HashTablePublic OBJECT
>     (* All the private fields and methods *)
>   END;
> END;
> 
> This allows you to have the public part really visible to the outside
> and the rest to remain "opaque". There are other more sophisticated uses
> where each module adds a layer of functionality to an object. IMHO from
> the Modula-3 experience it is a very elegant feature but which takes
> quite a bit of time to grasp for many newcomers. It poses some
> implementation challenges as well since the offset of fields and methods
> is not known until all revelations have been seen for a type with
> partial declarations.
> 
> 
> _______________________________________________
> Mono-list maillist  -  Mono-list@ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list