Subtype polymorphism in scala -


what difference between following 2 declarations of sum functions?

 def sum[a](xs:list[a]) = .....   def sum[a <: list[a]](xs:a) = .... 

edit: elaborate more....

lets want write method head on list.

i can write head follow:

 def head[a](xs:list[a]) = xs(0) 

or else can write

 def head[a <: list[a]] (xs:a) = xs(0) 

which 1 prefer , when?

note: 1 difference figured out implicit conversions not applied second one

the first 1 takes list[a] parameter, second 1 a subtype of list[a]. consider following example:

scala> trait foo[a <: foo[a]] defined trait foo  scala> class bar extends foo[bar] defined class bar 

the recursive type parameter on foo can useful, when want define method in trait takes or returns type of subclass:

trait foo[a <: foo[a]] {   def f: }  class bar extends foo[bar] {   def f: bar = new bar }  class baz extends foo[baz] {   def f: baz = new baz } 

i don't know how specific example should work, because don't know how sum on type.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -