f# - Attributes and let statements in type members -
i using fsunit write unit tests in f# , have noticed odd behaviour around attributes , let statements in type members , wondering if explain it?
if write test this:
[<fact>] member test. ``test works correctly`` () = let x = 1 x + 2 |> should equal 3 i error in visual studio 2012 on first character of 'x + 2' line saying:
unexpected keyword 'let' or 'use' in expression. expected 'in' or other token.
using 'let ... in' gets around error, although if want use more 1 let gets messy quickly:
[<fact>] member test. ``test works correctly`` () = let x = 1 in x + 2 |> should equal 3 i discovered having attribute on separate line gets around error:
[<fact>] member test. ``test works correctly`` () = let x = 1 x + 2 |> should equal 3 can provide insight on why first code snippet causes error other 2 not?
in first snippet, had 2 indentation errors. first issue ''test works correctly'' should indented after member keyword. second 1 body of function should indented after lines of member definitions.
if fix first error:
[<fact>] member test. ``test works correctly`` () = let x = 1 x + 2 |> should equal 3 the compiler issue "possible incorrect indentation" warning, can fix indenting body of function further:
[<fact>] member test. ``test works correctly`` () = let x = 1 x + 2 |> should equal 3 in second snippet, use of in keyword triggers verbose syntax indentation doesn't matter anymore. in last example, member happens have lowest indentation; incidentally have correct indentation.
that said, should avoid break member definitions multiple lines. in of cases, can use let bindings instead of more verbose member bindings.
[<fact>] let ``test works correctly``() = let x = 1 x + 2 |> should equal 3
Comments
Post a Comment