Skip to content

Instantly share code, notes, and snippets.

@bvenners
Created October 23, 2014 20:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bvenners/6e2d9451760b7062fbbc to your computer and use it in GitHub Desktop.
Save bvenners/6e2d9451760b7062fbbc to your computer and use it in GitHub Desktop.
Shorthand form with existential type
// Can also do this:
scala> def addBippy(set: EquaSets[String]#EquaSet): set.path.EquaSet = {
| set.copyInto(set.path) union set.path.EquaSet("bippy")
| }
addBippy: (set: org.scalactic.EquaSets[String]#EquaSet)set.path.EquaSet
scala> val lowered = SortedEquaSets[String](StringNormalizations.lowerCased.toOrderingEquality)
lowered: org.scalactic.SortedEquaSets[String] = org.scalactic.SortedEquaSets@26273e89
// But it results in an existential type
scala> addBippy(lowered.EquaSet("hi", "ho"))
<console>:19: warning: inferred existential type set.path.EquaSet forSome { val set: lowered.EquaSet }, which cannot be expressed by wildcards, should be enabled
by making the implicit value scala.language.existentials visible.
This can be achieved by adding the import clause 'import scala.language.existentials'
or by setting the compiler option -language:existentials.
See the Scala docs for value scala.language.existentials for a discussion
why the feature should be explicitly enabled.
addBippy(lowered.EquaSet("hi", "ho"))
^
res0: set.path.EquaSet forSome { val set: lowered.EquaSet } = EquaSet(hi, ho, bippy)
scala> import scala.language.existentials
import scala.language.existentials
// Can see it better here
scala> addBippy(lowered.EquaSet("hi", "ho"))
res1: set.path.EquaSet forSome { val set: lowered.EquaSet } = EquaSet(hi, ho, bippy)
// Can "widen" if that's the appropriate word the type back to what it was before
scala> val result: lowered.EquaSet = addBippy(lowered.EquaSet("hi", "ho"))
result: lowered.EquaSet = EquaSet(hi, ho, bippy)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment