Scala Wart: Classes cannot have only implicit parameter lists
See original GitHub issueOpening this issue, as suggested by Martin, to provide a place to discuss the individual warts brought up in the blog post Warts of the Scala Programming Language and the possibility of mitigating/fixing them in Dotty (and perhaps later in Scala 2.x). These are based on Scala 2.x behavior, which I understand Dotty follows closely, apologies in advance if it has already been fixed
This doesn’t work:
@ class Foo(i: Int)
defined class Foo
@ new Foo(1)
res50: Foo = $sess.cmd49$Foo@7230510
@ class Bar(implicit i: Int)
defined class Bar
@ new Bar(1)
cmd52.sc:1: no arguments allowed for nullary constructor Bar: ()(implicit i: Int)$sess.cmd51.Bar
val res52 = new Bar(1)
^
Compilation Failed
But this does:
@ new Bar()(1)
res52: Bar = $sess.cmd51$Bar@467de021
This one straddles the line between “Wart” and “Bug”, but definitely should be fixed so that a class defined with one argument list doesn’t magically sprout two.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:15
- Comments:6 (5 by maintainers)
Top Results From Across the Web
Warts of the Scala Programming Language
Classes cannot have only implicit parameter lists ; Presence of comments affects program logic; Conflating total destructuring with partial ...
Read more >Why implicit parameter not working when combined with non ...
A method or constructor can have only one implicit parameter list, and it must be the last parameter list given. has been altered...
Read more >Proposal To Revise Implicit Parameters - Scala Contributors
This proposal goes against the nature of Scala where we do have control over how variables are defined. There is a difference between...
Read more >Warts of the Scala Programming Language - Reddit
Classes cannot have only implicit parameter lists. This one is surprisingly related to the previous point. Because of Java interop, we want new...
Read more >Haskell has them, and Scala encodes them using a horrible ...
In Scala you can't do it, unless they introduced their own String class, ... Because of implicit parameters, Scala achieves the best marriage...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’m reopening this issue because the syntax has evolved quite a bit since it was closed and the situation is now “wart-y” again in my opinion:
The fact that there’s some sort of optional empty parameter list is fairly weird, but it gets weirder when combined with the fact that we can now have a regular parameter list after a using parameter list:
Here in particular, the fact that one cannot write
new Bar("")
(orBar("")
using creator application syntax) seems like a real wart to me.It turns out this is can alternatively be seen as the problem of old-style implicits taking regular arguments. If we move to
given
parameters and arguments, all test cases work as expected. E.g. the following compiles:Classes without a leading regular parameter list still assume
()
as first parameter list. I tried for a while to avoid this but in the end this created more problems than it solved. In particular, with creator applications, it’s quite reasonable to demand thatFoo()
works even if classFoo
is parameterless. Otherwise we’d either have to go back tonew Foo
for this case, or make sure everyone creates their classes with at least a()
parameter list. Either choice is unpalatable.But if creator applications take a
()
it’s consistent to expect that corresponding constructors do so as well.