Created
October 23, 2014 20:11
-
-
Save bvenners/6e2d9451760b7062fbbc to your computer and use it in GitHub Desktop.
Shorthand form with existential type
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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