daml icon indicating copy to clipboard operation
daml copied to clipboard

Understanding the limitation of using "large" tuples

Open stefanobaghino-da opened this issue 3 years ago • 1 comments

What is the problem you want to solve?

Using "large" tuples (where "large" here means "having more than five items") incurs in significant usability penalties. In particular the fact that there are no Show and HasField instances available for those means that they cannot be printed and items cannot be accessed with the dot record instance. The following snippet will have two errors:

module Test where

import Daml.Script

example : Script Int
example = script do
  let tuple = (1,1,1,1,1,1)
  debug tuple   -- error: no `Show`
  pure tuple._1 -- error: no `HasField`

The errors are quite difficult to read for a newcomer and the reason behind this limitation (the compilation time and output size everyone would incur in due to more instances being available) can not be clear even to more advanced users.

What is the solution you would propose?

Add a warning when a Tuple6 (or above) is defined that clearly reports the limitation and possibly suggests a possible way forward; something along the lines of:

module Test where

import Daml.Script

example : Script Int
example = script do
  let tuple = (1,1,1,1,1,1) -- WARNING: tuples with six or more elements are subject to limitations,
                            -- such as the inability of being printed and of individual items being
                            -- accessible with the dot record syntax. Consider using a named record,
                            -- which will likely improve readability.

  pure tuple._1             -- error: no `HasField`

Describe alternatives you've considered

Adding Show and HasField instances for Tuple6 and above alongside the warning. This, however, would have the drawback of causing compilation time and output size cost for all users.

stefanobaghino-da avatar Aug 08 '22 08:08 stefanobaghino-da

Possibly helpful context: https://github.com/digital-asset/daml/pull/8475.

garyverhaegen-da avatar Aug 08 '22 09:08 garyverhaegen-da

Fixed in #15018.

basvangijzel-DA avatar Nov 10 '22 14:11 basvangijzel-DA