- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Implicits error
Sat, 2009-02-07, 04:47
I'm using implicits to enhance the Joda date time library like so:
object JodaConversions {
implicit def enhancePeriod (period: Period) = new {
def >= (other: Period) =
period.toStandardSeconds.getSeconds >=
other.toStandardSeconds.getSeconds
def > (other: Period) =
period.toStandardSeconds.getSeconds >
other.toStandardSeconds.getSeconds
def <= (other: Period) =
period.toStandardSeconds.getSeconds <=
other.toStandardSeconds.getSeconds
def < (other: Period) =
period.toStandardSeconds.getSeconds <
other.toStandardSeconds.getSeconds
def - (other: Period) = period.minus(other)
def + (other: Period) = period.plus(other)
}
}
And using it elsewhere like so (simplified):
import my.code.JodaConversions
...
if (item.duration > remainingDuration)
Which gives me this error:
error: value > is not a member of time.this.Period
Both values are indeed Period, not some other ReadablePeriod implementation.
What's wrong? I thought it might be the = new { syntax I was using, but
changing that to a named class does nothing.
Sun, 2009-02-08, 17:27
#2
Re: Implicits error
One general question about style and implicits - is it preferred to define
these rich wrappers as anonymous classes (using closures, yes?) as defined
in the example, or is it better to explicitly name the class?
e.g.
class JodaConversions(period: Period) {
def >= (other: Period) =
period.toStandardSeconds.getSeconds >=
other.toStandardSeconds.getSeconds
// etc.
}
object JodaConversions {
implicit def periodToJodaConversions (period: Period) = new
JodaConversions(period)
}
Is this a style issue, or are there significant implementation consequences
of doing one versus the other?
Dan
Sun, 2009-02-08, 17:37
#3
Re: Implicits error
val foo = new { def x = 5 }
foo.x uses reflection at runtime (though it is statically guaranteed to succeed). That's the difference.
2009/2/8 Daniel Wellman <etldan@gmail.com>
foo.x uses reflection at runtime (though it is statically guaranteed to succeed). That's the difference.
2009/2/8 Daniel Wellman <etldan@gmail.com>
One general question about style and implicits - is it preferred to define
these rich wrappers as anonymous classes (using closures, yes?) as defined
in the example, or is it better to explicitly name the class?
e.g.
class JodaConversions(period: Period) {
def >= (other: Period) =
period.toStandardSeconds.getSeconds >=
other.toStandardSeconds.getSeconds
// etc.
}
object JodaConversions {
implicit def periodToJodaConversions (period: Period) = new
JodaConversions(period)
}
Is this a style issue, or are there significant implementation consequences
of doing one versus the other?
Dan
--
View this message in context: http://www.nabble.com/Implicits-error-tp21884807p21900495.html
Sent from the Scala - User mailing list archive at Nabble.com.
Sun, 2009-02-08, 20:17
#4
Re: Implicits error
Reflection is about 10x slower at runtime. I'd use an explicitly named class.
--j
On Sun, Feb 8, 2009 at 8:27 AM, Ricky Clarkson <ricky.clarkson@gmail.com> wrote:
--j
On Sun, Feb 8, 2009 at 8:27 AM, Ricky Clarkson <ricky.clarkson@gmail.com> wrote:
val foo = new { def x = 5 }
foo.x uses reflection at runtime (though it is statically guaranteed to succeed). That's the difference.
2009/2/8 Daniel Wellman <etldan@gmail.com>
One general question about style and implicits - is it preferred to define
these rich wrappers as anonymous classes (using closures, yes?) as defined
in the example, or is it better to explicitly name the class?
e.g.
class JodaConversions(period: Period) {
def >= (other: Period) =
period.toStandardSeconds.getSeconds >=
other.toStandardSeconds.getSeconds
// etc.
}
object JodaConversions {
implicit def periodToJodaConversions (period: Period) = new
JodaConversions(period)
}
Is this a style issue, or are there significant implementation consequences
of doing one versus the other?
Dan
--
View this message in context: http://www.nabble.com/Implicits-error-tp21884807p21900495.html
Sent from the Scala - User mailing list archive at Nabble.com.
Mon, 2009-02-09, 03:57
#5
Re: Implicits error
Ricky Clarkson wrote:
>
> val foo = new { def x = 5 }
> foo.x uses reflection at runtime (though it is statically guaranteed to
> succeed). That's the difference.
>
Now that's worth knowing! I'll change it to using a named class forthwith.
Are there cases when the reflection based one is preferable?
Mon, 2009-02-09, 09:27
#6
Re: Implicits error
Yes. Where the named class version is impossible.
class X { def foo = 5 }
class Y { def foo = 10 }
def gimmeDaFoo(hasFoo: { def foo: Int }) = hasFoo.foo
2009/2/9 Marcus Downing <marcus@minotaur.it>
class X { def foo = 5 }
class Y { def foo = 10 }
def gimmeDaFoo(hasFoo: { def foo: Int }) = hasFoo.foo
2009/2/9 Marcus Downing <marcus@minotaur.it>
Ricky Clarkson wrote:
>
> val foo = new { def x = 5 }
> foo.x uses reflection at runtime (though it is statically guaranteed to
> succeed). That's the difference.
>
Now that's worth knowing! I'll change it to using a named class forthwith.
Are there cases when the reflection based one is preferable?
--
View this message in context: http://www.nabble.com/Implicits-error-tp21884807p21906347.html
Sent from the Scala - User mailing list archive at Nabble.com.
Wed, 2009-02-11, 14:27
#7
Re: Implicits error
Ricky Clarkson wrote:
>
> Yes. Where the named class version is impossible.
>
> class X { def foo = 5 }
> class Y { def foo = 10 }
>
> def gimmeDaFoo(hasFoo: { def foo: Int }) = hasFoo.foo
>
That's something different - that's duck typing. I was asking about the
following:
implicit def gimmeDaFooable(bar: Bar) = new { def foo: Foo = ... }
Wed, 2009-02-11, 15:07
#8
Re: Implicits error
It's called structural typing in the Scala docs. If it was duck typing, you would be able to implicitly convert Any to { def x: Int }
Basically, if it can't go wrong, it ain't duck typing.
2009/2/11 Marcus Downing <marcus@minotaur.it>
Basically, if it can't go wrong, it ain't duck typing.
2009/2/11 Marcus Downing <marcus@minotaur.it>
Ricky Clarkson wrote:
>
> Yes. Where the named class version is impossible.
>
> class X { def foo = 5 }
> class Y { def foo = 10 }
>
> def gimmeDaFoo(hasFoo: { def foo: Int }) = hasFoo.foo
>
That's something different - that's duck typing. I was asking about the
following:
implicit def gimmeDaFooable(bar: Bar) = new { def foo: Foo = ... }
--
View this message in context: http://www.nabble.com/Implicits-error-tp21884807p21954524.html
Sent from the Scala - User mailing list archive at Nabble.com.
On Sat, Feb 7, 2009 at 2:47 PM, Marcus Downing wrote:
> import my.code.JodaConversions
> ...
> if (item.duration > remainingDuration)
Try this:
import my.code.JodaConversions._
This will import the object's members, rather than just the object itself.
Stuart