По-русски Notes

Monday, June 28, 2004

.NET architecture...

I have reread .NET architecture specification and other related documents on the weekend. The architecture does not provide any means to implement metaclasses. That is why I don’t ask to add this feature to C# further more. The problem is classes in .NET were static and they were not created with a constructor of class Type. Disappointing. Haiku?

I have reread .NET architecture specification and other related documents on the weekend. The architecture does not provide any means to implement metaclasses. That is why I don’t ask to add this feature to C# further more. The problem is classes in .NET were static and they were not created with a constructor of class Type. Disappointing. Haiku?

Another interesting feature is static members of interfaces. It’s possible to provide implementation for static methods of interfaces, but this will impact CLS-compliance. Do we need this?

I wonder why authors of C# did not include exception filters. Those are supported by the framework. Anonymous delegates will simplify the process.

more...

Friday, June 25, 2004

Tuples

Often they request tuples support in C#. Actually I will remove all the out parameters from my APIs as soon as tuples have been introduced to C#. But what do we really need?

Often they request tuples support in C#. Actually I will remove all the out parameters from my APIs as soon as tuples have been introduced to C#. But what do we really need?

Tuple is an immutable array of objects. "Objects" means instances of type System.Object. Thus method returning tuple has type object[]. This behavior does not require any language change. If someone wishes the method to return something like (int x, int y, Color c) he|she should rethink the model and declare

struct Pixel{ int x, y; Color c }

What tuples actually provide is multiple assignment:

int [] F( string str )
{
string [] ss = str.Split();
int [] result = new int[ ss.Length ];
for( int i = 0; i < ss.Length; i++ )
result[i] = int.Parse(ss[i]);
return result;
}
int a, b;
(a,b) = F( "3 5" ); // assures a=3 and b=5;
(a,b) = (b,a); // swap a and b

That is unrolling of array to a sequence of variables with casting and coercion if needed. Reverse operation is needed to support the last statement of swapping two variables. That is the comma operator which combines objects to an array.

One question is what should happen in the following situation:

(a,b) = F( "3 5 7 9" );

Extra objects could be dropped or an exception could be raised. In the latter case the exception should be thrown after assigning to a and b. Thus there will be a chance to simply ignore the exception:

try { (a,b) = F( "3 5 7 9" ); }
catch(ExtraObectException){};

There could be a way of handling all the extra objects with an array:

int [] others;
(a,b,others) = F( "3 5 7 9" );
// a=3 and b=5 and others = new int[]{7,9}

I have placed parenthesis around tuples a,b after I had written the article. I feel the need of the parenthesis to prevent ambiguity in method calls:

void M( int [] );
void M( int, int, int );
M( 3,4,5 );
M( (3,4,5) );

And one note: IMO tuples are not needful.

more...

Thursday, June 24, 2004

Formatting

The task is to apply colors, fonts, and layout attributes to blocks of text accoring to some patterns. It's common practice to write text patterns in form of regular expressions. Thus we could define style for document as a set of pairs of a regular expression and formatting description.

The task is to apply colors, fonts, and layout attributes to blocks of text accoring to some patterns. It's common practice to write text patterns in form of regular expressions. Thus we could define style for document as a set of pairs of a regular expression and formatting description.

But C# code is structured text. There is a working solution of structured text formatting, that is XML with style sheets. While C# code is not XML, editor could treat the code as XML internally and apply style sheets, and then render the result on screen. Equivalent XML could look like this

<comment>An example of class</comment>
<class>
<keyword>public</keyword>
<keyword>class</keyword> Class1
{
<method>
<keyword>public</keyword>
<keyword>static</keyword>
<MethodName>Main</MethodName>()
{
<MethodBody>
Console.WriteLine("Hello World");
</MethodBody>
} </method>
} </class>

CSS can use regular expressions as well, thus providing possibility of using regular expressions only to describe formatting rules.

Options dialog present in IDE could provide a limited set of funstionality to simplify the process of formatting. Advanced users can type their own style sheets or create them with third party tools.

