Skip to content
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

[TODO] quoted symbols foo should not be replaced in template substitution #8899

Closed
timotheecour opened this issue Sep 6, 2018 · 9 comments
Closed

Comments

@timotheecour
Copy link
Member

timotheecour commented Sep 6, 2018

to avoid running into #8882 and allow a common (for me at least) idiom, I'd like to suggest that a quoted symbol name should not be replaced in template substitution

type Bar = object
  name: string
proc funAux(name: string)=discard

template fun(name = ""): untyped =
  # this gives errors, see https://github.com/nim-lang/Nim/issues/8882
  funAux(name = name)
  let foo = Bar(name: name)

  # proposal: avoid name substitution when symbol is quoted:
  funAux(`name` = name) # in this example, would substitute as: funAux(name = "")
  var foo = Bar(`name`: name) # in this example, would substitute as: Bar(name: "")
  foo.`name` = "baz"
fun()

The workaround (renaming the template arguments) is not great, eg:

type Bar = object
  name: string
  niter: int

template fun(niter2: int, name2 = ""): untyped =
  # ....
  Bar(niter:niter2, name: name2)
@Araq
Copy link
Member

Araq commented Sep 6, 2018

This keeps coming up but the template substitution mechanism should remain simple, rename your template parameters when you have a conflict like this.

@joshgoebel
Copy link
Contributor

How can you rename them if you purposely want to properly support parameter naming at the point of calling?

For example:

template render*(body: string) :untyped =
  resp.body = body

I want the user to be able to call it like such:

render(body="raw content")

Renaming body isn't a choice as it would break the public API I want to provide.

@joshgoebel
Copy link
Contributor

+1 on the quoting idea. I was already trying it anyways, but of course it doesn't work.

@Araq
Copy link
Member

Araq commented Oct 19, 2018

Then you rename your object field. :P

@dom96
Copy link
Contributor

dom96 commented Oct 19, 2018

I would propose the opposite: only substitute quoted symbols.

In fact, I'm pretty sure I got @Araq to agree to this over PM at one point...

@krux02
Copy link
Contributor

krux02 commented Oct 19, 2018

@yyyc514 @timotheecour

nested templates solve all problems

var resp: tuple[body: string]

template renderInner(bodyArg: string): untyped =
  resp.body = bodyArg

template render*(body: string) :untyped =
  renderInner(body)

render(body = "raw content")

echo resp

@dom96 I would agree on it as an initial design choice, but right now it would just break too much code.

@krux02 krux02 closed this as completed Oct 19, 2018
@dom96
Copy link
Contributor

dom96 commented Oct 19, 2018

Maybe we could use a different symbol for the quoting to have a transition period?

@timotheecour timotheecour changed the title quoted symbols foo should not be replaced in template substitution [TODO] quoted symbols foo should not be replaced in template substitution Oct 19, 2018
@joshgoebel
Copy link
Contributor

Nested templates. Nice thanks. Will work for now.

@Araq
Copy link
Member

Araq commented Oct 22, 2018

Maybe we could use a different symbol for the quoting to have a transition period?

Gah, no, quoting is so ugly, but I think you mean, "quote in dot notation to get the replacement"?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants