F# - 我应该在有或没有#light的情况下学习吗?

时间:2022-06-01 22:06:00

I'm in the process of learning F# and am enjoying it so far. Almost all of the examples online use the lightweight syntax (#light); however, also give a comment about it being on for said example in most cases.

我正在学习F#,到目前为止我很享受。几乎所有在线示例都使用轻量级语法(#light);但是,在大多数情况下,也会就上述例子发表评论。

Is it better to learn F# using #light enabled or disabled? I'm planning on eventually learning it w/o it turned on but am curious on if it would be better to learn it at the beginning or work on applying it after I know the core language more.

使用#light启用还是禁用来学习F#是否更好?我计划在没有开启的情况下最终学习它,但我很好奇是否在开始学习它或者在我更熟悉核心语言之后应用它。

4 个解决方案

#1


I'd definitely prefer learning F# with the #light syntax. The non-light version is sometimes useful for understanding some tricks about the F# syntax, but the #light syntax gives you much pleasant experience.

我绝对更喜欢用#light语法学习F#。非光版本有时对于理解有关F#语法的一些技巧很有用,但#light语法为您提供了非常愉快的体验。

For example - using #light

例如 - 使用#light

let add a b c = 
  let ab = a + b
  printfn "%d" ab
  c - ab

Using non-light you can write the same thing like this:

使用非光可以写出像这样的东西:

let add a b c = 
  let ab = a + b in // 'in' keyword specifies where the binding (value 'ab') is valid
  printfn "%d" ab;  // ';' is operator for sequencing expressions
  c - ab;;          // ';;' is end of a function declaration

This for example shows that you cannot write something like:

例如,这表明你不能写如下:

let doNothing a b = 
  let sum = a + b in 

There is an 'in' keyword at the end but the function doesn't have any body (because there is no expression following 'in'). In this case non-light syntax is sometimes interesting to understand what's going on... But as you can see, the #light code is a lot simpler.

最后有一个'in'关键字,但该函数没有任何正文(因为'in'后面没有表达式)。在这种情况下,非轻量级语法有时很有用,可以理解正在发生的事情......但正如您所看到的,#light代码更简单。

#2


The "#light" will probably become the default in a future release of the language, so I would learn it that way. I think it's rare for anyone to use the heavier syntax except for OCaml-compatibility (either when cross-compiling, or because the human sitting at the keyboard knows OCaml and is making a smoother transition to F#).

“#light”可能会成为该语言未来版本的默认设置,因此我会以这种方式学习它。我认为除了OCaml兼容性之外,任何人都很少使用较重的语法(无论是在交叉编译时,还是因为坐在键盘上的人都知道OCaml并且正在向F#过渡更平滑)。

#3


Because I learned F# from an OCaml book (and I use an OCaml mode for Emacs to edit F# code), I prefer to use the "heavy" syntax. I have worked with #light code, and of course most of the F# examples are written using the light syntax so having some general familiarity is useful. That said, it's quite a bit easier to switch from heavy to light than the other way around, so it's certainly not a bad idea to learn it using the heavy syntax.

因为我从OCaml书中学到了F#(我使用OCaml模式为Emacs编辑F#代码),我更喜欢使用“重”语法。我使用过#light代码,当然大多数F#示例都是使用轻量级语法编写的,因此具有一定的熟悉程度非常有用。也就是说,从繁重切换到轻松比使用其他方式更容易,因此使用繁重的语法学习它当然不是一个坏主意。

I have come across the occasional annoying bug with heavy syntax being treated as a second class citizen (combine was broken for computation expressions a couple releases back), but these are pretty rare. Generally speaking, I don't think the differences are very significant and I need to look close to determine which syntax is being used when looking at code in isolation. YMMV.

我偶然遇到了一个令人讨厌的错误,重度语法被视为二等公民(组合被破坏了计算表达式,一对夫妇发布回来),但这些很少见。一般来说,我认为差异不是很大,我需要仔细查看单独查看代码时使用的语法。因人而异。

#4


If I remember correctly, book "Expert C#" mentions that #light will be the default when F# ships and that non-light syntax is intended for compatibility only.

如果我没记错的话,预订“Expert C#”会提到#light将是F#发布时的默认值,而非轻量语法仅用于兼容性。

#1


I'd definitely prefer learning F# with the #light syntax. The non-light version is sometimes useful for understanding some tricks about the F# syntax, but the #light syntax gives you much pleasant experience.

我绝对更喜欢用#light语法学习F#。非光版本有时对于理解有关F#语法的一些技巧很有用,但#light语法为您提供了非常愉快的体验。

For example - using #light

例如 - 使用#light

let add a b c = 
  let ab = a + b
  printfn "%d" ab
  c - ab

Using non-light you can write the same thing like this:

使用非光可以写出像这样的东西:

let add a b c = 
  let ab = a + b in // 'in' keyword specifies where the binding (value 'ab') is valid
  printfn "%d" ab;  // ';' is operator for sequencing expressions
  c - ab;;          // ';;' is end of a function declaration

This for example shows that you cannot write something like:

例如,这表明你不能写如下:

let doNothing a b = 
  let sum = a + b in 

There is an 'in' keyword at the end but the function doesn't have any body (because there is no expression following 'in'). In this case non-light syntax is sometimes interesting to understand what's going on... But as you can see, the #light code is a lot simpler.

最后有一个'in'关键字,但该函数没有任何正文(因为'in'后面没有表达式)。在这种情况下,非轻量级语法有时很有用,可以理解正在发生的事情......但正如您所看到的,#light代码更简单。

#2


The "#light" will probably become the default in a future release of the language, so I would learn it that way. I think it's rare for anyone to use the heavier syntax except for OCaml-compatibility (either when cross-compiling, or because the human sitting at the keyboard knows OCaml and is making a smoother transition to F#).

“#light”可能会成为该语言未来版本的默认设置,因此我会以这种方式学习它。我认为除了OCaml兼容性之外,任何人都很少使用较重的语法(无论是在交叉编译时,还是因为坐在键盘上的人都知道OCaml并且正在向F#过渡更平滑)。

#3


Because I learned F# from an OCaml book (and I use an OCaml mode for Emacs to edit F# code), I prefer to use the "heavy" syntax. I have worked with #light code, and of course most of the F# examples are written using the light syntax so having some general familiarity is useful. That said, it's quite a bit easier to switch from heavy to light than the other way around, so it's certainly not a bad idea to learn it using the heavy syntax.

因为我从OCaml书中学到了F#(我使用OCaml模式为Emacs编辑F#代码),我更喜欢使用“重”语法。我使用过#light代码,当然大多数F#示例都是使用轻量级语法编写的,因此具有一定的熟悉程度非常有用。也就是说,从繁重切换到轻松比使用其他方式更容易,因此使用繁重的语法学习它当然不是一个坏主意。

I have come across the occasional annoying bug with heavy syntax being treated as a second class citizen (combine was broken for computation expressions a couple releases back), but these are pretty rare. Generally speaking, I don't think the differences are very significant and I need to look close to determine which syntax is being used when looking at code in isolation. YMMV.

我偶然遇到了一个令人讨厌的错误,重度语法被视为二等公民(组合被破坏了计算表达式,一对夫妇发布回来),但这些很少见。一般来说,我认为差异不是很大,我需要仔细查看单独查看代码时使用的语法。因人而异。

#4


If I remember correctly, book "Expert C#" mentions that #light will be the default when F# ships and that non-light syntax is intended for compatibility only.

如果我没记错的话,预订“Expert C#”会提到#light将是F#发布时的默认值,而非轻量语法仅用于兼容性。