morganstanley/hobbes

Request for documentation for DEFINE_VARIANT

mortehu opened this issue · 1 comments

DEFINE_VARIANT is briefly described in reflect.H:

A macro 'DEFINE_VARIANT(T, (cn0, T0), ... (cnN, TN))' is included to be equivalent to '|cn0:T0, ..., cnN:TN|'

However, I'm having trouble using types defined using DEFINE_VARIANT without getting "Unsatisfiable predicate" errors. Here's my attempt:

#include <iostream>

#include "hobbes/hobbes.H"

using FooSum = hobbes::variant<int, float>;

DEFINE_VARIANT(FooVariant, (A, int), (B, float));

int main(int argc, char** argv)
{
  hobbes::cc cc;

  // Ok
  cc.compileFn<int(FooSum*)>("foo", "match foo with | |0=a| -> a | _ -> 0");

  // Ok
  cc.define("bar", "|A=1| :: |A:int, B:float|");
  cc.compileFn<int()>("match bar with | |A=a| -> a | _ -> 0");

  // Prints "|A=1|"
  std::cerr << FooVariant::A(1) << std::endl;

  // Prints "stdin:1,1-36: Unsatisfiable predicate: |A:int|::<FooVariant>"
  cc.compileFn<int(FooVariant*)>("foo", "match foo with | |A=a| -> a | _ -> 0");
}

Thank you for reporting this; it appears to be a bug in Hobbes wherein the compileFn isn't prepared to lift these kinds of variant types.