The following code snippet is from reasonml, a dialect from ocaml,
type schoolPerson = Teacher | Director | Student(string);
let greeting = person =>
  switch (person) {
  | Teacher => "Hey Professor!"
  | Director => "Hello Director."
  | Student("Richard") => "Still here Ricky?"
  | Student(anyOtherName) => "Hey, " ++ anyOtherName ++ "."
  };
I know Nim has object variant and generic feature, but How can I express type with "payload" as
("Richard")One way might be:
type
  spKind = enum
    spkTeacher,spkDirector,spkStudent
  SchoolPerson = object
    case kind:spKind
    of spkStudent:
      name: string
    else: discard
proc greeting(sp: SchoolPerson):string =
  case sp.kind
  of spkTeacher: "Hey Prof!"
  of spkDirector: "Sup Guv"
  of spkStudent:
    case sp.name
    of "Richard": "Still here Dick?"
    else: "Hi " & sp.name & "."
echo greeting(SchoolPerson(kind:spkStudent,name:"George"
I guess this is more concise:
proc greeting(sp: SchoolPerson):string =
  let (kind, name) = (sp.kind, sp.name)
  case (kind, name):
    of (spkTeacher, _): "Hey Prof!"
    of (spkDirector, _): "Sup Guv"
    of (spkStudent, "Richard"): "Still here Dick?"
    else: "Hi " & sp.name & "."
fusion/matching actually has a special treatment of kind, so you can write:
case sp
of Teacher(): "Hey Prof!"
of Director(): "Sup Guv"
of Student(name: "Richard"): "Still here Dick?"
of Student(name: @name): "Hi " & name & "."