There should be no need for such a template, this must be a regression.
Since [this PR](https://github.com/nim-lang/Nim/pull/6695/files) blocks should
be an expression (see [corresponding
issue](https://github.com/nim-lang/Nim/issues/3827)). There is also a test
case. However the test case
@Udiknedormin Thanks, you code works! Actually I was using this:
template scope(body : typed) : auto = (block: body)
but I guess ther is something I miss about the semantics of typed because this
code
template scope(body : typed) : auto = (block: body)
let
Well... the template is the right thing to do but without closure magic ---
just use a block:
template scope(code): auto =
block:
code
let a = scope:
echo "hello"
1
echo a
@Hlaaftana Yes, I'd just like to omit parenthesis without losing the scope
semantics
Moral of the story, this works:
let a = (block:
echo "hello"
1)
Here's a related thread:
[https://forum.nim-lang.org/t/2201](https://forum.nim-lang.org/t/2201)
I also noticed another problem
let a = (proc() : auto =
let b = 4
6
)()
echo b
this clearly does not compiles since d is accessed from an outer scope. This
one should be the equivalent written with templates:
template bxpr(body : typed)
I'll answer my own question.. It seems that I just forgot to specify the
template return type:
template scope(body : typed) : auto = (proc() : auto = body) ()
let a = scope:
echo "hello"
1
this works fine. There are some things that are not very clear
It would be really nice to be able to write something like this
let a = block:
echo "hello"
1
but this does not compile because block is a statement
a trivial workaround is
let a = (proc() : auto =
echo "hello"
1
)()
I've reported the issue
[https://github.com/nim-lang/Nim/issues/5630](https://github.com/nim-lang/Nim/issues/5630)
that is correct. yet
template foo(p: untyped): auto = (block:
1
)
let x = foo:
discard
won't compile, while this will
let x = (block:
1
)
in fact, this compiles as well:
template foo(p: untyped): auto
>From example, if you think that's returning something it's indeed returning
>exactly `not (a == b)` but not returning a `bool` value. It's only be `bool`
>after evaluated in runtime.
> Is it possible to return value from template which the last of argument is
> untyped?
pretty sure you can:
[https://nim-lang.org/docs/manual.html#templates](https://nim-lang.org/docs/manual.html#templates)
Is it possible to return value from template which the last of argument is
`untyped`?
If possible, maybe some example would be nice
I used template with last argument `untyped` for executing that argument (which
consists of statements) but never use so that it would return something.
Maybe you should use a proc instead.
By passing nil (ok, by setting the default to nil), you can get foo3() behaving
as follows
template foo(_: untyped): auto =
echo "foo"
1
template foo2(_: varargs[untyped]): auto =
echo "foo2"
block:
discard
2
template foo3(_:
Hmm, that's true. I don't know the reason but judging from error, it's said
wrong number of argument so need to change to `varargs` to accommodate zero
arity.
If it's like below, is it ok?
template foo(_: untyped): auto =
echo "foo"
1
template foo2(_:
if I understand correcly what you mean I'd expect it to compile with:
template foo(p: untyped): auto = (block:
1
)
but it won't
I think if you substitutes the template directly into the assignment, it would
be assigning a variable from multiple statements.
Hi, i'm trying to assign the result of a template, but apparently the compiler
gets confused unless I surround the template with (block: ...)
template foo(p: untyped): auto =
1
let x = (block:
foo:
discard)
let y =
20 matches
Mail list logo