You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Jun 5, 2023. It is now read-only.
Method polymorphism in Scala is resolved at compile-time and is only resolved for variables with explicitly declared types.
My suggestion is to add a case def kind of declaration that searches for all similar implementations in a class/object and builds
a single method that uses match/case entries at runtime to decide a branch.
and we have a collection of Animal that we want to iterate over and do something for each animal.
defpet(a: Animal):Unit= println("Trying to pet an animal")
defpet(b: Bear):Unit= println("The bear bit you, he doesn't like being pet")
defpet(c: Cat):Unit= println("The cat purrs.")
List(newCat, newBear, newAnimal, newPet).foreach(pet(_))
But this code would only print the animal branch 4 times because the list contains Animal. If we wanted a more specific behaviour we would use a match statement for each branch in our 1 pet class. How about the case def?:
casedefpet(a: Animal):Unit= println("Trying to pet an animal")
casedefpet(b: Bear):Unit= println("The bear bit you, he doesn't like being pet")
casedefpet(c: Cat):Unit= println("The cat purrs.")
that would get compiled to
defpet(a: Animal):Unit= a matchcasec: Cat=> println("The cat purrs.")
caseb: Bear=> println("The bear bit you, he doesn't like being pet")
casea: Animal=> println("Trying to pet an animal")
The compiled method's signature takes the broadest union found (in this case Animal is the broadest type and no union is to be made, however if the case def pet(a: Animal) wasn't present at compile time, the method's signature would become Cat | Bear for the sake of match exhaustion.
This would make the code more readable and would bring Scala closer to some other functional programming languages because now you could say:
casedefreverse(string: ""):String=???casedefreverse(string: String):String=???// That would become:defreverse(string: String):String= string matchcase""=>???cases: String=>???
This would also be really cool which is what Scala is.
The text was updated successfully, but these errors were encountered:
Hello!
Method polymorphism in Scala is resolved at compile-time and is only resolved for variables with explicitly declared types.
My suggestion is to add a
case def
kind of declaration that searches for all similar implementations in a class/object and buildsa single method that uses match/case entries at runtime to decide a branch.
Example
Say we have the following hierarchy:
and we have a collection of Animal that we want to iterate over and do something for each animal.
But this code would only print the animal branch 4 times because the list contains Animal. If we wanted a more specific behaviour we would use a match statement for each branch in our 1 pet class. How about the case def?:
that would get compiled to
The compiled method's signature takes the broadest union found (in this case
Animal
is the broadest type and no union is to be made, however if thecase def pet(a: Animal)
wasn't present at compile time, the method's signature would becomeCat | Bear
for the sake of match exhaustion.This would make the code more readable and would bring Scala closer to some other functional programming languages because now you could say:
This would also be really cool which is what Scala is.
The text was updated successfully, but these errors were encountered: