Why indentation so confusing in Daml?

I am a beginner of Daml and I found indentation so confusing in Daml. Sometimes 2-space indentation work, sometimes 4-sapce indentation can run the code. Is there any unified official way when writing code?

Hello @skyler, and welcome to the forum.

Daml inherits indentation rules from Haskell. Like in other languages that use indentation, such as Python, the number of white spaces is insignificant. A single white space indent works just as well as 2 or 3, or 4 space. What is significant is alignment. For more details see Haskell indentation rules.

Whether you use 2 space or 4 space indentation makes no difference to the compiler, as long as the statements are properly aligned. In other words, as long as the proper alignment is preserved, the choice between 2 space and 4 space indentation is purely cosmetic.

Internally at DA we tend to use as a code style guide the following indentation rules, which we feel improve readability of the code:
Indent all code blocks with 4 spaces. Indent the where keyword with 2 spaces to set it apart from the rest of the code, indent definitions in where clauses with 2 spaces. E.g.

createLookupContext req = do
    createdTime <- getTime
    create LookupContext with counterIds; actorIds; createdTime
    pure ()
    counterIds = [CounterId.HolderIdGeneration]
    actorIds = [Holder.getActorIdForRequest req]
1 Like