On Fri, Apr 3, 2009 at 12:53 PM, David Pollak <[email protected] > wrote:
> > > On Wed, Apr 1, 2009 at 8:06 PM, Kris Nuttycombe <[email protected] > > wrote: > >> >> Something that occurred to me recently along these lines - perhaps >> someone can disabuse me of this notion. In Java, such recursive types >> are necessary because you don't have abstract types. To refer to the >> implementation type in the declaring class you have to use the >> self-type. >> >> But in Scala, what application would not be satisfied by: >> >> trait Mapper { >> type T <: Mapper >> } >> >> class User extends Mapper { >> type T = User >> } >> >> Is it just that the restriction on T is not sufficiently narrow? > > > No... becayse you can say: > > class User extends Mapper { > type T = Address > } > > I think this captures things, but I'm not 100% sure (and self-types weren't > around when I did Mapper): > > trait Mapper[T <: Mapper] { > self: T => > } > This can be expressed as: trait Mapper { type T >: this.type <: Mapper } class User extends Mapper { type T = User } Actually, this might be sufficient: trait Mapper { type T = this.type } class User extends Mapper Actually, that's too specific, because then: object MetaUser extends User { def create: T = new User // oops, MetaUser.T == MetaUser.type != User } But I think the first suggestion still works: trait Mapper { type T >: this.type <: Mapper } class User extends Mapper { type T = User } object MetaUser extends User { def create: T = new User } Weeee, types are fun... It might be a good idea to user these for Mapper* stuff, but Mapped* stuff would maybe get too verbose with type T = ... syntax versus [T] syntax. --j >> >> Kris >> >> On Fri, Mar 27, 2009 at 5:26 PM, Alex Boisvert <[email protected]> >> wrote: >> > Or said another way, >> > >> > MappedTextarea[ T <: Mapper[T] ] >> > >> > declares a type parameter T and fixes the upper bound of the >> MappedTextarea >> > type parameter to Mapper[T], which means that the type passed to >> > MappedTextArea must be a subtype of Mapper. >> > >> > I, too, found this notation confusing at first and wished I could write >> > MappedTextarea[ <: Mapper[T] ] directly but declaring the T before its >> use >> > is necessary to disambiguate it from existing class names. >> > >> > alex >> > >> > >> > >> > On Fri, Mar 27, 2009 at 1:52 PM, Stefan Scott < >> [email protected]> >> > wrote: >> >> >> >> Hi - >> >> >> >> Sorry to be asking a Scala syntax question here in the Lift group, but >> >> I figured somebody here would know, since this Scala syntax occurs >> >> quite a bit in the Lift source code. >> >> >> >> When reading some of the Lift source I come across a particular Scala >> >> "idiom" involving parameterized types with bounds, whose semantics I'm >> >> unsure of. >> >> >> >> For example: >> >> >> >> class MappedTextarea[ T <: Mapper[ T ] ] ( owner : T, maxLen: Int ) >> >> extends >> >> MappedString[ T ]( owner, maxLen ) { ... } >> >> >> >> What I'm unsure about here is the part where it says: >> >> >> >> T <: Mapper[ T ] >> >> >> >> At first, this made no sense to me - how could a type T be a subtype >> >> of type Mapper[ T ] ? >> >> >> >> Then I guessed that maybe the two occurrences of T are unrelated to >> >> each other - ie, class MappedTextarea is parameterized over a type T, >> >> which must be a subtype of a type Mapper[ T ] -- where the second T is >> >> actually in a separate scope so that it has nothing to do with the >> >> first T. >> >> >> >> Is that what this really means? >> >> >> >> And, if that's really the case, then I guess the other occurrences of >> >> T later in the text: >> >> >> >> owner : T >> >> MappedString[ T ] >> >> >> >> are also referring to the first occurrence of T -- the type T which is >> >> upper-bounded by type Mapper[ T ]. >> >> >> >> Finally, does this mean that the above code could also have been >> >> written equivalently as follows: >> >> >> >> class MappedTextarea[ T <: Mapper[ U ] ] ( owner : T, maxLen: Int ) >> >> extends >> >> MappedString[ T ]( owner, maxLen ) { ... } >> >> >> >> using U instead of T for the type parameter that's in a separate >> >> scope? >> >> >> >> Thanks for any help. >> >> >> >> - Stefan Scott >> >> >> >> >> >> >> >> >> >> >> >> >> > >> > >> > > >> > >> >> >> > > > -- > Lift, the simply functional web framework http://liftweb.net > Beginning Scala http://www.apress.com/book/view/1430219890 > Follow me: http://twitter.com/dpp > Git some: http://github.com/dpp > > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Lift" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/liftweb?hl=en -~----------~----~----~----~------~----~------~--~---
