dropbox / djinni

A tool for generating cross-language type declarations and interface bindings.
Apache License 2.0
2.88k stars 488 forks source link

Improve error messages and generate operator<< for records #417

Open danyowdee opened 5 years ago

danyowdee commented 5 years ago

This PR improves the error messages when a record cannot conform to a typeclass, because it has fields of types that don’t. In addition, it generates the C++ operator<< for enums, and introduces the show typeclass to do the same for records deriving (show).

Assuming the following Djinni file:

enum CurrencyCode {
    USD;
    EUR;
};

Amount = record {
    units: i64;
    currency: CurrencyCode;
} deriving (show)

Product = record {
    name: string;
    identifier: string;
    price: Amount;
} deriving (show)

ShoppingCart = record {
    items: list<Product>;
} deriving (show)

This PR allows you to write the following C++ code:

Product p{"Little Red Corvette", "v0KpfrJE4zw", Amount{1999, CurrencyCode::USD}};
Product q{"Breakfast Can Wait", "CzWX1gv6u2s", Amount{2013, CurrencyCode::EUR}};
ShoppingCart c{{p, q}};
std::cout << c << std::endl;
/* prints
"""ShoppingCart{ items:[
Product{ name:Little Red Corvette, identifier:v0KpfrJE4zw, price:Amount{ units:1999, currency:CurrencyCode::USD } },
Product{ name:Breakfast Can Wait, identifier:CzWX1gv6u2s, price:Amount{ units:2013, currency:CurrencyCode::EUR } },
] }"""
 */

If Amount would be missing the declaration of conformance to show, the error message from running Djinni would clearly say so:

Record 'Amount' not deriving required operation(s): 'show'

Supported Field Types

Limitations/Known Issues

xianwen commented 5 years ago

Hi, @danyowdee: I noticed you haven't signed the CLA yet, could you please sign it here: https://opensource.dropbox.com/cla/ Thanks a lot!

danyowdee commented 5 years ago

Done.