PC Review


Reply
Thread Tools Rate Thread

code generation like in Java

 
 
Lars Schouw
Guest
Posts: n/a
 
      14th Jul 2011
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
 
Reply With Quote
 
 
 
 
Arne Vajhøj
Guest
Posts: n/a
 
      15th Jul 2011
On 7/14/2011 11:47 AM, Lars Schouw wrote:
> 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

 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      19th Jul 2011
On 7/14/2011 11:28 PM, Peter Duniho wrote:
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
MS Access metadata repository generation query, matrix generationquery, table-generation query, array-generation query. Roy Roebuck Microsoft Access Database Table Design 1 20th Dec 2008 07:23 PM
C# 2005 how to remove warning "The file 'C:\Main.cs' does not support code parsing or generation because it is not contained within a project that supports code. Serdge Kooleman Microsoft C# .NET 0 9th Feb 2006 05:43 PM
java based pdf and excel generation paulthomasweb@gmail.com Microsoft Excel Misc 1 13th Dec 2005 01:01 AM
Cavaj Java Decompiler - reconstructs java source code from CLASSfiles Vegard Krog Petersen Freeware 0 8th Jul 2004 09:03 PM
code generation woes (need code at END of Initialize Component) Bill Foust Microsoft Dot NET Framework Forms 0 11th Aug 2003 05:01 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:02 PM.