3

i have this trait

trait Extractor extends Mapping[Node]

what's the simplest way to change it to take a type parameter with a default value

trait Extractor[T] extends Mapping[**T if given else Node**] 

other similar questions involves some irrelevant details about upper and lower type bounds

1
  • @flavian yeah but suppose the types i'm intending to set for the Extractor are not related by any means to class Node , so upper and lower bounds are in applicable in this case , or is there something that fits ? Commented Nov 18, 2013 at 1:51

1 Answer 1

7

I think you want to overload Extractor to be of kind * and of kind * -> *. Overloading is usually best avoided, so instead I'd recommend,

scala> trait Mapping[T]
defined trait Mapping

scala> trait Node
defined trait Node

scala> trait Extractor[T] extends Mapping[T]
defined trait Extractor

scala> type NodeExtractor = Extractor[Node]
defined type alias NodeExtractor

scala> val ne = new NodeExtractor {}
ne: NodeExtractor = $anon$1@1ebdcc9a

If Extractor[Node] is the common case, then you could optimize for that and do something like this,

scala> trait ExtractorT[T] extends Mapping[T]
defined trait ExtractorT

scala> type Extractor = ExtractorT[Node]
defined type alias Extractor

scala> val ne = new Extractor {}
ne: Extractor = $anon$1@3d70fe39
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.