GithubHelp home page GithubHelp logo

Comments (7)

qwertie avatar qwertie commented on May 31, 2024

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.

furesoft avatar furesoft commented on May 31, 2024

hmm, ive tried your example but it does not work. the generated file is only the same as the source file

from ecsharp.

qwertie avatar qwertie commented on May 31, 2024

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.

furesoft avatar furesoft commented on May 31, 2024

i use the latest leMp version

from ecsharp.

furesoft avatar furesoft commented on May 31, 2024

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.

qwertie avatar qwertie commented on May 31, 2024

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.

qwertie avatar qwertie commented on May 31, 2024

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)

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.