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

Structural types and generics

4 replies
edmondo1984
Joined: 2011-09-14,
User offline. Last seen 28 weeks 3 days ago.

Dear all,
Is it possible to descrive the bound of a generic argument as a mix of bound on other classes plus some view bounds?

F[A<:B with {def func: Int}] is legal?

Thank you all for your precious help
Best regards
Edmondo

Inviato da BlackBerry(R) Wireless Handheld

Peter 2
Joined: 2011-02-16,
User offline. Last seen 42 years 45 weeks ago.
Re: Structural types and generics

Edmondo, you may simply write

trait F[A<: {def func: Int}]

Peter

edmondo1984
Joined: 2011-09-14,
User offline. Last seen 28 weeks 3 days ago.
Re: Re: Structural types and generics
My use case is slightly different,
I want A to be a subclass of B and to have a certain method...
Best Regards
Edmondo

2012/1/19 Sonnenschein <peter.empen@arcor.de>
Edmondo, you may simply write

trait F[A<: {def func: Int}]

Peter

Derek Williams 3
Joined: 2011-08-12,
User offline. Last seen 42 years 45 weeks ago.
Re: Re: Structural types and generics
How about this:
trait B
type C = { def func: Int }
class F[A <: B with C ](a: A) {  def doStuff { println(a.func) }}
val b = new B {  def func: Int = 42}
val f = new F(b)
f.doStuff
--
Derek Williams
Peter 2
Joined: 2011-02-16,
User offline. Last seen 42 years 45 weeks ago.
Re: Structural types and generics

Perfect! And if we wanted to omit type C:

class F[A <: B {def func: Int}]

Peter

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