-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support F#ish object expression or Java#ish anonymous clases #120
Comments
Hmm, this is the sort of thing that requires non-local transformation (as the usual in-place macro transformations are useless), and I have done these in the past with top-of-file macros like Edit: I wrote this response without noticing that it doesn't actually do what you wanted. You wanted to define a class inside a method that would edit a local variable of the same method. That's a tough requirement that I haven't attempted to fulfill. The problem can be broken down into two independent macros. The first macro is a non-local transformation called outsideCurrentMethod {
// gee, this class is useless, but it's just an example
class MyIDisposable : IDisposable {
bool disposed = false;
public void Dispose() => disposed = true;
};
}
using var x = new MyIDisposable(); Its job would be to make move something from inside a method to outside a method (and there could be related macros like The second macro would be a local transformation giving you what you actually want, an anonymous object. The syntax you suggested is not viable in EC#, but you could use a syntax like this: // Note 1: I changed your example to use conventional `using` syntax
// Note 2: you should really just use on_finally for this sort of thing
using (var x = anon_object(IDisposable) {
// I'm assuming we'll make a reference the outer class available
public void Dispose() => outer.disposed = true;
}) {
DoStuffWith(x);
} I'd like to see #ecs;
define anon_object($(..bases), { $(..content); }) {
#runSequence {
outsideCurrentMethod {
class InnerClassunique# : $bases {
#outerTypeName outer;
public InnerClassunique#(#outerTypeName outer) => this.outer = outer; //
$content;
}
}
new InnerClassunique#(this);
}
}
using (var x = anon_object(IDisposable) { public void Dispose() => outer.disposed = true; })
DoSomethingWith(x); While trying this macro I ran into more bugs in And of course one also needs a macro to transform Might you care to help out by attempting to write the latter macro? If so I can give you tips. |
@qwertie I need some time to process it. |
Basically I always wanted this for the in-place
IDisposable
implementation, but other things could be useful too, e.g. quick Stub implementations in tests:The text was updated successfully, but these errors were encountered: