Skip to content
Snippets Groups Projects
Commit e180d028 authored by Manos Koukoutos's avatar Manos Koukoutos
Browse files

Comments

parent ee1361f8
No related branches found
No related tags found
No related merge requests found
...@@ -328,9 +328,13 @@ object Expressions { ...@@ -328,9 +328,13 @@ object Expressions {
lazy val optionType = unapplyFun.returnType.asInstanceOf[AbstractClassType] lazy val optionType = unapplyFun.returnType.asInstanceOf[AbstractClassType]
lazy val Seq(noneType, someType) = optionType.knownCCDescendants.sortBy(_.fields.size) lazy val Seq(noneType, someType) = optionType.knownCCDescendants.sortBy(_.fields.size)
lazy val someValue = someType.classDef.fields.head lazy val someValue = someType.classDef.fields.head
// Pattern match unapply(scrut)
// In case of None, return noneCase. /** Construct a pattern matching against unapply(scrut) (as an if-expression)
// In case of Some(v), return someCase(v). *
* @param scrut The scrutinee of the pattern matching
* @param noneCase The expression that will happen if unapply(scrut) is None
* @param someCase How unapply(scrut).get will be handled in case it exists
*/
def patternMatch(scrut: Expr, noneCase: Expr, someCase: Expr => Expr): Expr = { def patternMatch(scrut: Expr, noneCase: Expr, someCase: Expr => Expr): Expr = {
// We use this hand-coded if-then-else because we don't want to generate // We use this hand-coded if-then-else because we don't want to generate
// match exhaustiveness checks in the program // match exhaustiveness checks in the program
...@@ -345,14 +349,17 @@ object Expressions { ...@@ -345,14 +349,17 @@ object Expressions {
) )
) )
} }
// Inlined .get method
/** Inlined .get method */
def get(scrut: Expr) = patternMatch( def get(scrut: Expr) = patternMatch(
scrut, scrut,
Error(optionType.tps.head, "None.get"), Error(optionType.tps.head, "None.get"),
e => e e => e
) )
// Selects Some.v field without type-checking.
// Use in a context where scrut.isDefined returns true. /** Selects Some.v field without type-checking.
* Use in a context where scrut.isDefined returns true.
*/
def getUnsafe(scrut: Expr) = CaseClassSelector( def getUnsafe(scrut: Expr) = CaseClassSelector(
someType, someType,
FunctionInvocation(unapplyFun, Seq(scrut)), FunctionInvocation(unapplyFun, Seq(scrut)),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment