I have some code:
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
trait LogicUnit
trait Ezib extends LogicUnit
trait Zieb extends LogicUnit
object LogicUnit {
@inline
def combine[A <: LogicUnit, B <: LogicUnit](arg1: A, arg2: B): Future[LogicUnit] = {
if (arg1.isInstanceOf[Ezib] && arg2.isInstanceOf[Ezib]) return Future(new Ezib {})
else if (arg1.isInstanceOf[Zieb] && arg2.isInstanceOf[Zieb]) return Future(new Zieb {})
else if (arg1.isInstanceOf[Ezib] && arg2.isInstanceOf[Zieb]) return Future(new Zieb {})
else return Future(new Ezib {})
}
}
Since this code will be run a lot I am trying to optimize it a lot hence, I am trying to combine the first two lines of the combine function. To do so I figured the code would look something like this:
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
trait LogicUnit
trait Ezib extends LogicUnit
trait Zieb extends LogicUnit
object LogicUnit {
@inline
def combine[A <: LogicUnit, B <: LogicUnit](arg1: A, arg2: B): Future[LogicUnit] = {
if (arg1.isInstanceOf[typeOf(arg2)]) return Future(arg1)
else if (arg1.isInstanceOf[Ezib] && arg2.isInstanceOf[Zieb]) return Future(new Zieb {})
else return Future(new Ezib {})
}
}
Is there any way I can get the code to just check if the two types are the same so I don't have to match for all cases?