Split an operator (expression) into several lines: how to indent

It was very difficult for me to find a simple indent pointer in F #. Basically, I wonder what rule is for indenting outputs with multiple lines. There is no problem in C # since spaces are not taken into account. Although I can write F # code according to my intuition, and it works, I really want to know what a rule is for breaking one operator into several lines. I write how

printfn "%d" 1 

It works as expected.

And if I write them in one column, something will go wrong.

 > printfn "%A%A" 1 [];; > //nothing is returned... and no error in this case 

I want to confirm the basic rule for this. It is a little annoying when you cannot be sure what you are doing.

Thanks in advance

I just tried another case

 List.iter (printfn "%d") [1..10];; 

And he prints from 1 to 10. Why not

 List.iter ((printfn "%d") [1..10]);; 
+6
source share
2 answers

As Yin points out, the rule is that function arguments must be indented further than the function call. To add more details, your first fragment is interpreted as follows:

 printfn "%A%A"; 1; []; 

Each of them is a valid expression that returns something (function, number, empty list), and then ignores the result and continues. Since they are written in the top-level area, F # Interactive does not give a warning that you are ignoring some values. If they were in a do or let declaration:

 do printfn "%A%A" 1 [] 

The F # compiler issued a warning when sequencing expressions (using ; ) that did not return unit :

stdin (5,3): warning FS0193: This expression is a function value, that is, missing arguments. Its type is 'a →' b → unit.

stdin (6,3): warning FS0020: this expression must be of type 'unit', but of type e 'int'. Use ignore to cancel the result of the expression, or let to bind the result to the name.

stdin (5,3): warning FS0020: this expression must be of type 'unit', but has type e 'list. Use "ignore" to cancel the result of the expression, or "let" to b ind the result to the name.

+6
source

In your second example, you should backtrack:

 > printfn "%A%A" 1 [];; 

Otherwise, the three expressions are three consecutive expressions, not one expression.

You can specify the F # Language Specification for company rules, for example. Chapter 15 in the specification.

+3
source

Source: https://habr.com/ru/post/886571/


All Articles