Hacker Read top | best | new | newcomments | leaders | about | bookmarklet login

The compiler checks that you don't write code where the caller disobeys that restriction :p


sort by: page size:

It literally isn't enforced by the compiler, because I can compile code which is invalid.

Because the compiler won't let you, except under duress.

And what is to prevent someone from checking in code that doesn't compile?

Your code dictates the compiler's behavior. If you grant the compiler flexibility, it's unreasonable to complain about unexpected results.

One of my professors in college wouldn't accept code that didn't compile with the invocation.

"Your compiler is trying to help you. Listen to it."


Then how come I get a compiler error if I don't check the result?

The compiler can test the validity of your code, not it's behavior. Only the most trivial bugs can be caught this way.

Isn't the compiler supposed to protect us? /s

Totally agree, which is why I was baffled when I saw this code. But it goes to show that if something isn't enforced at the compiler level, "clever" developers will find a way around it.

Unsure. I think it's really important that people know the rules _can_ be broken if you think you know better than the compiler.

Thanks. :)


The compiler isn't so much strict in a pedantic sense. It more nudges you to avoid bugs, in a "you may want to rethink this" way. It's actually quite nice to have someone point out potential bugs in your code.

That would be the relevant example.

Compile-time code restriction seems dumb. Programers can write any program they wish, stopping them for some ideological reason isnt effective. As with all the half-baked metaprogramming people restort to anyway.


> using a compiler that's going to do all sorts of modifications to what you've created at the machine code level

You do know that the compiler isn't allowed to change the behaviour of your code, right?

(If it did, it would be a bug in the compiler, and C compilers are pretty damn mature.)


"It is not a compiler error, it's your code"

That's exactly my point: when the compiler can decide not to do something you told it to you don't have full explicit control over everything. That's not a bad thing though - the compiler usually makes good decisions on your behalf. Usually.

The problem with the compiler is that it implements a language whose rules, applied to the code I just wrote, yield a bug. That this language is consistent with the spec makes it no less a problem.

My point is that I can change what I pass in in my code, and the compile time check doesn't catch it. I haven't "fundamentally missed" anything, it's you misreading my post, thanks.

What's wrong with a compiler error? "You are calling a function (Do) that is only defined by calling a function (NeverCalled) that we can't guarantee you ever call"

Yeah, in theory. In practice, if someone writes code that is throwing away compiler checks for no reason, you simply don't use it.
next

Legal | privacy