This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Casting to anonymous class with a given method

6 replies
edmondo1984
Joined: 2011-09-14,
User offline. Last seen 28 weeks 4 days ago.
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
Kevin Wright 2
Joined: 2010-05-30,
User offline. Last seen 26 weeks 4 days ago.
Re: Casting to anonymous class with a given method
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



Maxime Lévesque
Joined: 2009-08-18,
User offline. Last seen 42 years 45 weeks ago.
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

Naftoli Gugenheim
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
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:
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

edmondo1984
Joined: 2011-09-14,
User offline. Last seen 28 weeks 4 days ago.
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>
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
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
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
H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.
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
> >>
> >
> >
> >
> >

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland