code generation like in Java

L

Lars Schouw

I am trying to port some code over from Java but are having problems.
In Java you can define code on the when you override and call it at the same place.
For example in Java you can do this:

public static Pipe newPipe(final InputStream in)
{
final CodedInput codedInput = new CodedInput(in, false);
return new Pipe()
{
protected Input begin(Pipe.Schema pipeSchema) throws IOException
{
return codedInput;
}
protected void end(Pipe.Schema pipeSchema, Input input,
boolean cleanupOnly) throws IOException
{
if(cleanupOnly)
return;

//assert input == codedInput;
}
};
}

where Pipe is defined as
public abstract class Pipe
{

protected abstract Input begin(Pipe.Schema pipeSchema) throws IOException;
protected abstract void end(Pipe.Schema pipeSchema, Input input,
boolean cleanupOnly) throws IOException;
}

How can I do this in C#?

Lars
 
A

Arne Vajhøj

I am trying to port some code over from Java but are having problems.
In Java you can define code on the when you override and call it at the same place.
For example in Java you can do this:

public static Pipe newPipe(final InputStream in)
{
final CodedInput codedInput = new CodedInput(in, false);
return new Pipe()
{
protected Input begin(Pipe.Schema pipeSchema) throws IOException
{
return codedInput;
}
protected void end(Pipe.Schema pipeSchema, Input input,
boolean cleanupOnly) throws IOException
{
if(cleanupOnly)
return;

//assert input == codedInput;
}
};
}

where Pipe is defined as
public abstract class Pipe
{

protected abstract Input begin(Pipe.Schema pipeSchema) throws IOException;
protected abstract void end(Pipe.Schema pipeSchema, Input input,
boolean cleanupOnly) throws IOException;
}

How can I do this in C#?

Java has anonymous classes.

C# has anonymous methods.

So the Java code:

public class Anonymous {
public static interface Foo {
public void foo();
}
public static interface FooBar {
public void foo();
public void bar(int a);
}
public static void m1(Foo i) {
i.foo();
}
public static void m2(FooBar i) {
i.foo();
i.bar(1);
}
public static void main(String[] args) {
final int v1 = 1;
final int v2 = 2;
m1(new Foo() {
public void foo() {
System.out.println(v1);
}
});
m2(new FooBar() {
public void foo() {
System.out.println(v1);
}
public void bar(int a) {
System.out.println(v2 + a);
}
});
}
}

becomes the C# code:

using System;

namespace E
{
public delegate void Foo();
public delegate void Bar(int a);
public class Anonymous
{
public static void M1(Foo f)
{
f();
}
public static void M2(Foo f, Bar b)
{
f();
b(1);
}
public static void Main(string[] args)
{
int v1 = 1;
int v2 = 2;
M1(() => Console.WriteLine(v1));
M2(() => Console.WriteLine(v1), (a) => Console.WriteLine(v2 + a));
}
}
}

(there are a couple of other ways of doing it in C#, but I think
this is the best translation)

Arne
 
A

Arne Vajhøj

However, note that the majority of the time in Java, the class being
implemented anonymously implements just a single member, usually of an
interface. As such, anonymous methods in C# do the job just fine, where
a delegate type takes the place of the single-member interface that was
used in Java (see Arne's examples).

For anything more complicated, I'd argue that an anonymous type in Java
probably was not the best choice in the first place. Methods that have
within them embedded code can be hard to follow anyway, but this gets
even worse when there are multiple methods.

It is common in Java especially in Swing to do it that way.
It's one of the reasons that the lambda syntax for anonymous methods in
C# can be so nice, as it takes what could be a lot of clutter and tidies
it up a bit.

As useful as anonymous methods can be, with features like variable
capturing (a nice improvement over the Java requirement that referenced
variables must be "final"), in both Java and C# if the anonymous
class/method is starting to get too large, it's probably time to start
thinking about moving all that code over to a dedicated class.

The Java final requirements mean that there are never any doubt about
how the variable is used.

It happens in C# that captured variables has a different value than
what the developer thought.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top