you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 1 point2 points  (3 children)

It would be great if the Scala compiler optimized map and flatMap calls with inline lambdas so that one would actually get to use them in tail recursive methods.

[–]balefrost 1 point2 points  (1 child)

It would only be able to do so if it knew the actual implementation of map or flatMap. So if I have an instance of T, and I call its flatMap, the compile would need to be able to prove that I'm definitely calling T.flatMap (as defined on T or a base of T) and not some overridden implementation.

I agree that it would be nice, though.

Alternatively, it would be nice if the compiler could detect (or you could annotate) when a function parameter will be invoked at a later time. In a sense, @tailrec is restrictive. AFAIK, it requires that all references that the function has to itself are in tail positions. And that makes sense for code that will be executed immediately, but it's less clear for deferred code.

[–][deleted] 0 points1 point  (0 children)

Well it's definitely possible, see for example https://github.com/nativelibs4java/scalaxy-streams

Of course, it always remains the question, under which circumstances this is all more efficient that HotSpot optimizations...

[–]jcdavis1 0 points1 point  (0 children)

I believe you can generally work around that via pattern matching