Skip to content

Instantly share code, notes, and snippets.

@arturaz
Created September 22, 2013 09:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arturaz/6658516 to your computer and use it in GitHub Desktop.
Save arturaz/6658516 to your computer and use it in GitHub Desktop.
Defining same ADT in C# and Scala
public abstract class Kind {
public class Global : Kind {}
public class World : Kind {
public readonly int world;
public World(int world) {
this.world = world;
}
}
public class Level : Kind {
public readonly int world;
public readonly int level;
public readonly bool bonus;
public Level(int world, int level, bool bonus) {
this.world = world;
this.level = level;
this.bonus = bonus;
}
}
}
sealed trait Kind
object Kind {
case object Global extends Kind
case class World(world: Int) extends Kind
case class Level(world: Int, level: Int, bonus: Boolean) extends Kind
}
@golergka
Copy link

I don't know Scala, so I might not understand well enough what exactly are you trying to achieve. However%

  • Why do these classes have to be nested? I've almost never seen a child class as a nested class of the parent in C#.
  • If all your classes are immutable, wouldn't you be better off using structs instead?

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