FAQ Database Discussion Community


Scala Option type upper bound don't understand

scala,optional,type-systems
I'm reading Functional Programming in Scala, and in chapter 04 the authors implement Option on their own. Now, when defining the function getOrElse they use an upper bound to restrict the type of A to a supertype (if a understood correctly) So, the definition goes: sealed trait Option[+A] { def...

How does the Scala type system know that cons + Nil is exhaustive?

scala,type-systems,algebraic-data-types
I just wrote this function, wondering what would happen if I omitted the Nil case, and noticed that scalac gives me a warning: def printList[String](list: List[String]) { list match { case head :: tail => { println(head) printList(tail) } //case Nil => println("Done") } } Warning: match may not be...

Composing type-level functions with implicit witnesses

scala,type-systems,implicits,path-dependent-type,type-level-computation
I am experimenting with some rather complex type-level calculations. There, I have some type tags (say, A, B, and C), and functions working on them, which are represented by implicit witnesses with path-dependent result types: class A class B class C trait F1[T] { type result } trait F2[T] {...

Guessing the correct instance of Num from the context

haskell,typeclass,type-systems
Suppose: import Data.Int (Int64) data Unknown class Meh a where meh :: a -> String instance Meh Int64 where meh = show instance Meh Unknown where meh _ = "Unknown" It is obvious that meh can only take either an Int64 or an Unknown. But we all know that meh...

Why can't I remove the Ord Typeclass from this haskell implementation of Kadane's algorithm?

algorithm,haskell,types,type-inference,type-systems
I have been implementing kadane's algorithm in haskell, AFAIK it is working correctly kadane' :: (Ord a, Num a) => [a] -> a kadane' xs = head$ foldl maxsub [0,0] xs maxsub :: (Ord a, Num a) => [a] -> a -> [a] maxsub x y | last(x)+y > head(x)...