Loading
Raghav Shankar
This is a live streamed presentation. You will automatically follow the presenter and see the slide they're currently on.
enum Event { Print(String), File(PathBuf), } let a = Event::Print("aaa".to_owned()); match a { Event::File(path) => {} Event::Print(content) => {} };
sealed class Event permits File, Print {} final class File extends Event {} final class Print extends Event {}
interface Celestial { ... }
final class Planet implements Celestial { ... }
final class Star implements Celestial { ... }
final class Comet implements Celestial { ... }
interface Celestial { ... }
final class Planet implements Celestial { ... }
final class Star implements Celestial { ... }
final class Comet implements Celestial { ... }
// !!!
final class SpaceRoadster implements Celestial { ... }
sealed interface Celestial { ... }
final class Planet implements Celestial { ... }
final class Comet implements Celestial { ... }
sealed class Star implements Celestial { ... }
final class Dwarf implements Star { ... }
final class RedGiant implements Star { ... }
// In user code
// This will be an error!
final class SpaceRoadster implements Celestial {
...
}
record Book ( int id, String title ) {}
@jdk.internal.ValueBased
public final class Optional<T> {
/**
* Common instance for {@code empty()}.
*/
private static final Optional<?> EMPTY =
new Optional<>(null);
/**
* If non-null, the value; if null,
* indicates no value is present
*/
private final T value;
...
Optional<Integer> optInt = Optional.of(3);
if (optInt.isPresent()) {
// This would throw an exception
// if the optional were empty.
int value = optInt.get();
}
Pattern matching is nice (Java 19+)
I like it (sue me)
Space savings !1!!!
1. We are using Java 19 preview features (record patterns in switch cases)
2. Java requires exhaustive matching for sealed classes.
3. Generic types absolutely need to be mentioned at all times.
f, err := os.Open("filename.ext")
if err != nil {
log.Fatal(err)
}
enum Result<T, E> { Ok(T), Err(E) }
use std::fs::File;
fn main() {
// main.jpg doesn't exist
let f = File::open("main.jpg");
match f {
Ok(f)=> {
println!("file found {:?}",f);
},
Err(e)=> {
//handled error
}
}
}
if (!someCondition) throw InvalidArgumentException(...); if (!someOtherCondition) throw InvalidStateException(...);