-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Improve type inference for literal named tuples #20497
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import language.experimental.namedTuples | ||
import NamedTuple.* | ||
|
||
@FailsWith[HttpError] | ||
trait GreetService derives HttpService: | ||
@HttpInfo("GET", "/greet/{name}") | ||
def greet(@HttpPath name: String): String | ||
@HttpInfo("POST", "/greet/{name}") | ||
def setGreeting(@HttpPath name: String, @HttpBody greeting: String): Unit | ||
|
||
@main def Test = | ||
|
||
val e = HttpService.endpoints[GreetService] | ||
|
||
println(e.greet.describe) | ||
println(e.setGreeting.describe) | ||
|
||
// Type-safe server logic, driven by the ops-mirror, | ||
// requires named tuple with same labels in the same order, | ||
// and function that matches the required signature. | ||
val logic = e.serverLogic: | ||
( | ||
greet = (name) => Right("Hello, " + name), | ||
setGreeting = (name, greeting) => Right(()) | ||
) | ||
|
||
val server = ServerBuilder() | ||
.handleAll(logic) | ||
.create(port = 8080) | ||
|
||
sys.addShutdownHook(server.close()) | ||
|
||
end Test | ||
|
||
// IMPLEMENTATION DETAILS FOLLOW | ||
|
||
/** Assume existence of macro to generate this */ | ||
given (OpsMirror.Of[GreetService] { | ||
type MirroredType = GreetService | ||
type OperationLabels = ("greet", "setGreeting") | ||
type Operations = ( | ||
OpsMirror.Operation { type InputTypes = (String *: EmptyTuple); type OutputType = String; type ErrorType = HttpError }, | ||
OpsMirror.Operation { type InputTypes = (String *: String *: EmptyTuple); type OutputType = Unit; type ErrorType = HttpError } | ||
) | ||
}) = new OpsMirror: | ||
type MirroredType = GreetService | ||
type OperationLabels = ("greet", "setGreeting") | ||
type Operations = ( | ||
OpsMirror.Operation { type InputTypes = (String *: EmptyTuple); type OutputType = String; type ErrorType = HttpError }, | ||
OpsMirror.Operation { type InputTypes = (String *: String *: EmptyTuple); type OutputType = Unit; type ErrorType = HttpError } | ||
) | ||
|
||
object OpsMirror: | ||
type Of[T] = OpsMirror { type MirroredType = T } | ||
|
||
type Operation_I[I <: Tuple] = Operation { type InputTypes = I } | ||
type Operation_O[O] = Operation { type OutputType = O } | ||
type Operation_E[E] = Operation { type ErrorType = E } | ||
|
||
trait Operation: | ||
type InputTypes <: Tuple | ||
type OutputType | ||
type ErrorType | ||
|
||
trait OpsMirror: | ||
type MirroredType | ||
type OperationLabels <: Tuple | ||
type Operations <: Tuple | ||
|
||
trait HttpService[T]: | ||
def route(str: String): Route | ||
trait Route | ||
|
||
type Func[I <: Tuple, O, E] = I match | ||
case EmptyTuple => Either[E, O] | ||
case t *: EmptyTuple => t => Either[E, O] | ||
case t *: u *: EmptyTuple => (t, u) => Either[E, O] | ||
|
||
type ToFunc[T] = T match | ||
case HttpService.Endpoint[i, o, e] => Func[i, o, e] | ||
|
||
final class FailsWith[E] extends scala.annotation.Annotation | ||
final class HttpInfo(method: String, route: String) extends scala.annotation.Annotation | ||
final class HttpBody() extends scala.annotation.Annotation | ||
final class HttpPath() extends scala.annotation.Annotation | ||
|
||
sealed trait HttpError | ||
|
||
object HttpService: | ||
opaque type Endpoint[I <: Tuple, O, E] = Route | ||
|
||
extension [I <: Tuple, O, E](e: Endpoint[I, O, E]) | ||
def describe: String = ??? // some thing that looks inside the Route to debug it | ||
|
||
type ToEndpoints[Ops <: Tuple] <: Tuple = Ops match | ||
case EmptyTuple => EmptyTuple | ||
case op *: ops => (op, op, op) match | ||
case (OpsMirror.Operation_I[i]) *: (OpsMirror.Operation_O[o]) *: (OpsMirror.Operation_E[e]) *: _ => | ||
Endpoint[i, o, e] *: ToEndpoints[ops] | ||
|
||
trait Handler | ||
|
||
class Endpoints[T](val model: HttpService[T]) extends Selectable: | ||
type Fields <: AnyNamedTuple | ||
def selectDynamic(name: String): Route = model.route(name) | ||
|
||
def serverLogic(funcs: NamedTuple[Names[Fields], Tuple.Map[DropNames[Fields], ToFunc]]): List[Handler] = ??? | ||
|
||
def derived[T](using OpsMirror.Of[T]): HttpService[T] = ??? // inline method to create routes | ||
|
||
def endpoints[T](using model: HttpService[T], m: OpsMirror.Of[T]): Endpoints[T] { | ||
type Fields = NamedTuple[m.OperationLabels, ToEndpoints[m.Operations]] | ||
} = | ||
new Endpoints(model) { type Fields = NamedTuple[m.OperationLabels, ToEndpoints[m.Operations]] } | ||
|
||
class ServerBuilder(): | ||
def handleAll(hs: List[HttpService.Handler]): this.type = this | ||
def create(port: Int): Server = Server() | ||
|
||
class Server(): | ||
def close(): Unit = () |
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.