question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Scala Wart: Classes cannot have only implicit parameter lists

See original GitHub issue

Opening 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:closed
  • Created 6 years ago
  • Reactions:15
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

8reactions
smartercommented, Aug 24, 2020

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:

given Int = 1

class Foo(using x: Int)

new Foo // compiles
new Foo() // compiles
new Foo(using 1) // compiles
new Foo()(using 1) // compiles

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:

class Bar(using x: Int)(y: String) // For a more realistic usecase, see https://github.com/lampepfl/dotty-feature-requests/issues/133

new Bar("") // ERROR: too many arguments for constructor Bar: ()(using x: Int)(y: String): Bar
new Bar()("") // compiles
new Bar(using 1)("") // compiles
new Bar()(using 1)("") // compiles

Here in particular, the fact that one cannot write new Bar("") (or Bar("") using creator application syntax) seems like a real wart to me.

3reactions
oderskycommented, Apr 11, 2019

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:

class TC
implied tc for TC

class Foo given TC

object Test {
  new Foo
  new Foo given tc
  new Foo()
  new Foo() given tc
  Foo()
  Foo() given tc
}

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 that Foo() works even if class Foo is parameterless. Otherwise we’d either have to go back to new 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found