top | item 44034155

(no title)

bpshaver | 9 months ago

What is an example of a compiler that flat out refuses to run (compile) your code? Obviously Python is not an example. The other language I know best is Rust, where as I understand the compiler doesn't refuse to compile your code, it cannot compile your code. Is there a language where the compiler could compile your code but refuses to do so unless the types are all correct?

discuss

order

Jtsummers|9 months ago

Not that you should ever write this in any language, but as an illustration:

  fn main() {
    let x: i32 = if true {1} else {"3"};
    println!("{}", x);
  }
This will not compile even though if it were allowed to execute it would, correctly, assign an integer to x. Python will happily interpret its equivalent:

  x = 1 if True else "3"
  print(x)
Even giving the if-expression an explicit `true` constant for the condition, Rust won't accept that as a valid program even though we can prove that the result of the expression is always 1.

lisper|9 months ago

> the compiler doesn't refuse to compile your code, it cannot compile your code

"Can not" and "will not" are kind of the same thing in this context. It's not like compilers have free will and just decide to give you a hard time. It's the language design that makes it (im)possible to run code that won't type-check.

bpshaver|9 months ago

Thanks for clarifying that compilers don't have free will. I was being facetious, sorry.

steveklabnik|9 months ago

Haskell has -fdefer-type-errors, which makes any type error fail at runtime.

This means you can only really rely on the parts of your program which are type correct, so...