Skip to content

Instantly share code, notes, and snippets.

@heidisu
Created May 14, 2019 20:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heidisu/eec751c2dbcc1e8e2b46944bed838e99 to your computer and use it in GitHub Desktop.
Save heidisu/eec751c2dbcc1e8e2b46944bed838e99 to your computer and use it in GitHub Desktop.
Path dependent types - inner class in java is related to enclosing class, inner class in Scala is related to object it is created from
package animal;
class Animal {
class Sound{
final String sound;
public Sound(String sound){
this.sound = sound;
}
}
}
public class App {
public static void main(String[] args){
var dog = new Animal();
var cat = new Animal();
var dogSound = dog.new Sound("voff");
var catSound = cat.new Sound("mjau");
dogSound = catSound;
System.out.println(dogSound.sound);
}
}
class Animal {
class Sound(sound: String) {
}
}
object Test {
def main(args: Array[String]): Unit = {
val dog = new Animal
val cat = new Animal
var dogSound = new dog.Sound("voff")
val catSound = new cat.Sound("mjau")
// Error:(21, 16) type mismatch;
// found : cat.Sound
// required: dog.Sound
// dogSound = catSound
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment