nitlang / nit

Nit language
http://nitlanguage.org
Apache License 2.0
242 stars 67 forks source link

Static ifs #2220

Open lbajolet opened 8 years ago

lbajolet commented 8 years ago

Static ifs are seldom used structures in some languages.

C/C++'s #if/#else macros are some examples of such structures, so are D's static if or their debug/version.

Such structures are useful should someone want to add code which depends on a flag for inclusion in the compiled program.

In our case, this could be an interesting addition for several uses like debugging.

In some cases, each of us has added debug prints to a function, only to remove it before committing. Then when we do go back to a specific module or code snippet to debug it, we do that same operation again, and over and over.

The addition of such structures would simplify this process by adding debug information directly to the code for future use whilst not adding any burden at execution for anyone using the language. The flag used for debugging could be activated through the use of the -D command, or any other flag for that matter.

Of course debug is not the only use case for this, architecture/OS specific code might be enclosed within those (although arguably in our case, we could do it through refinements), etc.

I'm unsure whether anyone ever felt the need for such a feature, which is why I propose it today. Feel free to add any input to this discussion.

ventilooo commented 8 years ago

@R4PaSs : This feature seems interesting and useful. I like the idea of imply use a flag to enable debug ! However, in a magic world where nit would have is own debugger. Does this features going to be deprecated ?

lbajolet commented 8 years ago

The existence of a debugger does not supersede the need for such macros. C/C++ and D all have functional debuggers, and all three of them still support such a structure. In many cases debuggers are too fine grained for efficient bug tracking and prints are often a more efficient way of finding the source of bugs.

jcbrinfo commented 8 years ago

An alternative to conditional compilation is to let the compiler remove dead code. This allows to reuse existing syntax and semantic. Also, other areas may benefit from work done on the optimization phases (maybe). However, this alternative is obviously against the « explicit is better than implicit » principe (but agrees with the « convention over configuration » principe).

lbajolet commented 8 years ago

Yeah, I guess this could do too, but the nice thing with the static if way is complete reusability of code pieces, the dead code strategy implies the uncommenting or adding some more lines to make the debug code alive. With the static if strategy, simply adding a flag at compile time does exactly what we need, which I find easier.

jcbrinfo commented 8 years ago

What I mean by dead code removal is when you have something like this:

if DEBUG then print "foo"

… and the compiler determines that DEBUG is always false, it removes the if.

jcbrinfo commented 8 years ago

So, like with conditional compilation, « simply adding a flag at compile time does exactly what we need […] »

privat commented 8 years ago

Both are fine.

The issue with static ifs is that they add another syntactic layer and all the related semantic processing. And because it is static, it can become complex for the user when weaved with dynamic things.

The issue with dead-code removal is that either the language defines what must be statically removed (so make the specification more complex), or that the user has to live with heuristics and best-effort from the engines. Another issue is how to inform the role of dead code. E.g warnings that target deadcode should not report deadcode because of an unset compilation flag.

if DEBUG then
   print "foo"
#  ^ Warning: dead code
end

The version thing of D is nice. But I'm not sure it is adapted to Nit.