There's a (slightly hackish) way you can accomplish this right now:
const _ = uint(foo - 2)
The compiler will throw an error if foo - 2 is negative. It will also
throw an error if foo is really big.
I'll leave it to someone else to comment on the feasibility/likelihood
of adding some kind of compile-time assertion mechanism to the
language.
- Evan
The problem is what runtime checks would it transform? Also this prevents the compile time checks from being in any kind of conditional statement.
Panic() is the obvious choice, so is there a situation where you would want a panic() built in to the main program flow?
func main() {
for {
foo()
}
panic("i should never get here")
}
> I'm looking for an ideomatic way of doing in go what i would do (in C):
> #if foo <= 1
> #error foo must be greater than 1
> #endif
Would you have a more concrete example of what "foo" would be here?
> to generate a compile-time error, rather than it going all of the way
> through the compilation then hitting a test and failing. This is especially
> useful in large projects, where you could halt compilation midway instead of
Is this a real issue you're facing? Even for large projects,
compilation is quite fast.
--
Gustavo Niemeyer
http://niemeyer.net
http://niemeyer.net/plus
http://niemeyer.net/twitter
http://niemeyer.net/blog
-- I never filed a patent.
cgo may, however, invalidate the above reasoning.
in need of much more prozac than we are now.
ron
Without conditional compilation, it is "impossible" to write a piece of Go code which will be interpreted differently by (the same compiler on) another system, which vastly increases the confidence a developer can have in his application on a target system. If we added them, then people would ask for command-line #defines and then someone would write ./configure and then where would we be?
What I was looking for was a way to break compilation partway through on a conditional, NOT conditional compilation (yes i do know all the horrible nasty breaks that causes).
This short circuit could save you compiling the remainder of a (very) large project, and running through the testing suite.
Split it into separate files and do it in the build system. Nicer
organised when one file is always entirely in or out of the build, and
avoids the need for a preprocessor, but can do the same job.