Skip to content

Instantly share code, notes, and snippets.

@tkellogg
Created November 6, 2013 16:09
Show Gist options
  • Save tkellogg/7338884 to your computer and use it in GitHub Desktop.
Save tkellogg/7338884 to your computer and use it in GitHub Desktop.
Is this a bad pattern? I've begun using it a lot and I haven't seen any negative effects, but I've never heard anyone advocate for it either. Wondering if my intuition is mislead. As far as I know, these patterns don't exist in F#, ML or Haskell, so I'm wondering if they're a brilliant addition of Scala or an antipattern.
sealed trait ParseTree
sealed trait ValueNode extends ParseTree
sealed trait PrimitiveValueNode extends ValueNode
case class OrNode(left: ParseTree, right: ParseTree) extends ParseTree
case class AndNode(left: ParseTree, right: ParseTree) extends ParseTree
case class BoolNode(value: Boolean) extends PrimitiveValueNode
case class IntNode(value: Int) extends PrimitiveValueNode
case class StringNode(value: String) extends PrimitiveValueNode
// Use refined trait ValueNode to better describe what value can *actually* hold
case class PropertyNode(key: StringNode, value: ValueNode) extends ParseTree
case class ObjectNode(properties: List[PropertyNode]) extends ValueNode
@t0yv0
Copy link

t0yv0 commented Nov 6, 2013

Not sure I can read Scala well, does this F# do the same job, roughly? Sounds like Scala just lets you elide some of the tags (like StringNode).

type StringNode =
    | StringNode of string

type PrimitiveValueNode =
    | BoolNodePrim of bool
    | IntNodePrim of int
    | StringNodePrim of StringNode

type PropertyNode =
    | ProperyNode of StringNode * ValueNode

and ValueNode =
    | ObjectNode of list<PropertyNode>
    | PrimValueNode of PrimitiveValueNode

type ParseTree =
    | OrNode of ParseTree * ParseTree
    | AndNode of ParseTree * ParseTree
    | PropertyNodeTree of PropertyNode

@tkellogg
Copy link
Author

tkellogg commented Nov 6, 2013

No, everything is a ParseTree, everything but AndNode and OrNode are ValueNode. So it's subsets of types

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