SCL-9153 Warn about calling .toString() on Option#631
SCL-9153 Warn about calling .toString() on Option#631cobr123 wants to merge 19 commits intoJetBrains:idea222.xfrom
Conversation
|
Thanks for the contribution! I have some doubts of making this inspection enabled by default. |
|
Also, I don't think that we should provide a "quick fix" which leads to a runtime error. Maybe we should use It won't be the same as with nullable value, because it most of the cases from previous comment |
|
NoSuchElementException was used because of main use case is when toString accidentally called on sys.env.get("host") and then failing at runtime. |
|
That might be your use case but that doesn't mean it's the right way to implement it
…On Tue Sep 20, 2022, 07:55 AM GMT, Roman Tabulov ***@***.***> wrote:
NoSuchElementException was used because of main use case is when toString accidentally called on sys.env.get("host") and then failing at runtime.
—
Reply to this email directly, view it on GitHub <#631 (comment)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/AAAYAUCECS4KNORJSZD22V3V7FUXLANCNFSM6AAAAAAQPMZWKM>.
You are receiving this because you are subscribed to this thread.Message ID: ***@***.***>
|
|
It`s not clear how to use mkString on Option. There is no point to emulate toString. |
It's one of the many many possible use cases.
Like this: Option(42).mkString //equals to "42"
None.mkString //equalst to empty string ""
Sorry, don't think I understood you. |
|
mkString is not necessarily the best answer either. Maybe the current type has a good toString and later it changes to another type that doesn't. Besides, None usually shouldn't be an empty string. Why not use fold? Usually when I need to convert an Option to a String that's what I end up doing, e.g. myOption.fold("none")(_.summaryString) |
|
Or it could be consistent with nullable values and be "null" when empty. Less magic constants would be introduced |
…e.StringBuilder.append(thing)
|
@cobr123 |
|
@unkarjedy, I finished |
|
@unkarjedy, now finished for sure |
|
I really don't consider mkString the best option. At I explained if the Option is None it will print nothing which will usually look weird and isn't what you want. The second issue is that it hides the fact that it's essentially calling toString on whatever's inside the Option. This is often as bad as calling toString on an Option, except that it's not explicit so the IDE can't help and to change it requires replacing the mkString. This could be solved with fold, as I said. The equivalent of mkString with fold is Personally I almost always use fold, though. |




https://youtrack.jetbrains.com/issue/SCL-9153/Warn-about-calling-toString-on-Option