nus-cs2030s / 2122-s2 Goto Github PK
View Code? Open in Web Editor NEWhttps://nus-cs2030s.github.io/2122-s2/
https://nus-cs2030s.github.io/2122-s2/
Putting this to remember to change this for next sem.
Line 28 in 7af5e1e
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
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.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.