Comments (2)
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 #useSymbols
and #ecs
that preprocess the entire file and then postprocesses it to make the requested transformation(s).
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
:
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 atNamespaceScope
and atOuterScope
).
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 outsideCurrentMethod
, atNamespaceScope
, atOuterScope
included as part of #ecs
, but the second macro is something someone could implement themselves with help from outsideCurrentMethod
:
#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 #useSequenceExpressions
, which I fixed. I also realized that it is necessary to know the name of the outer class - above I've pretended that there is another macro called #outerTypeName
which reports this piece of information, but a simpler approach is to add another argument to anon_object
to explicitly tell it the name of the outer class.
And of course one also needs a macro to transform outsideCurrentMethod
.
Might you care to help out by attempting to write the latter macro? If so I can give you tips.
from ecsharp.
@qwertie I need some time to process it.
from ecsharp.
Related Issues (20)
- Base compileTime on dotnet-script CLI tool instead of Roslyn scripting HOT 11
- Add `define` macro with first argument treated as `this` to enable chaining HOT 29
- Comments before multi-using statements are erased HOT 4
- await fluent operator HOT 1
- Error when transforming the `typeof(X<>)` construct HOT 2
- The error Semicolon': expected Colon for the specific code HOT 2
- Support C# 9 pattern matching HOT 3
- Ref locals cannot have parentheses
- EC#: Syntax error in `Foo<T?>`
- Qualified names of identifier macros not working?
- 'with' or quick binding operator bug HOT 1
- EC#: [return: ...] attribute sometimes causes IndexOutOfRangeException in InternalList
- EC#: `#pragma warning` is not propagated to output file
- EC#: Add support for suffix `!` operator
- EC#: New `?>` operator causes parser errors in code like `X<T?>` HOT 1
- Lemp integration - custom macro is not been called
- Lemp - Match only on attribute name
- Add Deconstruct Method To All LNode types to use c# pattern matching
- FPL Abs bug HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ecsharp.