Skip to content

Commit 19d12d9

Browse files
committed
Another test - magnet pattern
1 parent 53298ce commit 19d12d9

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

tests/run/magnet.scala

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import scala.concurrent.Future
2+
object magnets {
3+
4+
enum StatusCode {
5+
case OK, Error
6+
}
7+
8+
trait Marshaller[T] { def marshall(t: T): String }
9+
class HttpResponse
10+
11+
sealed trait CompletionMagnet {
12+
type Result
13+
def apply(): Result
14+
}
15+
16+
object CompletionMagnet {
17+
18+
extend (StatusCode, type T: Marshaller) implements CompletionMagnet {
19+
type Result = String
20+
def apply(): String = implicitly[Marshaller[T]].marshall(this._2)
21+
}
22+
23+
extend Future[HttpResponse] implements CompletionMagnet {
24+
type Result = Int
25+
def apply(): Int = 1
26+
}
27+
28+
extend Future[StatusCode] implements CompletionMagnet {
29+
type Result = Int
30+
def apply(): Int = 2
31+
}
32+
}
33+
34+
implicit object stringMarshaller extends Marshaller[String] {
35+
def marshall(x: String): String = x
36+
}
37+
38+
def complete(magnet: CompletionMagnet): magnet.Result = magnet()
39+
}
40+
41+
object Test extends App {
42+
import magnets._
43+
44+
assert(complete((StatusCode.OK, "hello")) == "hello")
45+
46+
}

0 commit comments

Comments
 (0)