Scala案例类背后的内容是什么?(What's behind Scala case classes?)
我对Scala很新,但我已经使用过case类。 我理解常规类和案例类之间的主要区别如此处总结。
我甚至不想去掉案例类,但我想知道转换所需的代码是什么,例如:
class Tweet(val user: String, val text: String) { override def toString: String = "User: " + user + "\n" + "Text: " + text + "]" }
进入完整案例“兼容”类。 我的意思是,我想编写案例类的相同“行为”但不使用
case
关键字。 这是可能的还是编译器做了一些我无法通过代码(不包括优化)的东西?再一次说清楚我要问的是什么,当我需要一个案例类时,我将总是使用case关键字,但是在生命中,我想知道Scala编译器(在一般意义上)对我有什么作用,用代码术语。
编辑:另外一个疑问:编译器是否会以某种方式标记我的手工编码类与标准案例类的不同,以便我可以在执行中观察到不同的行为?
I'm quite new to Scala but I have already used case classes. I understood what are main differences between a regular class and a case class as summarized here.
I do not even think to get rid of case classes, but I would like to know what's the code needed to transform for example, this:
class Tweet(val user: String, val text: String) { override def toString: String = "User: " + user + "\n" + "Text: " + text + "]" }
into a full case "compatible" class. I mean, I would like to code the same "behavior" of a case class but without using
case
keyword. Is this possible or does the compiler do something that I cannot get through code (excluding optimizations)?Once again to make clear what I'm asking, I will always use case keyword when I need a case class, but once in life time, I would like to know what the Scala compiler (in generic sense) does for me, expressed in code terms.
edit: An additional doubt: will the compiler mark somehow differently my hand coded class from an standard case class so that I can observe a different behavior in execution?
原文:https://stackoverflow.com/questions/19756894
最满意答案
这里的解决方案并不是让大海沸腾,并且使Ruby中的每一个字符串都将所有内容强制转换为大写字母,而是将大小写转换为系统需要的东西, 以及将其提供给该系统时的情况。
以这种戏剧性的方式更改基本的Ruby类肯定会导致整个代码库崩溃。 许多内部函数依赖于能够将任意数据存储在字符串中,并且如果这些字符串是任意大写的,那么您会遇到很大的麻烦。 这就像重新定义
Integer#+
所做的一样。 你可以,但你真的不应该。 这将类似于重新定义质子的电荷。 宇宙真的会爆炸。最好编写一种适用于任意字符串或值的适配器方法,并确保它们符合任何怪癖或编码其他系统的用途:
def to_arcahic(string) string.upcase end
例如,如果他们不允许使用重音字符或表情符号,则需要将这些字符和/或转换为其他字符。 也许“é”变成“E”或者你可能只是删除它。
The solution here is not to boil the ocean and make every string in Ruby force everything to uppercase, but to uppercase the things that system needs if and when you provide it to that system.
Changing fundamental Ruby classes in this dramatic a way is bound to cause your entire code-base to implode. Many internals depend on being able to store arbitrary data in strings, and if those strings are arbitrarily uppercased you're in big trouble. It's like redefining what
Integer#+
does. You can, but you really, really shouldn't. This would be akin to redefining the electrical charge of a proton. The universe would literally explode.It's better to write some kind of adapter method that can operate on arbitrary strings or values and make sure they conform to whatever quirks or encoding your other system uses:
def to_arcahic(string) string.upcase end
If, for example, they don't allow accented characters or emoji, you'll need to strip those out and/or convert them to something else. Maybe "é" becomes "E" or maybe you just delete it.
相关问答
更多-
用大写字母拆分字符串(Split a string at uppercase letters)[2022-10-27]
不幸的是,在Python中的零宽度匹配是不可能的。 但是可以改用re.findall : >>> import re >>> re.findall('[A-Z][^A-Z]*', 'TheLongAndWindingRoad') ['The', 'Long', 'And', 'Winding', 'Road'] >>> re.findall('[A-Z][^A-Z]*', 'ABC') ['A', 'B', 'C'] Unfortunately it's not possible to split on ... -
>>> s = 'sdsd' >>> s.upper() 'SDSD' 请参阅字符串方法 >>> s = 'sdsd' >>> s.upper() 'SDSD' See String Methods.
-
没有正则表达式或linq,有一个非常简单的解决方案: bool hasUppercase = !str.equals(str.toLowerCase()); 现在您可以轻松查看: if(!hasUppercase) { // no uppercase letter } else { // there is an uppercase letter } 只检查字符串是否等于它的小写自我。 There is a very easy solution without regex or linq ...
-
使用split将空格中的字符串拆分并测试它是否不是数字和大写: Function Suburb(strInput As String) As String Dim i As Integer Dim spltStr() As String spltStr = Split(strInput) For i = 0 To UBound(spltStr) If UCase(spltStr(i)) = spltStr(i) And Not IsNumeric(spltStr(i)) Then ...
-
无论Python中的小写字母和大写字符如何比较字符串(Comparing string regardless of lowercase and uppercase characters in python)[2024-02-04]
简单的解决方案是将两个值转换为较低或大写,如果相等,则进行比较。 string1, string2 = "ABC", "abc" string1.lower() == string2.lower() Simple solution would be to convert both values to lower or upper case and make comparison if the are equal. string1, string2 = "ABC", "abc" string1.lowe ... -
您遇到崩溃的原因是您的代码修改了字符串文字。 字符串文字中的字符放在受保护的内存区域中,因此可能不会更改:它是未定义的行为。 替换它 uppercase("cold"); 有了这个: char cold[] = "cold"; uppercase(cold); 现在,字符串的字符放在可修改的内存区域中,允许您根据需要进行更改。 The reason you get a crash is that your code modifies a string literal. Characters inside ...
-
for(int y = 0; y <= z; y++){ 应该 for(int y = 0; y < z; y++){ 记住数组索引从零开始。 字符串长度返回 字符串中的16位Unicode字符数 因为循环从零开始,所以循环应该在长度为1时终止。 for(int y = 0; y <= z; y++){ should be for(int y = 0; y < z; y++){ Remember array index starts from ZERO. String length returns ...
-
可能最简单的方法来实现它将使用正则表达式像\b[AZ]{4,}\b表示 \b 字边界 - 放置在字母数字和非字母数字字符之间 [AZ] 范围内的 [AZ] 字符 {4,}至少出现4次(如果我们不想要像I这样的单字母单词)(更多信息请访问: http : //www.regular-expressions.info/repeat.html ) \b另一个单词边界,以确保我们正在阅读整个单词 所以你的代码看起来像: String s = "This text was just made RANDOMLY to ...
-
这里的解决方案并不是让大海沸腾,并且使Ruby中的每一个字符串都将所有内容强制转换为大写字母,而是将大小写转换为系统需要的东西, 以及将其提供给该系统时的情况。 以这种戏剧性的方式更改基本的Ruby类肯定会导致整个代码库崩溃。 许多内部函数依赖于能够将任意数据存储在字符串中,并且如果这些字符串是任意大写的,那么您会遇到很大的麻烦。 这就像重新定义Integer#+所做的一样。 你可以,但你真的不应该。 这将类似于重新定义质子的电荷。 宇宙真的会爆炸。 最好编写一种适用于任意字符串或值的适配器方法,并确保它们 ...
-
不需要另一个字符串 只需使用toLowerCase() public String getString() { return string.replaceAll( String.format("%s|%s|%s", "(?<=[A-Z])(?=[a-z])", "(?<=[^A-Z])(?=[A-Z])", ...