[Lift] Re: Newbie Scala syntax question re: parameterized types with bounds

2009-04-03 Thread David Pollak
On Fri, Apr 3, 2009 at 1:55 PM, Jorge Ortiz  wrote:

> On Fri, Apr 3, 2009 at 12:53 PM, David Pollak <
> feeder.of.the.be...@gmail.com> wrote:
>
>>
>>
>> On Wed, Apr 1, 2009 at 8:06 PM, Kris Nuttycombe <
>> kris.nuttyco...@gmail.com> 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
>   }
>
> W, 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.
>

We need the type parameters on the Mapped classes to that we get type-safe
query building.


>
>
> --j
>
>
>>>
>>> Kris
>>>
>>> On Fri, Mar 27, 2009 at 5:26 PM, Alex Boisvert 
>>> 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 <
>>> stefanscottal...@gmail.com>
>>> > 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
>>
>>
>>
>
> >
>


-- 
Lift, the simply func

[Lift] Re: Newbie Scala syntax question re: parameterized types with bounds

2009-04-03 Thread Jorge Ortiz
On Fri, Apr 3, 2009 at 12:53 PM, David Pollak  wrote:

>
>
> On Wed, Apr 1, 2009 at 8:06 PM, Kris Nuttycombe  > 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
  }

W, 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 
>> 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 <
>> stefanscottal...@gmail.com>
>> > 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 liftweb@googlegroups.com
To unsubscribe from this group, send email to 
liftweb+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/liftweb?hl=en
-~--~~~~--~~--~--~---



[Lift] Re: Newbie Scala syntax question re: parameterized types with bounds

2009-04-03 Thread David Pollak
On Wed, Apr 1, 2009 at 8:06 PM, Kris Nuttycombe
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 =>
}




>
>
> Kris
>
> On Fri, Mar 27, 2009 at 5:26 PM, Alex Boisvert 
> 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 <
> stefanscottal...@gmail.com>
> > 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 liftweb@googlegroups.com
To unsubscribe from this group, send email to 
liftweb+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/liftweb?hl=en
-~--~~~~--~~--~--~---



[Lift] Re: Newbie Scala syntax question re: parameterized types with bounds

2009-04-01 Thread Kris Nuttycombe

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?

Kris

On Fri, Mar 27, 2009 at 5:26 PM, Alex Boisvert  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 
> 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
>>
>>
>>
>>
>>
>>
>
>
> >
>

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Lift" group.
To post to this group, send email to liftweb@googlegroups.com
To unsubscribe from this group, send email to 
liftweb+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/liftweb?hl=en
-~--~~~~--~~--~--~---



[Lift] Re: Newbie Scala syntax question re: parameterized types with bounds

2009-03-27 Thread Alex Boisvert
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 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
>
>
>
>
>
> >
>

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Lift" group.
To post to this group, send email to liftweb@googlegroups.com
To unsubscribe from this group, send email to 
liftweb+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/liftweb?hl=en
-~--~~~~--~~--~--~---



[Lift] Re: Newbie Scala syntax question re: parameterized types with bounds

2009-03-27 Thread David Pollak
Stefan,

The Mapper[T <: Mapper[T]] type allows for:

User extends Mapper[User]

It means that T is the the class and it is the current subclass of Mapper.

Combined with the {
self: T =>
}

Construct, we know that T is the actual class that the current Mapper is the
subclass of.

And yes, the compiler has a special flag to be able to resolve the type of T
to User during the evaluation of User extends Mapper[User]

Make sense?

Thanks,

David

On Fri, Mar 27, 2009 at 1:52 PM, Stefan Scott 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 liftweb@googlegroups.com
To unsubscribe from this group, send email to 
liftweb+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/liftweb?hl=en
-~--~~~~--~~--~--~---