Code Monkey home page Code Monkey logo

2122-s2's People

Contributors

bnjmnt4n avatar boydanderson avatar chrisgzf avatar ktaekwon000 avatar weitsang avatar

Stargazers

 avatar  avatar  avatar

2122-s2's Issues

Type Inference Mechanism

Putting this to remember to change this for next sem.

## Type Inferencing

A more detailed type inference mechanism is to first list down all the type constraints of the form S <: T, then find the most specific solution to the unknown type variable without making it more specific than what is known in the type constraints (this prevents the problem of inferring the subclass).

Unfortunately, it seems to not be enough as shown in CS2030 midterm 2020-2021 where

import java.util.List;

interface Trainable {}
class Animal {}
class Mammal extends Animal {}
class Dog extends Mammal implements Trainable {
  public void f() { }
}
class ShihTzu extends Dog {
  public void g() { }
}

class Doggy {
  public static void main(String[] args) {
    List<Dog> lst = List.of((Dog) new ShihTzu());
    // Doggy.foo(lst).g(); /* fails, U is not ShihTzu */
    // Doggy.foo(lst).f(); /* fails, U is not Dog */
  }
  public static <U, T extends U> U foo(List<? super T> lst) {
    return null;
  }
}

The type constraints are:

T extends U
  ==> T <: U
List<Dog> <: List<? super T>
  ==> ? := Dog (since List<Dog> <: List<Dog> due to generic being invariant so ? has to be substituted with Dog)
  ==> T <: ? (because of ? super T)
  ==> T <: Dog (since ? := Dog)

So the inferred constraints are simply:

T <: U
T <: Dog

So it seems reasonable to infer that both T and U to be Dog based on the principle of most specific. Unfortunately as the code fragment shows, T is indeed Dog but U is actually Object. It seems Java infers each unknown type variables independently and replace the other unknown type variable with the upperbound after type erasure and remove any type constraints not related to the current type being inferred (in this case, during type inference, we replace both T and U with Object).

Type constraints for inference of T that infers Dog:

T <: Object // from T <: U with U replaced with Object
T <: Dog

Type constraints for inference of U that infers Object:

Object <: U // from T <: U with T replaced with Object

Guide mentions Java 11 as most recent LTS

The guide on Environments was recently updated (b100152) regarding its mention of Java 16's release. It currently mentions the use of Java 11 with it being the most recent version with long-term support, although Java 17 seems to have come out a bit ago with LTS.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.