- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Default type inferred
Wed, 2010-05-12, 10:35
Scalars,
this may be a total brainfart on my account, it might be due to lack of breakfast.
given:
def foo[T : Manifest] = manifest[T]
how to get it to infer T = Any given:
val a = foo
Because right now it infers Nothing,
I've tried every possible combination of Generics-foo I know, but to no avail.
--
Viktor Klang
| "A complex system that works is invariably
| found to have evolved from a simple system
| that worked." - John Gall
Akka - the Actor Kernel: Akkasource.org
Twttr: twitter.com/viktorklang
this may be a total brainfart on my account, it might be due to lack of breakfast.
given:
def foo[T : Manifest] = manifest[T]
how to get it to infer T = Any given:
val a = foo
Because right now it infers Nothing,
I've tried every possible combination of Generics-foo I know, but to no avail.
--
Viktor Klang
| "A complex system that works is invariably
| found to have evolved from a simple system
| that worked." - John Gall
Akka - the Actor Kernel: Akkasource.org
Twttr: twitter.com/viktorklang
Wed, 2010-05-12, 11:07
#2
Re: Default type inferred
On Wed, May 12, 2010 at 11:45 AM, Kevin Wright <kev.lee.wright@googlemail.com> wrote:
I think that the magic you're looking for could be:
def foo[T : Manifest] = implicitly[Manifest[T]].erasure
I was just providing a really basic example. That's not really the problem.
The problem is that the compiler infers Nothing when no type-hint is provided, but I'd like the compiler to infer Any in that case:
for a more illustrative example:
def ! : Option[T] = ...
val result = foo !! "pigdog"
I want that to be treated as:
val result : Option[Any] = foo !! "pigdog"
because honestly I don't want line-noise like ": Option[Any]" or the even worse:
val result = foo.! //<--- horrid
So basically what I need is to have Any be the default type, and not Nothing.
On 12 May 2010 10:34, Viktor Klang <viktor.klang@gmail.com> wrote:Scalars,
this may be a total brainfart on my account, it might be due to lack of breakfast.
given:
def foo[T : Manifest] = manifest[T]
how to get it to infer T = Any given:
val a = foo
Because right now it infers Nothing,
I've tried every possible combination of Generics-foo I know, but to no avail.
--
Viktor Klang
| "A complex system that works is invariably
| found to have evolved from a simple system
| that worked." - John Gall
Akka - the Actor Kernel: Akkasource.org
Twttr: twitter.com/viktorklang
--
Kevin Wright
mail/google talk: kev.lee.wright@googlemail.com
wave: kev.lee.wright@googlewave.com
skype: kev.lee.wright
twitter: @thecoda
--
Viktor Klang
| "A complex system that works is invariably
| found to have evolved from a simple system
| that worked." - John Gall
Akka - the Actor Kernel: Akkasource.org
Twttr: twitter.com/viktorklang
Wed, 2010-05-12, 11:57
#3
Re: Default type inferred
Viktor Klang schrieb:
AANLkTimZbWShWepEA-WEVrakZe3ttmWUjWXGXwUYsMPn [at] mail [dot] gmail [dot] com" type="cite">
On Wed, May 12, 2010 at 11:45 AM, Kevin Wright <kev [dot] lee [dot] wright [at] googlemail [dot] com" rel="nofollow">kev.lee.wright@googlemail.com> wrote:
I think that the magic you're looking for could be:
def foo[T : Manifest] = implicitly[Manifest[T]].erasure
I was just providing a really basic example. That's not really the problem.
The problem is that the compiler infers Nothing when no type-hint is provided, but I'd like the compiler to infer Any in that case:
for a more illustrative example:
def ! : Option[T] = ...
val result = foo !! "pigdog"
I want that to be treated as:
val result : Option[Any] = foo !! "pigdog"
I' sorry, maybe I'm stupid. but why do you need the type T when you want Type Any as result?
So whats wrong with
def !: Option[Any] = ....
??
Sorry if thats a stupid question ,-)
Dan <- Noob
AANLkTimZbWShWepEA-WEVrakZe3ttmWUjWXGXwUYsMPn [at] mail [dot] gmail [dot] com" type="cite">
because honestly I don't want line-noise like ": Option[Any]" or the even worse:
val result = foo.! //<--- horrid
So basically what I need is to have Any be the default type, and not Nothing.
On 12 May 2010 10:34, Viktor Klang <viktor [dot] klang [at] gmail [dot] com" target="_blank" rel="nofollow">viktor.klang@gmail.com> wrote:
Scalars,
this may be a total brainfart on my account, it might be due to lack of breakfast.
given:
def foo[T : Manifest] = manifest[T]
how to get it to infer T = Any given:
val a = foo
Because right now it infers Nothing,
I've tried every possible combination of Generics-foo I know, but to no avail.
Wed, 2010-05-12, 12:07
#4
Re: Default type inferred
On Wed, May 12, 2010 at 12:56 PM, Daniel Degrandi <Daniel.Degrandi@uni-duesseldorf.de> wrote:
Viktor Klang schrieb:
On Wed, May 12, 2010 at 11:45 AM, Kevin Wright <kev.lee.wright@googlemail.com> wrote:
I think that the magic you're looking for could be:
def foo[T : Manifest] = implicitly[Manifest[T]].erasure
I was just providing a really basic example. That's not really the problem.
The problem is that the compiler infers Nothing when no type-hint is provided, but I'd like the compiler to infer Any in that case:
for a more illustrative example:
def ! : Option[T] = ...
val result = foo !! "pigdog"
I want that to be treated as:
val result : Option[Any] = foo !! "pigdog"
I' sorry, maybe I'm stupid. but why do you need the type T when you want Type Any as result?
So whats wrong with
def !: Option[Any] = ....
??
Sorry if thats a stupid question ,-)
Dan <- Noob
I want the caller to be able to specify requested type if desired (can use the manifest to enforce it)
Makes any sense?
because honestly I don't want line-noise like ": Option[Any]" or the even worse:
val result = foo.! //<--- horrid
So basically what I need is to have Any be the default type, and not Nothing.
On 12 May 2010 10:34, Viktor Klang <viktor.klang@gmail.com> wrote:
Scalars,
this may be a total brainfart on my account, it might be due to lack of breakfast.
given:
def foo[T : Manifest] = manifest[T]
how to get it to infer T = Any given:
val a = foo
Because right now it infers Nothing,
I've tried every possible combination of Generics-foo I know, but to no avail.
Wed, 2010-05-12, 12:17
#5
Re: Default type inferred
Viktor Klang schrieb:
AANLkTilifP34J1AupgpfaeGZXhjVcaFx6axfChDVBnjD [at] mail [dot] gmail [dot] com" type="cite">
On Wed, May 12, 2010 at 12:56 PM, Daniel Degrandi <Daniel [dot] Degrandi [at] uni-duesseldorf [dot] de" rel="nofollow">Daniel.Degrandi@uni-duesseldorf.de> wrote:
Viktor Klang schrieb:
On Wed, May 12, 2010 at 11:45 AM, Kevin Wright <kev [dot] lee [dot] wright [at] googlemail [dot] com" target="_blank" rel="nofollow">kev.lee.wright@googlemail.com> wrote:
I think that the magic you're looking for could be:
def foo[T : Manifest] = implicitly[Manifest[T]].erasure
I was just providing a really basic example. That's not really the problem.
The problem is that the compiler infers Nothing when no type-hint is provided, but I'd like the compiler to infer Any in that case:
for a more illustrative example:
def ! : Option[T] = ...
val result = foo !! "pigdog"
I want that to be treated as:
val result : Option[Any] = foo !! "pigdog"
I' sorry, maybe I'm stupid. but why do you need the type T when you want Type Any as result?
So whats wrong with
def !: Option[Any] = ....
??
Sorry if thats a stupid question ,-)
Dan <- Noob
I want the caller to be able to specify requested type if desired (can use the manifest to enforce it)
Makes any sense?
Ahh, I see. Interesting. We would need default type parameters for this. Something like:
def !: Option[T]= ...
I'm nnot aware of any lauguage syntax that allows something like this...
Dan
AANLkTilifP34J1AupgpfaeGZXhjVcaFx6axfChDVBnjD [at] mail [dot] gmail [dot] com" type="cite">
because honestly I don't want line-noise like ": Option[Any]" or the even worse:
val result = foo.! //<--- horrid
So basically what I need is to have Any be the default type, and not Nothing.
On 12 May 2010 10:34, Viktor Klang <viktor [dot] klang [at] gmail [dot] com" target="_blank" rel="nofollow">viktor.klang@gmail.com> wrote:
Scalars,
this may be a total brainfart on my account, it might be due to lack of breakfast.
given:
def foo[T : Manifest] = manifest[T]
how to get it to infer T = Any given:
val a = foo
Because right now it infers Nothing,
I've tried every possible combination of Generics-foo I know, but to no avail.
Wed, 2010-05-12, 12:47
#6
Re: Default type inferred
> So basically what I need is to have Any be the default type, and not
> Nothing.
Hmm, I realize it might be silly and has the problem that foo never
returns the manifest of Nothing but anyway... how about:
def foo[T: Manifest] =
if (manifest[T] == manifest[Nothing]) manifest[Any]
else manifest[T]
val a = foo
:o)
Wed, 2010-05-12, 12:57
#7
Re: Default type inferred
On Wed, 12 May 2010 12:02:30 +0200, Viktor Klang wrote:
> On Wed, May 12, 2010 at 11:45 AM, Kevin Wright <
> kev.lee.wright@googlemail.com> wrote:
>
> > I think that the magic you're looking for could be:
> >
> > def foo[T : Manifest] = implicitly[Manifest[T]].erasure
> >
> >
> I was just providing a really basic example. That's not really the problem.
> The problem is that the compiler infers Nothing when no type-hint is
> provided, but I'd like the compiler to infer Any in that case:
>
> for a more illustrative example:
>
> def ! : Option[T] = ...
This may not be the best example as well, because Option is covariant in T.
> val result = foo !! "pigdog"
>
> I want that to be treated as:
>
> val result : Option[Any] = foo !! "pigdog"
So you do indeed get result : Option[Nothing]
but Option[Nothing] <: Option[Any]
so as long as it is a val and not a var does it really matter?
> because honestly I don't want line-noise like ": Option[Any]" or the even
> worse:
>
> val result = foo.! //<--- horrid
>
>
> So basically what I need is to have Any be the default type, and not
> Nothing.
Actually Any gets inferred in such a situation when the result type is
contravariant in the inferred type parameter.
Wed, 2010-05-12, 13:07
#8
Re: Default type inferred
2010/5/12 Michal Politowski <mpol@charybda.icm.edu.pl>
On Wed, 12 May 2010 12:02:30 +0200, Viktor Klang wrote:
> On Wed, May 12, 2010 at 11:45 AM, Kevin Wright <
> kev.lee.wright@googlemail.com> wrote:
>
> > I think that the magic you're looking for could be:
> >
> > def foo[T : Manifest] = implicitly[Manifest[T]].erasure
> >
> >
> I was just providing a really basic example. That's not really the problem.
> The problem is that the compiler infers Nothing when no type-hint is
> provided, but I'd like the compiler to infer Any in that case:
>
> for a more illustrative example:
>
> def ! : Option[T] = ...
This may not be the best example as well, because Option is covariant in T.
Doesn't matter, the same result is achieved with:
scala> def foo[T : Manifest] = manifest[T]
foo: [T](implicit evidence$1: Manifest[T])Manifest[T]
scala> val a = foo
a: Manifest[Nothing] = Nothing
> val result = foo !! "pigdog"
>
> I want that to be treated as:
>
> val result : Option[Any] = foo !! "pigdog"
So you do indeed get result : Option[Nothing]
but Option[Nothing] <: Option[Any]
so as long as it is a val and not a var does it really matter?
Yes it does.
Because could can't even pattern-match your way outta that hell.
val a = foo //returns Option[T]
scala> a match {
| case Some(List(1)) => println("a list!")
| case _ => println("Just a silly pigdog")
| }
<console>:8: error: type mismatch;
found : Int(1)
required: Nothing
case Some(List(1)) => println("a list!")
So this means that all code that will handle Any result type will have to be boilered:
val r : Option[Any] = foo !! "bar"
Unless you commit something even more esoteric:
type ? = Option[Any]
scala> val r : ? = foo !! "bar"
scala> r match {
| case Some("foo") => "pigdog"
| case _ => "dogpig"
| }
> because honestly I don't want line-noise like ": Option[Any]" or the even
> worse:
>
> val result = foo.! //<--- horrid
>
>
> So basically what I need is to have Any be the default type, and not
> Nothing.
Actually Any gets inferred in such a situation when the result type is
contravariant in the inferred type parameter.
--
Michał Politowski
Talking has been known to lead to communication if practiced carelessly.
--
Viktor Klang
| "A complex system that works is invariably
| found to have evolved from a simple system
| that worked." - John Gall
Akka - the Actor Kernel: Akkasource.org
Twttr: twitter.com/viktorklang
Wed, 2010-05-12, 13:37
#9
Re: Default type inferred
hmm really sorry for another dumb suggestion
but won't the following do it:
def foo[T](implicit evidence: Manifest[T] = manifest[Any]) = manifest[T]
?
Wed, 2010-05-12, 13:47
#10
Re: Default type inferred
On Wed, May 12, 2010 at 2:27 PM, Andreas Flierl <andreas@flierl.eu> wrote:
hmm really sorry for another dumb suggestion
but won't the following do it:
def foo[T](implicit evidence: Manifest[T] = manifest[Any]) = manifest[T]
?
scala> def foo[T](implicit evidence: Manifest[T] = manifest[Any]) = manifest[T]
foo: [T](implicit evidence: Manifest[T])Manifest[T]
scala> val a = foo
a: Manifest[Nothing] = Nothing
--
Viktor Klang
| "A complex system that works is invariably
| found to have evolved from a simple system
| that worked." - John Gall
Akka - the Actor Kernel: Akkasource.org
Twttr: twitter.com/viktorklang
Wed, 2010-05-12, 13:57
#11
Re: Default type inferred
> def foo[T](implicit evidence: Manifest[T] = manifest[Any]) = manifest[T]
I hate having no REPL here... I could have sworn the Eclipse mouseover
showed Manifest[Any]...
but now it doesn't...
Wed, 2010-05-12, 14:07
#12
Re: Default type inferred
On Wed, May 12, 2010 at 10:34 AM, Viktor Klang wrote:
> this may be a total brainfart on my account, it might be due to lack of
> breakfast.
>
> given:
>
> def foo[T : Manifest] = manifest[T]
>
> how to get it to infer T = Any given:
>
> val a = foo
>
> Because right now it infers Nothing,
> I've tried every possible combination of Generics-foo I know, but to no
> avail.
How about good old-fashioned overloading?
scala> object DefaultAny { def foo = manifest[Any] ; def foo[T :
Manifest] = manifest[T] }
defined module DefaultAny
scala> import DefaultAny._
import DefaultAny._
scala> val a = foo
a: Manifest[Any] = Any
scala> val b = foo[String]
b: Manifest[String] = java.lang.String
Cheers,
Miles
Wed, 2010-05-12, 14:17
#13
Re: Default type inferred
Which brings me to the question:
Why does this compile?
object Scratchpad {
def main(args: Array[String]): Unit = {
val a = foo
val b = foo[String]
}
def foo[T: Manifest] = manifest[T]
def foo = manifest[Any]
}
And this doesn't ?
object Scratchpad {
def main(args: Array[String]): Unit = {
def foo[T: Manifest] = manifest[T]
def foo = manifest[Any]
val a = foo
val b = foo[String]
}
}
Thanks
Andreas
> How about good old-fashioned overloading?
>
> scala> object DefaultAny { def foo = manifest[Any] ; def foo[T :
> Manifest] = manifest[T] }
> defined module DefaultAny
>
> scala> import DefaultAny._
> import DefaultAny._
>
> scala> val a = foo
> a: Manifest[Any] = Any
>
> scala> val b = foo[String]
> b: Manifest[String] = java.lang.String
>
> Cheers,
>
>
> Miles
Wed, 2010-05-12, 14:27
#14
Re: Default type inferred
On Wed, May 12, 2010 at 2:50 PM, Miles Sabin <miles@milessabin.com> wrote:
On Wed, May 12, 2010 at 10:34 AM, Viktor Klang <viktor.klang@gmail.com> wrote:
> this may be a total brainfart on my account, it might be due to lack of
> breakfast.
>
> given:
>
> def foo[T : Manifest] = manifest[T]
>
> how to get it to infer T = Any given:
>
> val a = foo
>
> Because right now it infers Nothing,
> I've tried every possible combination of Generics-foo I know, but to no
> avail.
How about good old-fashioned overloading?
scala> object DefaultAny { def foo = manifest[Any] ; def foo[T :
Manifest] = manifest[T] }
defined module DefaultAny
scala> import DefaultAny._
import DefaultAny._
scala> val a = foo
a: Manifest[Any] = Any
scala> val b = foo[String]
b: Manifest[String] = java.lang.String
Arghh....
Seems like I need to roll my own result type to get it to work:
scala> class MyOwnOptionTypeGdamnit[-X]
defined class MyOwnOptionTypeGdamnit
scala>
scala> def foo[T] : MyOwnOptionTypeGdamnit[T] = null
foo: [T]MyOwnOptionTypeGdamnit[T]
scala>
scala> foo
res19: MyOwnOptionTypeGdamnit[Any] = null
scala> val a : MyOwnOptionTypeGdamnit[String] = null
a: MyOwnOptionTypeGdamnit[String] = null
Cheers,
Miles
--
Miles Sabin
tel: +44 7813 944 528
gtalk: miles@milessabin.com
skype: milessabin
http://www.chuusai.com/
http://twitter.com/milessabin
--
Viktor Klang
| "A complex system that works is invariably
| found to have evolved from a simple system
| that worked." - John Gall
Akka - the Actor Kernel: Akkasource.org
Twttr: twitter.com/viktorklang
Wed, 2010-05-12, 14:47
#15
Re: Default type inferred
On Wed, May 12, 2010 at 3:09 PM, Viktor Klang <viktor.klang@gmail.com> wrote:
On Wed, May 12, 2010 at 2:50 PM, Miles Sabin <miles@milessabin.com> wrote:On Wed, May 12, 2010 at 10:34 AM, Viktor Klang <viktor.klang@gmail.com> wrote:
> this may be a total brainfart on my account, it might be due to lack of
> breakfast.
>
> given:
>
> def foo[T : Manifest] = manifest[T]
>
> how to get it to infer T = Any given:
>
> val a = foo
>
> Because right now it infers Nothing,
> I've tried every possible combination of Generics-foo I know, but to no
> avail.
How about good old-fashioned overloading?
scala> object DefaultAny { def foo = manifest[Any] ; def foo[T :
Manifest] = manifest[T] }
defined module DefaultAny
scala> import DefaultAny._
import DefaultAny._
scala> val a = foo
a: Manifest[Any] = Any
scala> val b = foo[String]
b: Manifest[String] = java.lang.String
Arghh....
Seems like I need to roll my own result type to get it to work:
scala> class MyOwnOptionTypeGdamnit[-X]
defined class MyOwnOptionTypeGdamnit
scala>
scala> def foo[T] : MyOwnOptionTypeGdamnit[T] = null
foo: [T]MyOwnOptionTypeGdamnit[T]
scala>
scala> foo
res19: MyOwnOptionTypeGdamnit[Any] = null
scala> val a : MyOwnOptionTypeGdamnit[String] = null
a: MyOwnOptionTypeGdamnit[String] = null
Yes, this would be totally retarded. I won't do that.
Cheers,
Miles
--
Miles Sabin
tel: +44 7813 944 528
gtalk: miles@milessabin.com
skype: milessabin
http://www.chuusai.com/
http://twitter.com/milessabin
--
Viktor Klang
| "A complex system that works is invariably
| found to have evolved from a simple system
| that worked." - John Gall
Akka - the Actor Kernel: Akkasource.org
Twttr: twitter.com/viktorklang
--
Viktor Klang
| "A complex system that works is invariably
| found to have evolved from a simple system
| that worked." - John Gall
Akka - the Actor Kernel: Akkasource.org
Twttr: twitter.com/viktorklang
Wed, 2010-05-12, 15:07
#16
Re: Default type inferred
On Wed, May 12, 2010 at 2:43 PM, Viktor Klang wrote:
> Yes, this would be totally retarded. I won't do that.
It might help if you showed us a more complete example of what you
want to work. We've seen,
def foo[T : Manifest] = manifest[T]
def ! : Option[T] = ...
val result = foo !! "pigdog"
but that's never going to compile anyway ... is !! meant to be pimped
on to Manifest?
Cheers,
Miles
Wed, 2010-05-12, 15:17
#17
Re: Default type inferred
On Wed, May 12, 2010 at 3:51 PM, Miles Sabin <miles@milessabin.com> wrote:
On Wed, May 12, 2010 at 2:43 PM, Viktor Klang <viktor.klang@gmail.com> wrote:
> Yes, this would be totally retarded. I won't do that.
It might help if you showed us a more complete example of what you
want to work. We've seen,
def foo[T : Manifest] = manifest[T]
def ! : Option[T] = ...
val result = foo !! "pigdog"
but that's never going to compile anyway ... is !! meant to be pimped
on to Manifest?
Nah, I was just experimenting with using Manifests to determine dynamic response types.
Cheers,
Miles
--
Miles Sabin
tel: +44 7813 944 528
gtalk: miles@milessabin.com
skype: milessabin
http://www.chuusai.com/
http://twitter.com/milessabin
--
Viktor Klang
| "A complex system that works is invariably
| found to have evolved from a simple system
| that worked." - John Gall
Akka - the Actor Kernel: Akkasource.org
Twttr: twitter.com/viktorklang
Wed, 2010-05-12, 16:27
#18
Re: Default type inferred
Viktor Klang schrieb:
AANLkTimGUoqnMfxnQVYdXCUCM0IQEm8WlMEtJO7NFPgd [at] mail [dot] gmail [dot] com" type="cite">
On Wed, May 12, 2010 at 3:51 PM, Miles Sabin <miles [at] milessabin [dot] com" rel="nofollow">miles@milessabin.com> wrote:
On Wed, May 12, 2010 at 2:43 PM, Viktor Klang <viktor [dot] klang [at] gmail [dot] com" rel="nofollow">viktor.klang@gmail.com> wrote:
> Yes, this would be totally retarded. I won't do that.
It might help if you showed us a more complete example of what you
want to work. We've seen,
def foo[T : Manifest] = manifest[T]
def ! : Option[T] = ...
val result = foo !! "pigdog"
but that's never going to compile anyway ... is !! meant to be pimped
on to Manifest?
Nah, I was just experimenting with using Manifests to determine dynamic response types.
I'm afraid I have no solution, but I find the following behavior a bit surprising:
scala> def foo[Any: Manifest] = manifest[Any]
foo: [Any](implicit evidence$1: Manifest[Any])Manifest[Any]
scala> foo
res14: Manifest[Nothing] = Nothing
So, I know its dumb to specify the type parameter this way, but why is it ignored?
Similar behavior appears with the following:
scala> def foo[Any](msg: Any): List[Any] = List(msg)
foo: [Any](msg: Any)List[Any]
scala> foo(1)
res13: List[Int] = List(1)
Looks like Any is converted in a type alias rather than the Any type, is this correct?
Dan
AANLkTimGUoqnMfxnQVYdXCUCM0IQEm8WlMEtJO7NFPgd [at] mail [dot] gmail [dot] com" type="cite">
Cheers,
Miles
--
Miles Sabin
tel: +44 7813 944 528
gtalk: miles [at] milessabin [dot] com" rel="nofollow">miles@milessabin.com
skype: milessabin
http://www.chuusai.com/
http://twitter.com/milessabin
Wed, 2010-05-12, 16:47
#19
Re: Default type inferred
On Wed, May 12, 2010 at 5:16 PM, Daniel Degrandi
wrote:
> I'm afraid I have no solution, but I find the following behavior a bit
> surprising:
>
> scala> def foo[Any: Manifest] = manifest[Any]
> foo: [Any](implicit evidence$1: Manifest[Any])Manifest[Any]
>
> [...]
>
> Looks like Any is converted in a type alias rather than the Any type, is
> this correct?
Yes, you are introducing a new type parameter Any with the syntax
def foo[Any] //...
Wed, 2010-05-12, 16:57
#20
Re: Default type inferred
The type parameter is not being ignored. Here:
def foo[Any: Manifest] = manifest[Any]
You get a type parameter called Any and produce it's manifest. Let's rename Any to avoid confusion between it and the type Any:
def foo[T: Manifest] = manifest[T]
Now, when you write just "foo", without anything to determine the type, Scala will just decide Nothing, which is a subtype of everything.
On Wed, May 12, 2010 at 12:16 PM, Daniel Degrandi <Daniel.Degrandi@uni-duesseldorf.de> wrote:
def foo[Any: Manifest] = manifest[Any]
You get a type parameter called Any and produce it's manifest. Let's rename Any to avoid confusion between it and the type Any:
def foo[T: Manifest] = manifest[T]
Now, when you write just "foo", without anything to determine the type, Scala will just decide Nothing, which is a subtype of everything.
On Wed, May 12, 2010 at 12:16 PM, Daniel Degrandi <Daniel.Degrandi@uni-duesseldorf.de> wrote:
Viktor Klang schrieb:
On Wed, May 12, 2010 at 3:51 PM, Miles Sabin <miles@milessabin.com> wrote:
On Wed, May 12, 2010 at 2:43 PM, Viktor Klang <viktor.klang@gmail.com> wrote:
> Yes, this would be totally retarded. I won't do that.
It might help if you showed us a more complete example of what you
want to work. We've seen,
def foo[T : Manifest] = manifest[T]
def ! : Option[T] = ...
val result = foo !! "pigdog"
but that's never going to compile anyway ... is !! meant to be pimped
on to Manifest?
Nah, I was just experimenting with using Manifests to determine dynamic response types.
I'm afraid I have no solution, but I find the following behavior a bit surprising:
scala> def foo[Any: Manifest] = manifest[Any]
foo: [Any](implicit evidence$1: Manifest[Any])Manifest[Any]
scala> foo
res14: Manifest[Nothing] = Nothing
So, I know its dumb to specify the type parameter this way, but why is it ignored?
Similar behavior appears with the following:
scala> def foo[Any](msg: Any): List[Any] = List(msg)
foo: [Any](msg: Any)List[Any]
scala> foo(1)
res13: List[Int] = List(1)
Looks like Any is converted in a type alias rather than the Any type, is this correct?
Dan
Cheers,
Miles
--
Miles Sabin
tel: +44 7813 944 528
gtalk: miles@milessabin.com
skype: milessabin
http://www.chuusai.com/
http://twitter.com/milessabin
Wed, 2010-05-12, 17:07
#21
Re: Default type inferred
On Wed, May 12, 2010 at 5:42 PM, Daniel Sobral <dcsobral@gmail.com> wrote:
The type parameter is not being ignored. Here:
def foo[Any: Manifest] = manifest[Any]
You get a type parameter called Any and produce it's manifest. Let's rename Any to avoid confusion between it and the type Any:
def foo[T: Manifest] = manifest[T]
Now, when you write just "foo", without anything to determine the type, Scala will just decide Nothing, which is a subtype of everything.
The problem here is that I wanted it to infer the supertype of everything :/
On Wed, May 12, 2010 at 12:16 PM, Daniel Degrandi <Daniel.Degrandi@uni-duesseldorf.de> wrote:
Viktor Klang schrieb:
On Wed, May 12, 2010 at 3:51 PM, Miles Sabin <miles@milessabin.com> wrote:
On Wed, May 12, 2010 at 2:43 PM, Viktor Klang <viktor.klang@gmail.com> wrote:
> Yes, this would be totally retarded. I won't do that.
It might help if you showed us a more complete example of what you
want to work. We've seen,
def foo[T : Manifest] = manifest[T]
def ! : Option[T] = ...
val result = foo !! "pigdog"
but that's never going to compile anyway ... is !! meant to be pimped
on to Manifest?
Nah, I was just experimenting with using Manifests to determine dynamic response types.
I'm afraid I have no solution, but I find the following behavior a bit surprising:
scala> def foo[Any: Manifest] = manifest[Any]
foo: [Any](implicit evidence$1: Manifest[Any])Manifest[Any]
scala> foo
res14: Manifest[Nothing] = Nothing
So, I know its dumb to specify the type parameter this way, but why is it ignored?
Similar behavior appears with the following:
scala> def foo[Any](msg: Any): List[Any] = List(msg)
foo: [Any](msg: Any)List[Any]
scala> foo(1)
res13: List[Int] = List(1)
Looks like Any is converted in a type alias rather than the Any type, is this correct?
Dan
Cheers,
Miles
--
Miles Sabin
tel: +44 7813 944 528
gtalk: miles@milessabin.com
skype: milessabin
http://www.chuusai.com/
http://twitter.com/milessabin
Wed, 2010-05-12, 17:17
#22
Re: Default type inferred
On Wed, May 12, 2010 at 05:47:49PM +0200, Viktor Klang wrote:
> The problem here is that I wanted it to infer the supertype of
> everything :/
Someone else already hit on the only way I know around this (which you
can see me using in the repl source.) Did you respond and I missed it?
Does this not solve your issue?
scala> def f[T: Manifest] = if (manifest[T] eq manifest[Nothing]) manifest[Any] else manifest[T]
f: [T](implicit evidence$1: Manifest[T])scala.reflect.Manifest[_ >: T]
scala> val result = f
result: scala.reflect.Manifest[_] = Any
scala> val result = f[String]
result: scala.reflect.Manifest[_ >: String] = java.lang.String
Wed, 2010-05-12, 17:27
#23
Re: Default type inferred
On Wed, May 12, 2010 at 6:01 PM, Paul Phillips <paulp@improving.org> wrote:
On Wed, May 12, 2010 at 05:47:49PM +0200, Viktor Klang wrote:
> The problem here is that I wanted it to infer the supertype of
> everything :/
Someone else already hit on the only way I know around this (which you
can see me using in the repl source.) Did you respond and I missed it?
Does this not solve your issue?
scala> def f[T: Manifest] = if (manifest[T] eq manifest[Nothing]) manifest[Any] else manifest[T]
f: [T](implicit evidence$1: Manifest[T])scala.reflect.Manifest[_ >: T]
scala> val result = f
result: scala.reflect.Manifest[_] = Any
scala> val result = f[String]
result: scala.reflect.Manifest[_ >: String] = java.lang.String
Yeah, thanks for the tip. it feels a bit unorthodox, I'll try to see if there's another way to structure the code.
Ideally I'd like to even not need the Manifest and just have a type hint for the inferencer to consider if it needs some help along the way.
--
Paul Phillips | Christ died for our sins. Dare we make his martyrdom
Caged Spirit | meaningless by not committing them?
Empiricist | -- Jules Feiffer
slap pi uphill! |----------* http://www.improving.org/paulp/ *----------
--
Viktor Klang
| "A complex system that works is invariably
| found to have evolved from a simple system
| that worked." - John Gall
Akka - the Actor Kernel: Akkasource.org
Twttr: twitter.com/viktorklang
Wed, 2010-05-12, 17:27
#24
Re: Default type inferred
On Wed, May 12, 2010 at 06:09:31PM +0200, Viktor Klang wrote:
> Yeah, thanks for the tip. it feels a bit unorthodox
What! Asking the compiler what the type of something is and then
discarding that and going the entire distance of the type lattice to the
absolute polar opposite type and using that instead feels UNORTHODOX?
That's the kind of thing we DO here in the land of STRONG STATIC TYPING.
Maybe you'd be happier in javascript or PHP or something, where there is
no such thing as unorthodox: only orthodoxies you haven't met yet.
Wed, 2010-05-12, 17:37
#25
Re: Default type inferred
Daniel Sobral schrieb:
AANLkTikWJfr0bifkpy0Y0ckqgUbZLDOKy5M2nxyTv-WM [at] mail [dot] gmail [dot] com" type="cite">The type parameter is not being ignored. Here:
def foo[Any: Manifest] = manifest[Any]
You get a type parameter called Any and produce it's manifest.
Yes, I found that surprising that you basically can overwrite existing type names. Isn't it dangerous?
scala> type String = Int
defined type alias String
scala> List[String]("hello")
<console>:7: error: type mismatch;
found : java.lang.String("hello")
required: String
List[String]("hello")
This is surely a dumb thing to do, but using a library of some kind and you overlook a type, this could lead to hard-to-find bugs, or is this paranoia ;-)
Dan
AANLkTikWJfr0bifkpy0Y0ckqgUbZLDOKy5M2nxyTv-WM [at] mail [dot] gmail [dot] com" type="cite">
def foo[T: Manifest] = manifest[T]
Now, when you write just "foo", without anything to determine the type, Scala will just decide Nothing, which is a subtype of everything.
On Wed, May 12, 2010 at 12:16 PM, Daniel Degrandi <Daniel [dot] Degrandi [at] uni-duesseldorf [dot] de" rel="nofollow">Daniel.Degrandi@uni-duesseldorf.de> wrote:
Viktor Klang schrieb:
On Wed, May 12, 2010 at 3:51 PM, Miles Sabin <miles [at] milessabin [dot] com" target="_blank" rel="nofollow">miles@milessabin.com> wrote:
On Wed, May 12, 2010 at 2:43 PM, Viktor Klang <viktor [dot] klang [at] gmail [dot] com" target="_blank" rel="nofollow">viktor.klang@gmail.com> wrote:
> Yes, this would be totally retarded. I won't do that.
It might help if you showed us a more complete example of what you
want to work. We've seen,
def foo[T : Manifest] = manifest[T]
def ! : Option[T] = ...
val result = foo !! "pigdog"
but that's never going to compile anyway ... is !! meant to be pimped
on to Manifest?
Nah, I was just experimenting with using Manifests to determine dynamic response types.
I'm afraid I have no solution, but I find the following behavior a bit surprising:
scala> def foo[Any: Manifest] = manifest[Any]
foo: [Any](implicit evidence$1: Manifest[Any])Manifest[Any]
scala> foo
res14: Manifest[Nothing] = Nothing
So, I know its dumb to specify the type parameter this way, but why is it ignored?
Similar behavior appears with the following:
scala> def foo[Any](msg: Any): List[Any] = List(msg)
foo: [Any](msg: Any)List[Any]
scala> foo(1)
res13: List[Int] = List(1)
Looks like Any is converted in a type alias rather than the Any type, is this correct?
Dan
Cheers,
Miles
--
Miles Sabin
tel: +44 7813 944 528
gtalk: miles [at] milessabin [dot] com" target="_blank" rel="nofollow">miles@milessabin.com
skype: milessabin
http://www.chuusai.com/
http://twitter.com/milessabin
Wed, 2010-05-12, 17:57
#26
performance problem
i want to grab all elements of a map which are instances of T and match
a certain condition. however, i don't want to write a "e.isInstanceOf[T]
&& e.asInstanceOf[T] && e.asInstanceOf[T].isMyConditionTrue"-filter
every time.
this is my current solution:
def filterEntitiesByType[T](c: Class[T], f: T => Boolean = (e: T) =>
true) = {
knownEntities.values.filter((e) => c.isInstance(e) &&
f(e.asInstanceOf[T])).asInstanceOf[Traversable[T]]
}
my profiler says that 30% of the whole time, the cpu is busy in
"isInstance". this is weird but true because a hardcoded "instanceof" in
java aka "isInstanceOf[classOf[XY]]" in scala is afaik a single vm
instruction. Class.isInstance is a native call. it's a lot slower, at
least using java 6. is there a way to cheat around that problem except
creating one filter per class which then does an .isInstanceOf[T]-call
instead of using the class itself?
another question:
why isn't the type for T of the second parameter inferred if i call the
method above like this:
ctx.filterEntitiesByType(classOf[X], e => e == e)
?
i have to write
ctx.filterEntitiesByType[X](classOf[X], e => e == e)
or declare the method using currying:
def filterEntitiesByTypeCurried[T](c: Class[T])(f: T => Boolean = (e:
T) => true) = {
knownEntities.values.filter((e) => c.isInstance(e) &&
f(e.asInstanceOf[T])).asInstanceOf[Traversable[T]]
}
makes no sense to me
Wed, 2010-05-12, 19:07
#27
Re: performance problem
On Wed, May 12, 2010 at 06:42:42PM +0200, HamsterofDeath wrote:
> i want to grab all elements of a map which are instances of T and
> match a certain condition. however, i don't want to write a
> "e.isInstanceOf[T] && e.asInstanceOf[T] &&
> e.asInstanceOf[T].isMyConditionTrue"-filter every time.
map.values collect { case x: Foo if x.isMyConditionTrue => x }
Wed, 2010-05-12, 19:17
#28
Re: Default type inferred
Yeah, I'd like warning whenever a type parameter shadows an existing class, type or another type parameter.
On Wed, May 12, 2010 at 1:24 PM, Daniel Degrandi <Daniel.Degrandi@uni-duesseldorf.de> wrote:
On Wed, May 12, 2010 at 1:24 PM, Daniel Degrandi <Daniel.Degrandi@uni-duesseldorf.de> wrote:
Daniel Sobral schrieb:The type parameter is not being ignored. Here:
def foo[Any: Manifest] = manifest[Any]
You get a type parameter called Any and produce it's manifest.
Yes, I found that surprising that you basically can overwrite existing type names. Isn't it dangerous?
scala> type String = Int
defined type alias String
scala> List[String]("hello")
<console>:7: error: type mismatch;
found : java.lang.String("hello")
required: String
List[String]("hello")
This is surely a dumb thing to do, but using a library of some kind and you overlook a type, this could lead to hard-to-find bugs, or is this paranoia ;-)
Dan
def foo[T: Manifest] = manifest[T]
Now, when you write just "foo", without anything to determine the type, Scala will just decide Nothing, which is a subtype of everything.
On Wed, May 12, 2010 at 12:16 PM, Daniel Degrandi <Daniel.Degrandi@uni-duesseldorf.de> wrote:
Viktor Klang schrieb:
On Wed, May 12, 2010 at 3:51 PM, Miles Sabin <miles@milessabin.com> wrote:
On Wed, May 12, 2010 at 2:43 PM, Viktor Klang <viktor.klang@gmail.com> wrote:
> Yes, this would be totally retarded. I won't do that.
It might help if you showed us a more complete example of what you
want to work. We've seen,
def foo[T : Manifest] = manifest[T]
def ! : Option[T] = ...
val result = foo !! "pigdog"
but that's never going to compile anyway ... is !! meant to be pimped
on to Manifest?
Nah, I was just experimenting with using Manifests to determine dynamic response types.
I'm afraid I have no solution, but I find the following behavior a bit surprising:
scala> def foo[Any: Manifest] = manifest[Any]
foo: [Any](implicit evidence$1: Manifest[Any])Manifest[Any]
scala> foo
res14: Manifest[Nothing] = Nothing
So, I know its dumb to specify the type parameter this way, but why is it ignored?
Similar behavior appears with the following:
scala> def foo[Any](msg: Any): List[Any] = List(msg)
foo: [Any](msg: Any)List[Any]
scala> foo(1)
res13: List[Int] = List(1)
Looks like Any is converted in a type alias rather than the Any type, is this correct?
Dan
Cheers,
Miles
--
Miles Sabin
tel: +44 7813 944 528
gtalk: miles@milessabin.com
skype: milessabin
http://www.chuusai.com/
http://twitter.com/milessabin
Wed, 2010-05-12, 19:27
#29
Re: performance problem
On 10-05-12 10:59 AM, Paul Phillips wrote:
20100512175907 [dot] GA87473 [at] jon [dot] local" type="cite">Ah, but he wants to pass the class that the values must conform to... This may not be any faster than his version, but it's more idiomatic (in 2.8):On Wed, May 12, 2010 at 06:42:42PM +0200, HamsterofDeath wrote:i want to grab all elements of a map which are instances of T and match a certain condition. however, i don't want to write a "e.isInstanceOf[T] && e.asInstanceOf[T] && e.asInstanceOf[T].isMyConditionTrue"-filter every time.map.values collect { case x: Foo if x.isMyConditionTrue => x }
/**-0xe1a
* @tparam T the type that the map values all share; bounded to AnyRef so you can .getClass
* @tparam U the more-specific type of the values you're looking for
*/
def filterByType[T <: AnyRef, U <: T](c: Class[U], in: Seq[T],
f: (T) => Boolean = (e: T) => true) =
in.collect {
case v if f(v) && c.isAssignableFrom(v.getClass) => v.asInstanceOf[U]
}
Wed, 2010-05-12, 19:47
#30
Re: performance problem
"isAssignableFrom" is a native call as well, so it's not faster
what i could do is fromCollection.getClass = givenClass || givenClass.isInstance(fromCollection) which would be a shortcut in 90% of my cases, but that isn't beautiful anymore.
but who cares once it works :D, coding against api > coding against implementation details
Alex Cruise schrieb:
what i could do is fromCollection.getClass = givenClass || givenClass.isInstance(fromCollection) which would be a shortcut in 90% of my cases, but that isn't beautiful anymore.
but who cares once it works :D, coding against api > coding against implementation details
Alex Cruise schrieb:
4BEAF0C2 [dot] 1060707 [at] cluonflux [dot] com" type="cite"> On 10-05-12 10:59 AM, Paul Phillips wrote:20100512175907 [dot] GA87473 [at] jon [dot] local" type="cite">Ah, but he wants to pass the class that the values must conform to... This may not be any faster than his version, but it's more idiomatic (in 2.8):On Wed, May 12, 2010 at 06:42:42PM +0200, HamsterofDeath wrote:i want to grab all elements of a map which are instances of T and match a certain condition. however, i don't want to write a "e.isInstanceOf[T] && e.asInstanceOf[T] && e.asInstanceOf[T].isMyConditionTrue"-filter every time.map.values collect { case x: Foo if x.isMyConditionTrue => x }
/**-0xe1a
* @tparam T the type that the map values all share; bounded to AnyRef so you can .getClass
* @tparam U the more-specific type of the values you're looking for
*/
def filterByType[T <: AnyRef, U <: T](c: Class[U], in: Seq[T],
f: (T) => Boolean = (e: T) => true) =
in.collect {
case v if f(v) && c.isAssignableFrom(v.getClass) => v.asInstanceOf[U]
}
Wed, 2010-05-12, 19:47
#31
Re: performance problem
On Wed, May 12, 2010 at 7:27 PM, HamsterofDeath wrote:
> "isAssignableFrom" is a native call as well, so it's not faster
That is not necessarily true, it depends on the JVM. You should not
assume that something is native code just because it shows up as
native in the JDK's Java source. e.g.:
"There are lots of intrinsic methods. See library_call.cpp and vmSymbols.hpp.
Object.getClass is one or two instructions.
Class.isInstance and Class.isAssignableFrom are as cheap as instanceof
bytecodes when the operands are constants, and otherwise no more
expensive than aastore type checks."
http://wikis.sun.com/display/HotSpotInternals/PerformanceTechniques
Best,
Ismael
Wed, 2010-05-12, 20:07
#32
Re: performance problem
On Wed, May 12, 2010 at 7:43 PM, Ismael Juma wrote:
> That is not necessarily true, it depends on the JVM. You should not
> assume that something is native code just because it shows up as
> native in the JDK's Java source.
I should clarify, I meant that one should not assume something is JNI
(and hence slow) just because it shows as native in the Java source.
Intrinsic methods are usually hand-optimised and are as fast as they
get.
Ismael
def foo[T : Manifest] = implicitly[Manifest[T]].erasure
On 12 May 2010 10:34, Viktor Klang <viktor.klang@gmail.com> wrote:
--
Kevin Wright
mail/google talk: kev.lee.wright@googlemail.com
wave: kev.lee.wright@googlewave.com
skype: kev.lee.wright
twitter: @thecoda