Giter Site home page Giter Site logo

Comments (2)

qwertie avatar qwertie commented on July 26, 2024 1

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.

dadhi avatar dadhi commented on July 26, 2024

@qwertie I need some time to process it.

from ecsharp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.