Nullsafe Operator (?->)

Is null a type or a value — PHP ?-> chain short-circuiting beside C# ?., JS ?., Rust Option and Java Optional.

C C23

C23
#include <stdio.h>

/* C: null-safety is hand-written pointer guards, nothing more */
typedef struct { const char *city; } Address;
typedef struct { Address *address; } User;

const char *city_of(const User *u) {
    if (u == NULL || u->address == NULL) { /* guard every hop */
        return "null";
    }
    return u->address->city;
}

int main(void) {
    Address a = {"Paris"};
    User with = {&a};
    User without = {NULL};
    printf("%s\n", city_of(&with));
    printf("%s\n", city_of(&without));
    return 0;
}
Output
Paris
null

C++ C++26

C++26
#include <cstdio>
#include <optional>
#include <string>

// C++: raw pointers need manual guards; std::optional (C++17) makes absence
// explicit and chains with value_or — closer to Rust's Option
struct Address { std::string city; };
struct User { std::optional<Address> address; };

std::string city_of(const User &u) {
    return u.address ? u.address->city : "null"; // guard on the optional
}

int main() {
    User with{Address{"Paris"}};
    User without{std::nullopt};
    std::printf("%s\n", city_of(with).c_str());
    std::printf("%s\n", city_of(without).c_str());
    return 0;
}
Output
Paris
null

C# v11.0.100

v11.0.100
var with = new User(new Address("Paris"));
var without = new User(null);

Console.WriteLine(with.Address?.City ?? "null"); // ?. and ?? : the same duo as JS
Console.WriteLine(without.Address?.City ?? "null");

record Address(string City);
record User(Address? Address);
Output
Paris
null

Go v1.27.4

v1.27.4
package main

import "fmt"

// Go has no nullsafe operator: nil checks are written out by hand
type Address struct{ City string }
type User struct{ Address *Address } // a pointer field that can be nil

func cityOf(u *User) string {
	if u == nil || u.Address == nil { // the guard chain, manually
		return "null"
	}
	return u.Address.City
}

func main() {
	with := &User{Address: &Address{City: "Paris"}}
	without := &User{}
	fmt.Println(cityOf(with))
	fmt.Println(cityOf(without))
}
Output
Paris
null

Java v27.0.1

v27.0.1
import java.util.Optional;

public class Main {
    record Address(String city) {}
    record User(Optional<Address> address) {} // Optional: absence as a wrapper type

    static String cityOf(User u) {
        return u.address().map(Address::city).orElse("null");
    }

    public static void main(String[] args) {
        System.out.println(cityOf(new User(Optional.of(new Address("Paris")))));
        System.out.println(cityOf(new User(Optional.empty())));
    }
}
Output
Paris
null

JavaScript v26.13.1

v26.13.1
class Address {
  constructor(city) { this.city = city; }
}
class User {
  constructor(address) { this.address = address; }
}

const withA = new User(new Address("Paris"));
const withoutA = new User(null);

console.log(withA?.address?.city ?? "null");   // ?. and ?? : the same duo as C#
console.log(withoutA?.address?.city ?? "null");
Output
Paris
null

PHP v8.6.0

v8.6.0
class Address {
    public function __construct(public string $city) {}
}
class User {
    public function __construct(public ?Address $address) {} // nullable type: null is a VALUE here
}

$with = new User(new Address("Paris"));
$without = new User(null);

echo $with?->address?->city, "\n";     // every link non-null: chain evaluates normally
echo $without?->address?->city ?? "null", "\n"; // a null link short-circuits the whole chain to null
Output
Paris
null

Python v3.15.2

v3.15.2
# Python has no nullsafe operator: absence (None) needs explicit guards
class Address:
    def __init__(self, city):
        self.city = city

class User:
    def __init__(self, address):
        self.address = address

with_a = User(Address("Paris"))
without_a = User(None)

def city_of(user):
    # the idiomatic chain: getattr with a default, applied step by step
    addr = getattr(user, "address", None)
    return getattr(addr, "city", None) or "null"

print(city_of(with_a))
print(city_of(without_a))
Output
Paris
null

Rust v1.99.1

v1.99.1
// Rust has no null: Option<T> makes absence a TYPE; combinators and ?
// replace ?. — the compiler forces every possibly-absent step to be handled
struct Address {
    city: String,
}
struct User {
    address: Option<Address>,
}

fn city_of(u: &User) -> &str {
    u.address
        .as_ref()
        .map(|a| a.city.as_str())
        .unwrap_or("null") // the explicit "else" of the chain
}

fn main() {
    let with = User { address: Some(Address { city: "Paris".to_string() }) };
    let without = User { address: None };
    println!("{}", city_of(&with));
    println!("{}", city_of(&without));
}
Output
Paris
null

TypeScript v26.13.1

v26.13.1
// TS: ?. and ?? like JS; with strictNullChecks null enters the type system,
// so `address: Address | null` must be handled
class Address {
  city: string;
  constructor(city: string) { this.city = city; }
}
class User {
  address: Address | null; // null is part of the TYPE here
  constructor(address: Address | null) { this.address = address; }
}

const withA = new User(new Address("Paris"));
const withoutA = new User(null);
console.log(withA.address?.city ?? "null");
console.log(withoutA.address?.city ?? "null");
Output
Paris
null

Language differences

C: purely hand-written null guards (example): one if per arrow; the language has no null-safety machinery at all, and dereferencing null is undefined behavior rather than an exception.

C++: the raw-pointer era guards by hand like C; std::optional (C++17) makes absence explicit (example) and chains via value_or/and_then, approaching Rust's Option.

C#: the ?. and ?? duo (example), isomorphic to JS; ?. is also the common idiom for firing delegates and events.

Go: no nullsafe operator — guards are hand-written (example); nil is the zero value of pointers/interfaces/slices, and if err != nil / if x != nil are Go's daily bread.

Java: no ?. — Optional<T> (example) wraps absence into a type; chains via map/orElse; by convention an Optional reference itself should never be null.

JavaScript: the ?. and ?? duo (example): ?. short-circuits to undefined, ?? falls back only for null/undefined; isomorphic to PHP.

PHP: the 8.0+ ?-> (example): any null link short-circuits the whole chain to null, equivalent to wrapping each hop in is_null; officially positioned for when null is a valid expected value — errors should be exceptions; read chains only, there is no nullsafe write.

Python: no nullsafe operator — the getattr(obj, name, default) chain (example) or explicit ifs; test absence with is None.

Rust: no null — Option<T> makes absence a type (example): map/unwrap_or chains, ? for early return in Option-returning functions; the compiler forces every possibly-absent step to be handled.

TypeScript: the same ?./?? as JS (example); under strictNullChecks null|undefined joins the type system, so a possibly-empty access point without ?. or narrowing is a compile error.