English 中文(简体)
Scala是否记录更新了可以变换的数据结构的斜体?
原标题:Does Scala have record update syntax for making modified clones of immutable data structures?

In Mercury I can use:

A = B^some_field := SomeValue

须对A至B文本的约束,但<编码> > > >>> 外地 是<代码>SomeValue,而不是在B中。 我认为,Haskell等同:

a = b { some_field = some_value }

Does Scala have something like this for "modifying" immutable values. The alternative seems to be to have a constructor that directly sets every field in the instance, which isn t always ideal (if there are invarients the constructor should be maintaining). Plus it would be really clunky and much more fragile if I had to explicitly pass every other value in the instance I want to have a modified copy of.

我无法找到这方面的任何东西,方法是ling,或者在语言参考手册或“Scala by Example”的简短调查中找到(我读过了开端到定点,但却吸收了全部内容,因此,情况可能确实如此)。

我可以看到,这一特点可能会与 Java式出入保护以及二等杂类(......)发生一些令人厌恶的互动。

最佳回答

You can use case classes for this, but you don t have to. Case classes are nothing magical - the modifier case just saves you a lot of typing. The copy method is realized by the use of named and default parameters. The names are the same as the fields and the defaults are the current values of the fields. Here s an example:

class ClassWithCopy(val field1:String, val field2:Int) {
    def copy(field1:String = this.field1, field2:Int = this.field2) = {
        new ClassWithCopy(field1,field2);
    }
}

你们可以像在案件类别上复制方法一样使用这一方法。 过时和缺省参数是一个非常有用的特点,而不仅仅是复制方法。

问题回答

如果你将你的类别定义为<条码>类别,就会产生一种方便的<条码> 范围/代码>方法,请你具体说明某些领域的新价值。

scala> case class Sample(str: String, int: Int)
defined class Sample

scala> val s = Sample("text", 42)
s: Sample = Sample(text,42)

scala> val s2 = s.copy(str = "newText")
s2: Sample = Sample(newText,42)

它甚至与多形态案件班合作:

scala> case class Sample[T](t: T, int: Int)
defined class Sample

scala> val s = Sample("text", 42)
s: Sample[java.lang.String] = Sample(text,42)

scala> val s2 = s.copy(t = List(1,2,3), 42)
s2: Sample[List[Int]] = Sample(List(1, 2, 3),42)

Note that s2 has a different type than s.

If the object you re planning on modifying is a case class then you can use the autogenerated copy method:

scala> val user = User(2, "Sen")
user: User = User(2,Sen)

scala> val corrected = user.copy(name = "Sean")
corrected: User = User(2,Sean)




相关问题
How to flatten a List of different types in Scala?

I have 4 elements:List[List[Object]] (Objects are different in each element) that I want to zip so that I can have a List[List[obj1],List[obj2],List[obj3],List[obj4]] I tried to zip them and I ...

To use or not to use Scala for new Java projects? [closed]

I m impressed with Twitter and investigating to use Scala for a new large scale web project with Hibernate and Wicket. What do you think about Scala, and should I use it instead of Java? EDIT: And, ...

Why does Scala create a ~/tmp directory when I run a script?

When I execute a Scala script from the command line, a directory named "tmp" is created in my home directory. It is always empty, so I simply deleted it without any apparent problem. Of course, when I ...

Include jar file in Scala interpreter

Is it possible to include a jar file run running the Scala interpreter? My code is working when I compile from scalac: scalac script.scala -classpath *.jar But I would like to be able to include a ...

Scala and tail recursion

There are various answers on Stack Overflow which explain the conditions under which tail recursion is possible in Scala. I understand the limitations and how and where I can take advantage of tail ...

热门标签