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
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
Here's a list of languages that by default don't allow variables to be null.
This makes them a lot safer, preventing all NullPointerException
s 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.
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".
unwrap()
. Similar to swift's !
operator but much easier to search for and avoid. –
Highwrought unwrap()
in Rust would cause a panic, that is program termination, which is different from an exception. It also has the ?
operator which has a similar effect. –
Nisus Elm, as the OP noted, is an excellent language without runtime exceptions. Another language that seems to be equally committed to safety is Pony.
Here's a list of languages that by default don't allow variables to be null.
This makes them a lot safer, preventing all NullPointerException
s 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.
© 2022 - 2024 — McMap. All rights reserved.
string name = null
is not allowed in the first place? – Roby