- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Casting to anonymous class with a given method
Wed, 2011-12-21, 14:38
Dear all,I have read somewhere on some scala book that it is possible to define generics in an anonymous way.
1) I can't find it .. it looked to me that you can do a generic for example [AnyRef with stop()] , the syntax is wrong, but the idea is that a function receiving this kind of object can call the stop() method on it.
2) Can this be used also to cast objects?
Best Regards
Edmondo
1) I can't find it .. it looked to me that you can do a generic for example [AnyRef with stop()] , the syntax is wrong, but the idea is that a function receiving this kind of object can call the stop() method on it.
2) Can this be used also to cast objects?
Best Regards
Edmondo
Wed, 2011-12-21, 15:01
#2
Re: Casting to anonymous class with a given method
def f(e: {def stop():Unit}) = {
e.stop
}
2011/12/21 Edmondo Porcu <edmondo.porcu@gmail.com>
Dear all,I have read somewhere on some scala book that it is possible to define generics in an anonymous way.
1) I can't find it .. it looked to me that you can do a generic for example [AnyRef with stop()] , the syntax is wrong, but the idea is that a function receiving this kind of object can call the stop() method on it.
2) Can this be used also to cast objects?
Best Regards
Edmondo
Fri, 2011-12-23, 08:11
#3
Re: Casting to anonymous class with a given method
On Wed, Dec 21, 2011 at 8:38 AM, Edmondo Porcu <edmondo.porcu@gmail.com> wrote:
Yes. x.asInstanceOf[{def x: Y}]
Dear all,I have read somewhere on some scala book that it is possible to define generics in an anonymous way.
1) I can't find it .. it looked to me that you can do a generic for example [AnyRef with stop()] , the syntax is wrong, but the idea is that a function receiving this kind of object can call the stop() method on it.
2) Can this be used also to cast objects?
Yes. x.asInstanceOf[{def x: Y}]
Best Regards
Edmondo
Tue, 2011-12-27, 11:31
#4
Re: Casting to anonymous class with a given method
Can one use structural typing with generics?
def method[T<:{def greet():Unit}]: List[T] ?
Best Regards
2011/12/21 Kevin Wright <kev.lee.wright@gmail.com>
def method[T<:{def greet():Unit}]: List[T] ?
Best Regards
2011/12/21 Kevin Wright <kev.lee.wright@gmail.com>
The magic phrase you're after is "structural typing", Google should give you plenty of useful hits.
On 21 December 2011 13:38, Edmondo Porcu <edmondo.porcu@gmail.com> wrote:
Dear all,I have read somewhere on some scala book that it is possible to define generics in an anonymous way.
1) I can't find it .. it looked to me that you can do a generic for example [AnyRef with stop()] , the syntax is wrong, but the idea is that a function receiving this kind of object can call the stop() method on it.
2) Can this be used also to cast objects?
Best Regards
Edmondo
Tue, 2011-12-27, 11:51
#5
Re: Casting to anonymous class with a given method
On Tue, Dec 27, 2011 at 11:22 AM, Edmondo Porcu <edmondo.porcu@gmail.com> wrote:
Can one use structural typing with generics?
def method[T<:{def greet():Unit}]: List[T] ?
What does this mean to you?
Best Regards
2011/12/21 Kevin Wright <kev.lee.wright@gmail.com>
The magic phrase you're after is "structural typing", Google should give you plenty of useful hits.
On 21 December 2011 13:38, Edmondo Porcu <edmondo.porcu@gmail.com> wrote:
Dear all,I have read somewhere on some scala book that it is possible to define generics in an anonymous way.
1) I can't find it .. it looked to me that you can do a generic for example [AnyRef with stop()] , the syntax is wrong, but the idea is that a function receiving this kind of object can call the stop() method on it.
2) Can this be used also to cast objects?
Best Regards
Edmondo
--
Viktor Klang
Akka Tech LeadTypesafe - Enterprise-Grade Scala from the Experts
Twitter: @viktorklang
Tue, 2011-12-27, 12:01
#6
Re: Casting to anonymous class with a given method
short answer: yes.
but your example method doesn't really make sense. what you have written there means:
T must be something that has a greet-method.
now let's say you have some classes, some traits and some types which match here. you pass in one of them as a type parameter. how are you going to figure out which one was passed in? you need to know, because you have to return List[T]
def method:List[{def greet():Unit}] makes more sense
-------- Original-Nachricht --------
> Datum: Tue, 27 Dec 2011 11:22:08 +0100
> Von: Edmondo Porcu
> An: Kevin Wright
> CC: scala-user
> Betreff: Re: [scala-user] Casting to anonymous class with a given method
> Can one use structural typing with generics?
>
> def method[T<:{def greet():Unit}]: List[T] ?
>
> Best Regards
>
> 2011/12/21 Kevin Wright
>
> > The magic phrase you're after is "structural typing", Google should give
> > you plenty of useful hits.
> >
> >
> >
> > On 21 December 2011 13:38, Edmondo Porcu
> wrote:
> >
> >> Dear all,
> >> I have read somewhere on some scala book that it is possible to define
> >> generics in an anonymous way.
> >>
> >> 1) I can't find it .. it looked to me that you can do a generic for
> >> example [AnyRef with stop()] , the syntax is wrong, but the idea is
> that a
> >> function receiving this kind of object can call the stop() method on
> it.
> >>
> >> 2) Can this be used also to cast objects?
> >>
> >> Best Regards
> >>
> >> Edmondo
> >>
> >
> >
> >
> >
On 21 December 2011 13:38, Edmondo Porcu <edmondo.porcu@gmail.com> wrote: