13

string name = null; name.ToLower();

In most languages such code would compile. Which languages would catch this and the like errors at compile time ?

The only one I know uptil now is elm: http://elm-lang.org

Tomas
  • 1,377
  • 3
  • 17
  • 32
  • 1
    Related: [Are there languages without "null"?](http://stackoverflow.com/a/34570644/3425536) – Emil Laine Feb 11 '16 at 15:46
  • Are you interested in languages where `string name = null` is not allowed in the first place? – Emil Laine Feb 11 '16 at 15:49
  • 1
    @zenith I'm interested in languages which can catch as much errors as possible on compile time. So yeah I'm also interested in languages where objects can not be null. – Tomas Feb 11 '16 at 16:16
  • 1
    It seems that a better question title could be "Which languages catch as many errors as possible at compile-time?" in that case. – Emil Laine Feb 11 '16 at 18:11
  • @zenith I guess it's the same.You can't go much further in catching as many errors than to not have run-time exceptions. Compiler won't be able to check if your business logic is correct. – Tomas Feb 11 '16 at 19:41
  • @zenith could you post your link to languages without null as an answer? I would like to accept that :) – Tomas Feb 20 '16 at 10:38

3 Answers3

34

Rust prevents a lot of errors at compile-time. Rust doesn't have runtime exceptions (other than panic! which crashes the program), instead it uses return values for error handling.

let name = None; // error: mismatched types
let name: Option<String> = None; // ok

name.to_lowercase(); // error: no method named `to_lowercase` found for type `Option`

// correct:
match name {
    None => { /* handle None */ },
    Some(value) => { value.to_lowercase(); },
}

// or to ignore None and crash if name == None
name.unwrap().to_lowercase();

One of Rust's core concepts that no other language has (afaik) is lifetimes, which prevent dangling references at compile-time. However this is something that garbage-collected and reference-counted languages don't need.


Go doesn't have exceptions. Like Rust, it uses return values to signal errors. But it's not null-safe:

// strings can't be nil:
var name string = nil // error: cannot use nil as type string in assignment

// string pointers can:
var name *string = nil // ok

string.ToLower(*name) // panic: runtime error: invalid memory address

The following languages do have exception handling but might still be helpful to answer the question.


Swift is also safer than average, but it does include runtime exceptions. However, Swift's exceptions are more explicit than those of C++, Java, C#, etc. (e.g. you have to prefix each call to a throwing function with try, and a function declaration must specify whether that function may throw).

let name = nil // error: type of expression is ambiguous without more context
let name: String? = nil // ok

name.lowercaseString // error: value of optional type 'String?' not unwrapped; did you mean to use '!' or '?'?
name!.lowercaseString // will crash at runtime with: fatal error: unexpectedly found nil while unwrapping an Optional value
name?.lowercaseString // calls method only if name != nil

Kotlin is a safer JVM-language which also compiles to JavaScript. Kotlin also has exceptions.

val name = null
name.toLowerCase() // compile-error

if (name != null)
    name.toLowerCase() // ok

name?.toLowerCase() // call only if non-null

Ada is a language designed for safety-critical purposes. According to http://www.adaic.org/advantages/features-benefits/, its "many built-in checks allow the compiler or linker to detect errors that in a C-based language would only be caught during run-time".

Nato Boram
  • 4,083
  • 6
  • 28
  • 58
Emil Laine
  • 41,598
  • 9
  • 101
  • 157
2

Elm, as the OP noted, is an excellent language without runtime exceptions. Another language that seems to be equally committed to safety is Pony.

code monkey
  • 139
  • 1
  • 3
0

Here's a list of languages that by default don't allow variables to be null.

This makes them a lot safer, preventing all NullPointerExceptions at compile-time, and clearer because you know where you're allowed to pass null and where not, simply by looking at the code.

Most of these languages also provide many other compile-time safety features.

Community
  • 1
  • 1
Emil Laine
  • 41,598
  • 9
  • 101
  • 157