Sitelet https://github.com/twitter/util/commit/a58db2892d82f68bbaa5b41c1394aca9011fd5ef
Skip to content

Commit a58db28

Browse files
Daniel Schobeljenkins
authored andcommitted
util-app: generic Flag.let
Summary: Problem / Solution Making Flag.let and letClear generic in their return type improves their expressive power. Differential Revision: https://phabricator.twitter.biz/D93951
1 parent a76fe1d commit a58db28

3 files changed

Lines changed: 28 additions & 6 deletions

File tree

‎CHANGES‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ Note that ``PHAB_ID=#`` and ``RB_ID=#`` correspond to associated messages in com
66

77
Next Version
88

9+
API Changes:
10+
11+
* util-app: c.t.app.Flag.let and letClear are now generic in their return type.
12+
``PHAB_ID=D93951``
13+
914
Runtime Behavior Changes:
1015

1116
* util-security: Assert validity of X.509 certificates when read from a file.

‎util-app/src/main/scala/com/twitter/app/Flag.scala‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ class Flag[T: Flaggable] private[app] (
149149
*
150150
* @see [[letClear]]
151151
*/
152-
def let(t: T)(f: => Unit): Unit =
152+
def let[R](t: T)(f: => R): R =
153153
let(Some(t), f)
154154

155155
/**
@@ -159,10 +159,10 @@ class Flag[T: Flaggable] private[app] (
159159
*
160160
* @see [[let]]
161161
*/
162-
def letClear(f: => Unit): Unit =
162+
def letClear[R](f: => R): R =
163163
let(None, f)
164164

165-
private[this] def let(t: Option[T], f: => Unit): Unit = {
165+
private[this] def let[R](t: Option[T], f: => R): R = {
166166
val prev = localValue
167167
setLocalValue(t)
168168
try f

‎util-app/src/test/scala/com/twitter/app/FlagTest.scala‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
package com.twitter.app
22

3-
import org.junit.runner.RunWith
43
import org.scalatest.FunSuite
5-
import org.scalatest.junit.JUnitRunner
64
import scala.collection.mutable.Buffer
75

8-
@RunWith(classOf[JUnitRunner])
96
class FlagTest extends FunSuite {
107

118
class Ctx(failFastUntilParsed: Boolean = false) {
@@ -77,6 +74,15 @@ class FlagTest extends FunSuite {
7774
assert(buf == Seq(1, 2, 3))
7875
}
7976

77+
test("Flag: let return values reflect bindings") {
78+
def current: Boolean = MyGlobalBooleanFlag()
79+
80+
val res1 = MyGlobalBooleanFlag.let(true) { current.toString }
81+
assert(res1 == "true")
82+
val res2 = MyGlobalBooleanFlag.let(false) { current.toString }
83+
assert(res2 == "false")
84+
}
85+
8086
test("Flag: letClear") {
8187
// track the order the blocks execute and that they only execute once
8288
var buf = Buffer[Int]()
@@ -95,6 +101,17 @@ class FlagTest extends FunSuite {
95101
assert(buf == Seq(1, 2, 3))
96102
}
97103

104+
test("Flag: letClear return values reflect bindings") {
105+
def current: Boolean = MyGlobalBooleanFlag()
106+
107+
MyGlobalBooleanFlag.let(true) {
108+
val res1 = current
109+
val res2 = MyGlobalBooleanFlag.letClear { current }
110+
assert(res1)
111+
assert(!res2)
112+
}
113+
}
114+
98115
class Dctx extends Ctx {
99116
val quuxFlag = flag[Int]("quux", "an int")
100117
}

0 commit comments

Comments
 (0)