-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
added operateOn to sugar.nim to give Nim the chaining mechanism it de… #13092
Conversation
I probably got it completely wrong but it's to start a discussion. |
Is there an example on how to use it? |
var x = "yay"
operateOn x:
add "abc"
add "efg"
doAssert x == "yayabcefg"
var a = 44
operateOn a:
+= 4
-= 5
doAssert a == 43 |
What about |
or just |
|
|
|
I found this proposal on the forum thanks to Araq. What is the reasoning behind this sugar syntax ? In a small web framework I am working on, I wanted to chain procedures to define callbacks for multiple HTTP methods on the same URL. Example: import phoon
var app = App()
app.route("/hacks")
.get(
proc (ctx: Context) {.async.} =
ctx.response.body("I handle GET requests")
)
.patch(
proc (ctx: Context) {.async.} =
ctx.response.body("I handle PATCH requests")
)
.delete(
proc (ctx: Context) {.async.} =
ctx.response.body("I handle DELETE requests")
) And wit the sugar syntax, it would become: import phoon
import sugar
var app = App()
var route = app.route("/hacks")
chainOn route:
get(
proc (ctx: Context) {.async.} =
ctx.response.body("I handle GET requests")
)
.patch(
proc (ctx: Context) {.async.} =
ctx.response.body("I handle PATCH requests")
)
.delete(
proc (ctx: Context) {.async.} =
ctx.response.body("I handle DELETE requests")
) I am genuinely wondering if this sugar is worth it, but I would be glad to be proven wrong :) |
builder pattern is an awful hack that compensates lack of such sugar |
Your example is using returns (builder pattern) to return the same router over and over again. Many of us think this is not that good because it confuses returns which should return useful info with making the syntax look nice. chainOn trying to fix this example: var app = App()
var route = app.route("/hacks")
route.get(
proc (ctx: Context) {.async.} =
ctx.response.body("I handle GET requests")
)
route.patch(
proc (ctx: Context) {.async.} =
ctx.response.body("I handle PATCH requests")
)
route.delete(
proc (ctx: Context) {.async.} =
ctx.response.body("I handle DELETE requests")
) |
...and it could be written as: var route = app.route("/hacks")
operateOn(route):
get:
proc (ctx: Context) {.async.} =
ctx.response.body("I handle GET requests")
patch:
proc (ctx: Context) {.async.} =
ctx.response.body("I handle PATCH requests")
delete:
proc (ctx: Context) {.async.} =
ctx.response.body("I handle DELETE requests" Please ship it :D |
How about turning assignments to fields? There is also https://github.com/citycide/cascade which is more advanced but slightly different (it returns a type). Maybe include that macro to the stdlib? cc @citycide |
This does work similarly to import cascade
var app = App()
var route = cascade app.route("/hacks"):
get(
proc (ctx: Context) {.async.} =
ctx.response.body("I handle GET requests")
)
patch(
proc (ctx: Context) {.async.} =
ctx.response.body("I handle PATCH requests")
)
delete(
proc (ctx: Context) {.async.} =
ctx.response.body("I handle DELETE requests")
) Though like you said, |
…serves