|
Scala example source code file (Variances.scala)
The Scala Variances.scala source code/* NSC -- new scala compiler
* Copyright 2005-2011 LAMP/EPFL
* @author Martin Odersky
*/
package scala.tools.nsc
package typechecker
import symtab.Flags.{ VarianceFlags => VARIANCES, _ }
/** Variances form a lattice, 0 <= COVARIANT <= Variances, 0 <= CONTRAVARIANT <= VARIANCES
*/
trait Variances {
val global: Global
import global._
/** Flip between covariant and contravariant */
private def flip(v: Int): Int = {
if (v == COVARIANT) CONTRAVARIANT
else if (v == CONTRAVARIANT) COVARIANT
else v
}
/** Map everything below VARIANCES to 0 */
private def cut(v: Int): Int =
if (v == VARIANCES) v else 0
/** Compute variance of type parameter `tparam' in types of all symbols `sym'. */
def varianceInSyms(syms: List[Symbol])(tparam: Symbol): Int =
(VARIANCES /: syms) ((v, sym) => v & varianceInSym(sym)(tparam))
/** Compute variance of type parameter `tparam' in type of symbol `sym'. */
def varianceInSym(sym: Symbol)(tparam: Symbol): Int =
if (sym.isAliasType) cut(varianceInType(sym.info)(tparam))
else varianceInType(sym.info)(tparam)
/** Compute variance of type parameter `tparam' in all types `tps'. */
def varianceInTypes(tps: List[Type])(tparam: Symbol): Int =
(VARIANCES /: tps) ((v, tp) => v & varianceInType(tp)(tparam))
/** Compute variance of type parameter `tparam' in all type arguments
* <code>tps which correspond to formal type parameters `tparams1'.
*/
def varianceInArgs(tps: List[Type], tparams1: List[Symbol])(tparam: Symbol): Int = {
var v: Int = VARIANCES;
for ((tp, tparam1) <- tps zip tparams1) {
val v1 = varianceInType(tp)(tparam)
v = v & (if (tparam1.isCovariant) v1
else if (tparam1.isContravariant) flip(v1)
else cut(v1))
}
v
}
/** Compute variance of type parameter `tparam' in all type annotations `annots'. */
def varianceInAttribs(annots: List[AnnotationInfo])(tparam: Symbol): Int = {
(VARIANCES /: annots) ((v, annot) => v & varianceInAttrib(annot)(tparam))
}
/** Compute variance of type parameter `tparam' in type annotation `annot'. */
def varianceInAttrib(annot: AnnotationInfo)(tparam: Symbol): Int = {
varianceInType(annot.atp)(tparam)
}
/** Compute variance of type parameter <code>tparam in type
Other Scala examples (source code examples)Here is a short list of links related to this Scala Variances.scala source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.