Skip to content

Instantly share code, notes, and snippets.

@bmc
Created September 2, 2011 15:08
Show Gist options
  • Save bmc/1188867 to your computer and use it in GitHub Desktop.
Save bmc/1188867 to your computer and use it in GitHub Desktop.
Scala compiler oddity #1
// So, given this declaration:
import java.io.File
val scalaDirs = new File("target").listFiles.filter(_.getName.startsWith("scala"))
// Why does this work:
scalaDirs.take(1).map(new File(_, "classes")).toList(0)
// but this fails to compile?
scalaDirs.take(1).map(new File(_, "classes")).toArray(0)
// <console>:15: error: type mismatch;
// found : Int(0)
// required: scala.reflect.ClassManifest[?]
// res8.take(1).map(new File(_, "classes")).toArray(0)
// This also fails to compile, though the error is slightly different:
scalaDirs.take(1).map(new File(_, "classes"))(0)
// <console>:15: error: type mismatch;
// found : Int(0)
// required: scala.collection.generic.CanBuildFrom[Array[java.io.File],java.io.File,?]
// res8.take(1).map(new File(_, "classes"))(0)
// ... even though
scalaDirs.take(1).map(new File(_, "classes"))
// is of type Array[File]
@bmc
Copy link
Author

bmc commented Sep 2, 2011

jsuereth: @brianclapper toArray requires an implicit ClassManifest. It thinks you're passing it. Try toArray(implicitly)(0)

d6: @brianclapper Instead of saying toArray.apply(0) you tricked it into thinking you're passing the implicit param explicitly. Similar in 2nd

Okay, that makes sense. Use of apply seems like the easiest-to-read solution, to me.

@alxbok
Copy link

alxbok commented Sep 2, 2011

I would expect that putting it in brackets would help as well:
(1) val kk = (scalaDirs.take(1).map(new File(_, "classes")))(0)
It does not, but this works:
(2) val kk = scalaDirs.take(1).map(new File(_, "classes")); val kkk = kk(0)
which is pretty much the same as (1), hence it seems like a compiler bug

@jsuereth
Copy link

jsuereth commented Sep 2, 2011

Why not just use apply directly, yeah:

scalaDirs take 1 map (new File(_, "classes")) apply 0

Real men use point-free :)

@bmc
Copy link
Author

bmc commented Sep 2, 2011

That's actually what I'm doing. Though I am using the points. Old habits die hard...

@jsuereth
Copy link

jsuereth commented Sep 2, 2011

scalaDirs.headOption map (new File(_,"classes")) get

or

scalaDirs.headOption map (new File(_, "classes")) getOrElse error("Can't find scala dir")

@bmc
Copy link
Author

bmc commented Sep 2, 2011

Ah. I like that.

@alxbok
Copy link

alxbok commented Sep 2, 2011

@jsuereth I would rather call it pointless than pointfree ;)

@jsuereth
Copy link

jsuereth commented Sep 2, 2011

I like that.

@bmc
Copy link
Author

bmc commented Sep 2, 2011

Isn't this a prime example of why Scala is hard?

(ducks)

@jsuereth
Copy link

jsuereth commented Sep 2, 2011

Sure, because programming is so easy I should make McDonald's salary.

@alxbok
Copy link

alxbok commented Sep 2, 2011

I disagree with that post of Mr. Pollak. And if you compare Scala with C++ or say C++0x it is not that hard after all...
And here some stuff for those who say Java is simple: http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.html After studying this nice tutorial carefully, which I do strongly recommend, they might come up with "Java is too complex". Hard things are hard and simple things are simple, that is what you try to say - this is a tautology.

@bmc
Copy link
Author

bmc commented Sep 2, 2011

I think you'll find that Josh and I agree with you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment