Created
May 14, 2019 20:52
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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