首页 \ 问答 \ Scala案例类背后的内容是什么?(What's behind Scala case classes?)

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
更新时间:2023-08-16 22:08

最满意答案

这里的解决方案并不是让大海沸腾,并且使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.

相关问答

更多

相关文章

更多

最新问答

更多
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • 电脑高中毕业学习去哪里培训
  • 电脑系统专业就业状况如何啊?
  • IEnumerable linq表达式(IEnumerable linq expressions)
  • 如何在Spring测试中连接依赖关系(How to wire dependencies in Spring tests)
  • Solr可以在没有Lucene的情况下运行吗?(Can Solr run without Lucene?)
  • 如何保证Task在当前线程上同步运行?(How to guarantee that a Task runs synchronously on the current thread?)
  • 在保持每列的类的同时向数据框添加行(Adding row to data frame while maintaining the class of each column)
  • 的?(The ? marks in emacs/haskell and ghc mode)
  • 一个线程可以调用SuspendThread传递自己的线程ID吗?(Can a thread call SuspendThread passing its own thread ID?)
  • 延迟socket.io响应,并“警告 - websocket连接无效”(Delayed socket.io response, and “warn - websocket connection invalid”)
  • 悬停时的图像转换(Image transition on hover)
  • IIS 7.5仅显示homecontroller(IIS 7.5 only shows homecontroller)
  • 没有JavaScript的复选框“关闭”值(Checkbox 'off' value without JavaScript)
  • java分布式框架有哪些
  • Python:填写表单并点击按钮确认[关闭](Python: fill out a form and confirm with a button click [closed])
  • PHP将文件链接到根文件目录(PHP Linking Files to Root File Directory)
  • 我如何删除ListView中的项目?(How I can remove a item in my ListView?)
  • 您是否必须为TFS(云)中的每个BUG创建一个TASK以跟踪时间?(Do you have to create a TASK for every BUG in TFS (Cloud) to track time?)
  • typoscript TMENU ATagParams小写(typoscript TMENU ATagParams lowercase)
  • 武陟会计培训类的学校哪个好点?
  • 从链接中删除文本修饰(Remove text decoration from links)