Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for multiple return types #37

Closed
mttrbrts opened this issue Mar 6, 2018 · 5 comments
Closed

Support for multiple return types #37

mttrbrts opened this issue Mar 6, 2018 · 5 comments

Comments

@mttrbrts
Copy link
Member

mttrbrts commented Mar 6, 2018

Context

Currently, clause logic can only specify a single return type.
e.g.
https://github.com/accordproject/cicero-template-library/blob/master/fragile-goods/lib/logic.js#L25

 * @param {io.clause.demo.fragileGoods.PayOut} context.response - the response

Expected Behavior

As a template author, I want my logic to be able to specify multiple CTO types without building a wrapper CTO type. This could be in the form of an array, or integer indexed object.

 * @param {Object[]} context.response - the response

or

 * @param {io.clause.demo.fragileGoods.PayOut} context.response.0 - the response
 * @param {io.clause.demo.fragileGoods.Message} context.response.1 - the response

Possible Fix

Need to look at this code:
https://github.com/accordproject/cicero/blob/master/packages/cicero-engine/lib/engine.js#L167

                let type${n} = '${ele.getParameterTypes()[2]}';

Existing issues

None.

@jeromesimeon
Copy link
Member

jeromesimeon commented Mar 6, 2018

This is something that should be discussed. With Object[] you lose the type information. In JavaScript you might get away with it because it's mostly dynamic, but if you want a type checker (as I think we should get in Jura), you need to be able to describe the signature for the clause.

A traditional way to handle multiple return values is with a tuple. See e.g., http://www.codingexplorer.com/tuples-in-swift-create-read-and-return/

Sadly there is no tuple type (that I know of?) in CTO, so this means an extension to the Jura type system.

In Jura this could look as follows:

clause getPayout(request DeliveryUpdate) : (Payout, Message) { // code goes here }

It could be implemented as an 'on the fly record' so as a kind of syntactic sugar for a record (or JS side a JSON object) with the following structure:

{ '0' : PayoutValue, '1' : MessageValue }

or

{ 'payout' : PayoutValue, 'message' : MessageValue }

JavaScript side you could access it as:

const payout = response['0'];
const message = response['1'];

Do you think that might work?

@jeromesimeon
Copy link
Member

jeromesimeon commented Mar 6, 2018

A couple of following up notes:

  • There is additional ramifications for Jura, e.g., if you allow a clause or a function to return a tuple, you want a way to deconstruct that tuple. e.g., let (payout, message) := getPayout(deliverytx); ...
  • It's possible that we could serialize/deserialize Jura tuples as JSON arrays to be closer to your original idea, but this means type-based serialization/deserialization which is makes things more complicated (concretely, if you get a JSON array as input you only know if it's a tuple or an actual array by looking t the type).
  • Multiple input parameters?

@jeromesimeon
Copy link
Member

@mttrbrts What's the reason for closing this?

@mttrbrts
Copy link
Member Author

Reading this again, this should be an Ergo issue, so deferring to your issue accordproject/ergo#20

@jeromesimeon
Copy link
Member

👍 I thought it was! Sounds good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants