Polymorphic Function Values Example Documentation Suggestion

5 views
Skip to first unread message

Alejandro Wainzinger

unread,
Jan 10, 2016, 4:44:50 AM1/10/16
to Shapeless Dev
Hey.

I was looking at polymorphic function values which demonstrates a `choose` object with an `apply` function.

// NOTE: changed the import from the example
import
shapeless.PolyDefns.~>

// choose is a function from Sets to Options with no type specific cases
object choose extends (Set ~> Option) {
 
def apply[T](s : Set[T]) = s.headOption
}

choose(Set(1, 2, 3))
choose(Set('a', 'b', 'c'))

I noticed that when running the below code that does not extend from the polymorphic (Set ~> Option), that functionality remains the same.

// NOTE: extending leads to loss of type safety or similar functionality is impossible
// * `object choose1 extends (Set[_] => Option[_]) // type safety gone
// * `object choose1 extends (Set[Any] => Option[Any]) // type safety gone
// * `object choose1[T] extends (Set[T] => Option[T]) // syntax does not exist, objects cannot be parametrized
object
choose1 {
 
def apply[T](s: Set[T]): Option[T] = s.headOption
}

choose1(Set(1, 2, 3))
choose1(Set('a', 'b', 'c'))

It wasn't until I got to the next bit that I realized where this latter approach broke down by trying to write the example without Shapeless.

// With Shapeless
def
pairApply(f: Set ~> Option) = (f(Set(1, 2, 3)), f(Set('a', 'b', 'c')))
pairApply
(choose)

// Without Shapeless
// * Set and Option cannot be parametrized on a given type T, because type T must be polymorphic to work on a Set[Int] and Set[Char]
// * The use of Any throws away the type system as a result
// * `choose1.apply` must explicitly be named because `choose1.type` does not match the type of `f`
def pairApply1(f: Set[Any] => Option[Any]) = (f(Set(1, 2, 3)), f(Set('a', 'b', 'c')))
pairApply1
(choose1.apply)

I was wondering, does it make sense to add some sort of attempt like this of seeing what happens when you don't use Shapeless as in the examples, to help see the value of it? It wasn't until I actually tried this out that the polymorphism bit really hit me, but I'm a relatively new Scala developer.

Let me know, thanks!

Miles Sabin

unread,
Jan 10, 2016, 4:58:06 AM1/10/16
to shapel...@googlegroups.com
Hi Alejandro,

This mailing list is pretty much deprecated in favour of the shapeless
gitter channel these days,

https://gitter.im/milessabin/shapeless

You'll find a lot more activity there and it's probably the best place
to discuss this topic :-)

Cheers,


Miles
> --
> You received this message because you are subscribed to the Google Groups
> "Shapeless Dev" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to shapeless-de...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Miles Sabin
tel: +44 7813 944 528
skype: milessabin
gtalk: mi...@milessabin.com
http://milessabin.com/blog
http://twitter.com/milessabin

Miles Sabin

unread,
Jan 10, 2016, 5:05:03 AM1/10/16
to shapel...@googlegroups.com
That said, I think you'll find the answers to most of your questions here,

http://milessabin.com/blog/2012/04/27/shapeless-polymorphic-function-values-1

and here,

http://milessabin.com/blog/2012/05/10/shapeless-polymorphic-function-values-2

Cheers,


Miles

On Sat, Jan 9, 2016 at 4:12 PM, Alejandro Wainzinger
<aikawa...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages