English 中文(简体)
Scala - infix v. dot notation
原标题:Scala - infix vs dot notation

是否有最佳实践? 页: 1 Scala book by Odersky, et al. 似乎在固定格式中用于收集信息预报系统的许多功能,而专用于方案设计人的职能。

最佳回答

我个人对此没有任何硬而快捷的规则,但我倾向于不仅使用象征性的方法名称,而且用字母数字表示。

固定标记使后来修改法典变得麻烦。 以下是一些例子。

mag:

xs filter { f } map { g }

Suppose at some latter point in time you need to add a toList at end. You put it so:

xs filter { f } map { g } toList

这可能会造成半殖民推论问题。 为避免这些问题,你要么结束半殖民地,要么把一条新路线。 我认为,这两种选择都是很明智的。 为了避免所有这种非专利,我倾向于使用<条码>xs.filter(f).map(g)。 它总是更容易与这一辛迪加混为一谈。

另一个例子: Saye 我的守则如下:

if(foo contains bar) { ..

Say, I need to negate the condition. If I modify it as follows:

if(!foo contains bar) { ..

Bummer。 这分为<代码>(!foo).contains(bar)。 不是我们想要的。

或认为你需要增加新的条件,并作修改:

if(foo contains bar && cond) { ..

另一轮bu。 这分为<代码>foo.contains(bar.&&(cond)>。 不是我们再次想要的。

当然,你可以在周围增加一只母体,但与 do相比,这很难读/编辑。

Now, all of what I said above applies to symbolic method names too. However symbolic methods look unnatural when used with dot syntax, and so I prefer the infix syntax for them.


以上准则的一个例外:内部数据交换系统。 他们通常小心谨慎,以便在文件/文件中规定的方式(通常在固定标记中使用)书写时不引起分歧。

问题回答

It s a matter of personal preference. Your decision to use one style or the other should be based on what makes your code the most readable.

但是,我们注意到,离开狗和母体的能力仅限于某些合成建筑,因此,有时你不得不重新使用这些建筑。

http://docs.scala-lang.org/上型/method-inue.html“rel=“nofollow noreferer”>在官方的Schala站点文件中有“,其中描述了在固定标记上对狗的正确使用。

Suffix Notation:

names.toList
// is the same as
names toList // Unsafe, don t use!

第1级:

// right!
names foreach (n => println(n))
names mkString ","
optStr getOrElse "<empty>"
// wrong!
javaList add item

高等职务:

// wrong!
names.map (_.toUpperCase).filter (_.length > 5)
// right!
names map (_.toUpperCase) filter (_.length > 5)

符号方法/工具:

// right!
"daniel" + " " + "spiewak"
// wrong!
"daniel"+" "+"spiewak"

我发现,使用<代码>>>的固定名称<>。 当我创建有<cats图书馆的漫画家时,我就做了出色的工作。 e.g:

(fetchIt(1) |@| fetchIt(2) |@| fetchIt(3)).map(MyCaseClass)

你们能够摆脱周围的亲子关系:

fetchIt(1) |@| fetchIt(2) |@| fetchIf(3) map MyCaseClass

在我看来,第二种变体为nic。 A matter of taste I suppose. 我只想增加两点价值。

以上是<条码>/代码>在><>>>中高于<条码>。 阅读Schala lang的这一部分,以便更详细地了解:

If there are several infix operations in an expression, then operators with higher precedence bind more closely than operators with lower precedence.

http://scala-lang.org/files/archive/spec/2.12/06-expressions.html#infix-operations





相关问题
Python: Binding method

In following example I am trying to bind a method object via types.MethodType(...). It does not seem to work. Any suggestions? Thanks in advance. import types class Base: def payload(self, *args)...

Invoking an instance method without invoking constructor

Let s say I have the following class which I am not allowed to change: public class C { public C() { CreateSideEffects(); } public void M() { DoSomethingUseful(); } } and I have to call M ...

Behaviour of final static method

I have been playing around with modifiers with static method and came across a weird behaviour. As we know, static methods cannot be overridden, as they are associated with class rather than instance....

Setting the Page-Title in .Net using C# from a Class

Question, How do i set the title of a page from a class. Is it even possible? I can and have set the page title from a page itself and a usercontrol. Can I, How Do I do this via a class using C# ....

how to drag in the animation in iphone application?

Iphone application is using the static co-ordinates to reach at some points in the application,which is based on the button event. But what i want is when i drag the item than it should reach at some ...

热门标签