3 ms·
This feels a lot like scala implicit conversions/views made into idiomatic Ruby. In scala, you could write an "implicit function" that converts Foos to Bars.
by fizx 16y ago
This feels a lot like scala implicit conversions/views made into idiomatic Ruby.
In scala, you could write an "implicit function" that converts Foos to Bars. If and only if you imported the function, you could then (1) pass an instance of Foo to a function accepting Bars and (2) call methods of Bar on a Foo.
For example, if you defined an implicit mapping from Int to IntWithTimeMethods, you could then call something like 1.seconds_from_now, and get a Time back. You can see something like this at work in https://github.com/robey/xrayspecs/blob/master/src/main/scala/com/twitter/xrayspecs/Time.scala https://github.com/robey/xrayspecs/blob/master/src/main/scal...
In scala, it's not required that the class you convert to is a subclass of the original, but it's a really common use case. This "refine" behavior gives you this same ability, but just with an anonymous subclass.
FWIW, I like Scala's implicit conversions. Usually, I see them with things like 1.seconds, or object.toJson, and then you just look among the imports for something referencing time or json. I guess people could think up absurd uses, but then, those people were writing bad code anyways.