Em 09/12/2012 11:17, "Rex Kerr" <ich...@gmail.com> escreveu:
>
> Er, I meant stored, not passed. If you need to store a value class anywhere, a wrapper class is created (even for arrays).
That doesn't make sense to me. An implicit value class used as extension method would never get stored. Why would you do that?
Em 09/12/2012 10:21, "Saxo" <jet...@web.de> escreveu:
>
> Hello,
>
> I know there is the pimp my library pattern with which extension methods can be defined. But this is kind of using a screw driver to hammer a nail into a wall. If there are extension methods, e.g. like in Kotlin (see this article), the overuse of implicit incurred by mimicking extension methods with its follow-up problems (code gets hard to read, you have to debug things to see what is happening) would go away. And implicit can fall back to fullfil the purpose it was initially conceived for.
That *is* one of the purposes it was conceived for. Only it is a general mechanism with namespace and scope, as opposed to extension methods, and I'm betting the lack of these characteristics will be felt once extension methods become widely used.
Em 09/12/2012 10:21, "Saxo" <jet...@web.de> escreveu:
>
> Hello,
>
> I know there is the pimp my library pattern with which extension methods can be defined. But this is kind of using a screw driver to hammer a nail into a wall. If there are extension methods, e.g. like in Kotlin (see this article), the overuse of implicit incurred by mimicking extension methods with its follow-up problems (code gets hard to read, you have to debug things to see what is happening) would go away. And implicit can fall back to fullfil the purpose it was initially conceived for.That *is* one of the purposes it was conceived for. Only it is a general mechanism with namespace and scope, as opposed to extension methods, and I'm betting the lack of these characteristics will be felt once extension methods become widely used.
any other extension mechanism would have the same performance. you call .capitalize and the compiler needs to find the 'extension' that adds that method. naturally compile time resolution it will be slower the more extension points you add in separate units. what makes you think a syntactic sugar will speed things up?
Am Sonntag, 9. Dezember 2012 16:14:23 UTC+1 schrieb Daniel Sobral:
Em 09/12/2012 10:21, "Saxo" <jet...@web.de> escreveu:
>
> Hello,
>
> I know there is the pimp my library pattern with which extension methods can be defined. But this is kind of using a screw driver to hammer a nail into a wall. If there are extension methods, e.g. like in Kotlin (see this article), the overuse of implicit incurred by mimicking extension methods with its follow-up problems (code gets hard to read, you have to debug things to see what is happening) would go away. And implicit can fall back to fullfil the purpose it was initially conceived for.That *is* one of the purposes it was conceived for. Only it is a general mechanism with namespace and scope, as opposed to extension methods, and I'm betting the lack of these characteristics will be felt once extension methods become widely used.
The purpose of implicits looks to me to be some kind of type safe proxy. Extension methods are for something else. Here is an example from "Programming Scala":
val name: String = "scala"
println(name.capitalize.reverse)
This only works, because of this implicit:
implicit def stringWrapper(x: String) = new runtime.RichString(x)
You just add capitalize and reverse as extension method to String and you are done. There is nothing more to it and the implicit doesn't give you any additional bang here. Seems to me to be a workaround for missing extension methods only. Implicits are very powerful when you need some type safe proxy mechanism when implementating some generic library. But in the case above it only makes the compiler slow and complicates the language.
Question is whether those implicit classes added to Scala 2.10 (which I didn't know of) fixes the compiler performance problem (implicit search to make sure that for some specific implicit conversion only a single definition exists).
> Therefore I would propose to add extension methods to Scala. If done well as in Kotlin (very intuitive for the developer) it will not add clutter to the language.
>
> Regards, Oliver
Em 09/12/2012 11:17, "Rex Kerr" <ich...@gmail.com> escreveu:
>
> Er, I meant stored, not passed. If you need to store a value class anywhere, a wrapper class is created (even for arrays).That doesn't make sense to me. An implicit value class used as extension method would never get stored. Why would you do that?
I don't even know what that means. I suggest you look up "implicit" in computer science papers, as well as "view", and then look at when implicits were introduced in Scala, and what they replaced.
Because you want something that isn't just an extension method. If you don't want anything more than an extension method, all that boilerplate is for nothing.
Right now it's an awkward compromise, though, because you'd be better off just passing the original.
I don't argue that extension methods can take the place of implicit conversions, just that they are way simpler yet do the same job as implicit value classes. Implicit value classes as they stand now are great compared to what we had before, but nonetheless they have a large language complexity footprint (because of all the rules about what you can and can't do with them), a large compiler footprint, and a moderately large boilerplate footprint.
Because you want something that isn't just an extension method. If you don't want anything more than an extension method, all that boilerplate is for nothing.
Right now it's an awkward compromise, though, because you'd be better off just passing the original.
I don't argue that extension methods can take the place of implicit conversions, just that they are way simpler yet do the same job as implicit value classes. Implicit value classes as they stand now are great compared to what we had before, but nonetheless they have a large language complexity footprint (because of all the rules about what you can and can't do with them), a large compiler footprint, and a moderately large boilerplate footprint.
As soon as you need to define more than a single extension method, implicit classes have already paid of. I think that's pretty good.
Considering that some existing, important use-cases would require hundreds of extension methods per type and comparing that to the few lines of an implicit class inheriting the necessary methods (which currently solves the issue for good), I have to say that I don't see any value in extension methods.
After using implicit classes, extension methods look like a sad, amateurish joke.
Well, I really think that implicit classes, even in the current state, pull a lot of weight
Because you want something that isn't just an extension method. If you don't want anything more than an extension method, all that boilerplate is for nothing.On Sun, Dec 9, 2012 at 10:09 AM, Daniel Sobral <dcso...@gmail.com> wrote:
Em 09/12/2012 11:17, "Rex Kerr" <ich...@gmail.com> escreveu:
>
> Er, I meant stored, not passed. If you need to store a value class anywhere, a wrapper class is created (even for arrays).That doesn't make sense to me. An implicit value class used as extension method would never get stored. Why would you do that?
Right now it's an awkward compromise, though, because you'd be better off just passing the original.
I don't argue that extension methods can take the place of implicit conversions, just that they are way simpler yet do the same job as implicit value classes. Implicit value classes as they stand now are great compared to what we had before, but nonetheless they have a large language complexity footprint (because of all the rules about what you can and can't do with them), a large compiler footprint, and a moderately large boilerplate footprint.
I expect some of these things to improve (maybe not enough to be worth the extra trouble over extension methods), but right now it's hard to understand the rationale (save for the rationale of building towards the future).
--Rex
P.S. I still am glad we have *some* mechanism for this; I've just gone and switched a whole bunch of actual methods into "extension methods" via implicit value classes. It really improves the use-site noticeably without losing performance. But the library is kind of uglfied by it:
// Before
def nan(f: Float) = java.lang.Float.isNaN(f)
// After
implicit class RicherFloat(f: Float) extends AnyVal {
def nan = java.lang.Float.isNaN(f)
}
I don't even know what that means. I suggest you look up "implicit" in computer science papers, as well as "view", and then look at when implicits were introduced in Scala, and what they replaced.
You may know the Binding class in Groovy. If a method is invoked from a subclass and cannot be dispatched, the invoke method in class Binding is called. You can redefine it in your subclass and then redirect the method send that could not be dispatched to some other object. This way you can implement a proxy.
With implicits you can do the same, but there is no runtime penalty caused by method lookup and dispatch at runtime and it's also type safe contrary to the solution in Groovy. In that way implicits can serve to implement type safe proxies.You would have understood if you had looked up "type safe" and "proxy" in the computer science papers.