Is that possible to get comments with macro? - macros

I was trying to parse some code and reformat them, but it seems that quote will just ignore the comments.
Is there any way to achieve this? I guess I have to dive into the erlang side?

No, you cannot get code comments inside macros. They never become part of the AST and are discarded still in Elixir's tokenizer.

It seems that comments are handled at the tokenizer level, so the parser will not even see them. The relevant parts from the elixir tokenizer indicate that comments are discarded pretty early in the pipeline. This test case from elixir core tells us the same thing:
comments_test() ->
[{number, {1,1,2}, 1},{eol, {1,3,4}},{number,{2,1,2},2}] = tokenize("1 # Comment\n2").

Related

Eclipse, lambdas and Java 8 templates

Is there a reason the Eclipse content assits doesn't work in/around lambdas? In a normal case Eclipse usually does this after writing a dot:
Which works just fine like anywhere else. However just a couple of lines later I get nothing:
Both objects are of the same type. Unfortunately I use these all the time as they make everything much faster and I don't understand why it works in one lambda and not the other.
Attempting an explanation (you asked for a reason): parsing lambda expressions in Java is a technical challenge, as the Java grammar was not made for parser generators. Code completion, OTOH, inevitably depends on parsing incomplete code, i.e., heuristics must be used to continue parsing after a syntax error. These two just don't nicely cooperate. As a result in some situations your incomplete code will look like garbage to the compiler and hence content assist is not able to figure out, what would be meaningful proposals.
The applied heuristics are constantly being improved. I recommend trying your examples on a recent milestone build. If the problem still exists, you might help the team by filing a bug providing a code example, and describing your expectations and what actual behavior you observe.

Scala formatter - show named parameter

I have a relatively large Scala code base that does not use named parameters for any function/class calls. Rather than going in and manually entering it, which would be a very tedious process, I was looking at a formatter to do the job. The best I found is scalariform, but I'm not sure whether I can even write a rule for something so complex.
I'm curious if anyone has ran into a similar problem and found a powerful formatter.
The Scala Refactoring library might be something you could use. You will need some knowledge of Scala's Abstract Syntax Tree representation.
Why do you want to use named parameters throughout your code base? I like IntelliJ's default which is to suggest to name boolean arguments (only).

scala syntax question :/ and ~

Could anybody help me, and explain what is :/ and then ~ in scala, example:
json = http(:/("api.twitter.com") / "1/users/show.json" <<? Map("screen_name" -> "aloiscochard") >~ { _.getLines.mkString })
from: http://aloiscochard.blogspot.com/2011/05/simple-rest-web-service-client-in-scala.html
In the code that you link to, note the import dispatch._. This imports the dispatch library.
In that library we find an object :/ that has an apply method, so that's what :/("api.twitter.com") means.
Also, there is no ~ in the code, either--only a >~. In Scala, any group of symbols is a method name, which can be used as an operator. So >~ must be a method on something.
Looking around, we find that HandlerVerbs defines a >~ method that will "Handle response as a scala.io.Source, in a block."
To understand what the code does in detail, you need to understand the dispatch library, which I don't.
This library seems to be very DSL-heavy. As such, it may be an excellent choice if you are doing lots and lots of dispatch work (because dispatching hopefully can be done in an intuitive and clean way). But it may be a horrible choice for one-off usage, since you have to be quite familiar with the library to understand what it might be doing (due to the choice of very short method names like >~).

Creating Macro's in C#

Is there a way to create macros in c#
ex:
string checkString = "'bob' == 'bobthebuilder'" (this will be dynamic)
if (##checkString)
//.........
else
//.........
Thanks
No, C# doesn't have macros. You could capture your logic in a delegate and apply that delegate in multiple places, potentially... would that help?
If you could describe the problem you're trying to solve rather than the solution you think you'd like, we may be able to help more.
T4 seems to be gaining traction these days for .NET work. It's not quite what you asked for, but it may be extremely beneficial in some cases (or it may just be a hint down the wrong path).
In most cases, esp. with generics, I do not wish for 'templates' or 'macros' in C# (or Scala). In the example above, you could simply use:
bool sameStuff = "'bob' == 'bobthebuilder'";
...
if (sameStuff) {
...
}
More complex cases can generally be dealt with refactoring methods or using anonymous functions.
Additionally, attributes (while a completely different approach) round out the case for many "traditional" uses of templates.
As mentioned, no, but there are a number of other approaches:
Conditional compilation via #if
Templating via T4 or something else (we use a port of Ned Batchelder's (mentioned) Cog
Aspect-Oriented Programming via something like PostSharp
As Jon said, lots of ways; it'd be better to describe exactly what you want to do.
Short answer: No.
Long answer: You can write a wrapper around the C/C++ compiler's preprocessor.
Most of the syntax will be accepted with the notable exception of #region/#endregion. You can just prefix those with #pragma before processing, and remove the #pragma part afterwards.

How do you define 'unwanted code'?

How would you define "unwanted code"?
Edit:
IMHO, Any code member with 0 active calling members (checked recursively) is unwanted code. (functions, methods, properties, variables are members)
Here's my definition of unwanted code:
A code that does not execute is a dead weight. (Unless it's a [malicious] payload for your actual code, but that's another story :-))
A code that repeats multiple times is increasing the cost of the product.
A code that cannot be regression tested is increasing the cost of the product as well.
You can either remove such code or refactor it, but you don't want to keep it as it is around.
0 active calls and no possibility of use in near future. And I prefer to never comment out anything in case I need for it later since I use SVN (source control).
Like you said in the other thread, code that is not used anywhere at all is pretty much unwanted. As for how to find it I'd suggest FindBugs or CheckStyle if you were using Java, for example, since these tools check to see if a function is used anywhere and marks it as non-used if it isn't. Very nice for getting rid of unnecessary weight.
Well after shortly thinking about it I came up with these three points:
it can be code that should be refactored
it can be code that is not called any more (leftovers from earlier versions)
it can be code that does not apply to your style-guide and way-of-coding
I bet there is a lot more but, that's how I'd define unwanted code.
In java i'd mark the method or class with #Deprecated.
Any PRIVATE code member with no active calling members (checked recursively). Otherwise you do not know if your code is not used out of your scope analysis.
Some things are already posted but here's another:
Functions that almost do the same thing. (only a small variable change and therefore the whole functions is copy pasted and that variable is changed)
Usually I tell my compiler to be as annoyingly noisy as possible, that picks 60% of stuff that I need to examine. Unused functions that are months old (after checking with the VCS) usually get ousted, unless their author tells me when they'll actually be used. Stuff missing prototypes is also instantly suspect.
I think trying to implement automated house cleaning is like trying to make a USB device that guarantees that you 'safely' play Russian Roulette.
The hardest part to check are components added to the build system, few people notice those and unused kludges are left to gather moss.
Beyond that, I typically WANT the code, I just want its author to refactor it a bit and make their style the same as the rest of the project.
Another helpful tool is doxygen, which does help you (visually) see relations in the source tree.. however, if its set at not extracting static symbols / objects, its not going to be very thorough.