News for hackers. Stuff that matters.
Re:Using TypeScript with React by ng12 on Thursday August 22, 2019 @12:49PM (#20770007)
This is the equivalent of casting in Java. Is Java's type system unsound?
Re:Using TypeScript with React by marcosdumay on Thursday August 22, 2019 @01:24PM (#20770389)
There is no casting in a sound type system.

No language actually gets there, but there are some where developers mostly don't even remember there is a cast operation.

Re:Using TypeScript with React by DougBTX on Thursday August 22, 2019 @03:25PM (#20771519)
Re:Using TypeScript with React by lhorie on Thursday August 22, 2019 @01:43PM (#20770583)
You can't cast from a HashMap to an Animal class in Java, so in that sense Java is sounder. But you can still do `Animal a = null; a.walk();`, so in that sense, Java isn't sound.
Re:Using TypeScript with React by skn0tt on Thursday August 22, 2019 @02:27PM (#20770995)
Correct me if I'm mistaken: I think you can indeed cast from HashMap to Animal and it will happily compile:

```java

import java.util.HashMap;

class Main {

  class Animal {
    String sound = "roar";
  }

  public static void main(String[] args) {
    HashMap<String, String> map = new HashMap<>();
    Animal animal = (Animal) (Object) map;
    System.out.println(animal.sound);
  }
  
} ```

At runtime, this will crash with the following Exception: `Exception in thread "main" java.lang.ClassCastException: class java.util.HashMap cannot be cast to class Main$Animal`, but it will satisfy the type system.

Re:Using TypeScript with React by lhorie on Thursday August 22, 2019 @03:40PM (#20771645)
Oh right, I totally forgot about casting to Object! So much for "sort of sound" haha