more...

Wednesday, June 23, 2004

foreach statement

using <class> is like with <an object> in Pascal. And I think there is a situation where such a feature will be useful.

using <class> is like with <an object> in Pascal. And I think there is a situation where such a feature will be useful.

The foreach statement always involves declaration of a local variable:

foreach( XmlElement element
in doc.SelectNodes(xpath) )
{
Console.WriteLine( "{0} of type {1}",
element.Name,
element.GetAttribute("Type"));
}

How often You name the variable with i? While there is always the variable in the foreach statement it's possible simply omit its name:

foreach( XmlElement in doc.SelectNodes(xpath) )
{
Console.WriteLine( "{0} of type {1}",
Name, GetAttribute("Type") );
}

more...

Metaclass in C#

I'm writing a class library and have to implement a feature close to XML serialization. The problem is I am not that guy who declares the scheme for serialized data. And this scheme may be expanded and extended in the future and certainly I wish to reuse the code...

I'm writing a class library and have to implement a feature close to XML serialization. The problem is I am not that guy who declares the scheme for serialized data. And this scheme may be expanded and extended in the future and certainly I wish to reuse the code. The scheme is designed such that there is no common algorithm to determine classname for object represented with an element. The classes are arranged in groups. Every group has its own algorithm to determine class or subgroup to which the obect belongs. If the algorithm detects only a subgroup then it calls to corresponding algorithm of that group.

For example, we have four classes B, C, E, F arranged in two groups A and D. Group D contains classes E and F, while A contains classes B and C and gourp D. I have defined the groups as highlevel classes in the hierarhy, while leaves are actual classes.

  • A
    • B
    • C
    • D
      • E
      • F

In XML objects look like

<B>B instance </B>
<C>C instance</C>
<D Type=”E”></D>
<D Type=”F”></D>

Every highlevel class has static method for the detection algorithm. I use reflection to obtain the method and transfer control to it. But I’d like to solve this task in the following way.

class AClass : Type
{
object CreateInstance( XmlElement e )
{
AClass C = Detect( e );
return C.InvokeMember( CreateInstance, new object[]{e} );
}
virtual AClass Detect( XmlElement e )
{
return this;
}
}
class DClass : AClass
{
override AClass Detect( XmlElement e )
{
AClass C = (AClass) Type.GetType( “MyNamespace.GroupA.GroupD.” + e.GetAttribute(“Type”) );
return C.Detect(e);
}
}
[Metaclass(typeof(AClass))]
abstract class A
{
A( XmlElement e ){...}
}
class B : A {...}
class C : A {...}
[Metaclass(typeof(DClass))]
class D : A{...}
namespace GroupD{
[Metaclass(typeof(AClass))]
class E : D {...}
[Metaclass(typeof(AClass))]
class F: D {...}
}

The loading object from XMl would look like

XmlElement e = (XmlElement) someXmlDoc.SelectSingleNode( someXPath );
AClass C = (AClass) Type.GetType( “MyNamespace.GroupA.” + e.Name );
A a =(A) C.CreateInstance( e );

The only new thig is MetaclassAttribute wich provides metaclass for the class being declared.

Certaily I could create some helper objects, containig detection methods. But this approach woul require creating of instances of those detection classes. Why should I instantiate new objects while .NET framework already created objects for the classes? I think the class objects should carry all the information on classes they define. The information could include some progrmmer-defined items. C# has already static memrebs. They in fact are pieces of such programmer's data on the class. MetaclassAttribute will generalize this feature.

We have run-time type information provided by .NET framework. Then we should use this information, because it's too costly to handle it for nothing.

Imagine a plug-in anabled application. It includes a plug-in management code. But the only thing a plug-in does was providing a managed class to the application. We use classes to simplify object management. But when classes became objects themselves we could use the same tool to simplify managing classes.

Another feature is the possibility to restrict method parameters of type Type to a specific set of classes.

more...