Skip to content

Instantly share code, notes, and snippets.

@t0mmyt
Last active November 6, 2017 16:56
Show Gist options
  • Save t0mmyt/ef7d090cd9c1e53e2f8a to your computer and use it in GitHub Desktop.
Save t0mmyt/ef7d090cd9c1e53e2f8a to your computer and use it in GitHub Desktop.
Scala Code Smells
val this_instruction = "sml." + fields(1).capitalize + "Instruction"
val instruction_args = fields.drop(2)
val c_int = classOf[Int]
val c_str = classOf[String]
val instruction = instruction_args.length match {
case 1 =>
val c = Class.forName(this_instruction).getConstructor(c_str, c_str, c_int)
c.newInstance(fields(0), fields(1), fields(2).toInt: Integer).asInstanceOf[Instruction]
case 2 =>
val c = Class.forName(this_instruction).getConstructor(c_str, c_str, c_int, c_int)
c.newInstance(fields(0), fields(1), fields(2).toInt: Integer, fields(3).toInt: Integer).asInstanceOf[Instruction]
case 3 =>
val c = Class.forName(this_instruction).getConstructor(c_str, c_str, c_int, c_int, c_int)
c.newInstance(fields(0), fields(1), fields(2).toInt: Integer, fields(3).toInt: Integer, fields(4).toInt: Integer).asInstanceOf[Instruction]
}
@oleg390
Copy link

oleg390 commented Nov 6, 2017

Very elegant solution :) typesafety? What?! Reflection? WHAT?! Matching instruction args instead of it's length?! Come on! Are you kidding...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment