Comments (7)
Luckily, the example you gave actually doesn't use any new syntax: Enhanced C# supports it already!
In fact, repository hellorep { ... }
is simply a property declaration. And in Enhanced C#, you are allowed to put things inside a property declaration other than get
and set
accessors (such as variables and methods). Therefore, it is already possible to do what you are asking for.
You simply have to tell LeMP that you want to replace repository hellorep { ... }
with public class hellorep : IRepository {...}
. To do this, put the following code at the top of an ecstatic source file:
replace ({
repository $Name {
$(..stuff);
}
} => {
public class $Name : IRepository {
$(..stuff);
}
});
Then you can add code like this (which will change into a class):
repository hellorep {
public void Method () {}
public int Field;
public this() {}
}
You might encounter one small problem: constructors. repository
is technically a property declaration, so the parser is not expecting to see a constructor inside it, and therefore hellorep() {}
will be interpreted as an expression instead of a constructor. This problem is easy to solve, though: the word this
is always interpreted as a constructor name, so you should write this () {}
instead of ClassName() {}
.
It is intentional that Enhanced C# does not support adding new syntax. One of the reasons is that often the syntax you want already exists. Another reason is that adding new syntax to C# creates ambiguity unless it is done very carefully. For example, you might think it is reasonable to add an <-
operator, so you can write Mouth <- Cake
. But this expression already has an interpretation in C#, Mouth < (-Cake)
. So if you were allowed to add a new <-
operator, it would be ambiguous. Instead, I decided it would be better if Enhanced C# created a few extra syntax rules which you are allowed to use in your macros and replace
commands.
from ecsharp.
hmm, ive tried your example but it does not work. the generated file is only the same as the source file
from ecsharp.
What version are you running? (version is written on the first line of output)
It should work in LeMP 2.6.2.0.
from ecsharp.
i use the latest leMp version
from ecsharp.
it works now, but i have another question.
i want to replace the name of the class by $name + "Repository"
ive tried with concatId but this is my result:
public class concatId { ; }
;
`
replace ({
repository $Name {
$(..stuff);
}
} => {
public class concatId($Name, "Repository") : IRepository {
$(..stuff);
}
});
repository Date {
public void Run() {}
}
`
and i have another question, how can i convert my replace macro call to make a new macro that i can use in different projects?
from ecsharp.
class concatId($Name, "Repository")
is a syntax error. Check your error list and make sure you can see the error.
In order to use an expression in a location where an expression is normally not allowed, you must use the $(out _)
macro: in this case, class $(out concatId($Name, Repository))
from ecsharp.
To share commands such as define
and replace
between different source files, you need to put the shared commands in a file and then use the includeFile("shared file name");
command at (or near) the top of other files that will use the shared commands.
from ecsharp.
Related Issues (20)
- Base compileTime on dotnet-script CLI tool instead of Roslyn scripting HOT 11
- Support F#ish object expression or Java#ish anonymous clases HOT 2
- 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 1
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.