-
Notifications
You must be signed in to change notification settings - Fork 20
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
Generated Haskell records should have strict fields #97
Comments
Interesting point. In what circumstances do you think the lack of strictness annotations will cause problems? For your case 2, the serialization process will fully evaluate all of the thunks. For case 1, I can see that unevaluated thunks may build up with in memory data structures (eg with this demo project!). In my experience, however, in real world API examples, one generally updating some persistent store and the thunks will be indeed evaluated. Rather than try and make a blanket decision on strictness, I think it would be better to have an an ADL annotation to specify strictness on an individual field or whole declaration. This of course leaves us with the decision as to what the annotation "default" should be. Given one goal of ADL is to generate idiomatic code in each target language, I'd favor keeping the default to non-strict fields, for consistency with regular haskell. |
In my opinion, the idiomatic Haskell way is to always use strict fields for records that are for storing "business data" (i.e. not generic data structures). This can be seen in lots of open source Haskell projects. For example, a random file from XMonad: https://github.com/xmonad/xmonad/blob/40466b2be266e50e941f2fcc53b7526f1cfc71be/src/XMonad/Layout.hs#L51 From the well known What I Wish I Knew when Learning Haskell:
From the Johan Tibell Haskell Style Guide:
Also, from the Kowainik Haskell style guide:
And here is another quote from some blog post:
One last example: The Haskell protocol-buffers package has a similar use-case as ADL, and always generates strict fields. In my experience and from what I've seen in the Haskell community, there is widespread consensus about this type of usage of strict fields. The above links have more in depth explanations. In regards to performance, strict fields can be a huge win, especially when a record has lots of small
Right, I don't think I communicated my thoughts clearly in my initial post. What I meant to say is: If it is certain that your Haskell record will have all of its fields eventually evaluated, then laziness only gives you worse performance for no benefit. In the case of ADL, we are certain that all of the fields of a record will be eventually evaluated:
It should be clear that my opinion is that the default should be strict :] (In fact, I would go so far as to say that I don't think it is necessary to even have the option for lazy fields). But I genuinely am interested in hearing the opinions of other people on this matter. |
I think you've convinced me that fields should be strict by default. But I don't want to preclude ADL from being used to define potentially lazy data structures, so I'd still like to see the annotation to control this. |
The generated data types are intended to be used in two different ways:
In both cases of these cases lazy fields don't make sense, therefore I recommend that strict
!
annotations be added to all fields.Workaround: You can compile the generated code with
-XStrictData
, but I believe the better approach is to just have all the fields be strict by defaultThe text was updated successfully, but these errors were encountered: