Please correct me if I'm wrong, but my understanding is that typed as return type of a template or proc just means void ; actually I couldn't find that in the docs (it only mentions "Typed vs untyped parameters", see https://nim-lang.org/docs/manual.html#templates-typed-vs-untyped-parameters)
Given that typed for parameters means something completely different from typed for output type, I find it un-necessarily confusing, and we should prefer using instead void or nothing, see below:
# don't use that, `typed` as output means something very different from `typed` as parameter
template foo(): typed = discard
# instead use one of these which are synonyms and more readable
template foo(): void = discard
template foo() = discard
Given that, I recommend deprecating typed as output, or at least indicate that in styleguide (cf https://github.com/nim-lang/Nim/pull/8271)
Templates don't always have void return types
I never implied that.
What I said is typed as return type implies void. So it's better to use void as typed is a misnomer in this context.
So I have come to a conclusion that template return types should be untyped when in doubt
The decision should'nt be about whether it's void or not-void (that's what auto is for); it should be about when we want type checking/type resolution done.
What I said is typed as return type implies void. So it's better to use void as typed is a misnomer in this context.
No, we don't know what typed as a return type means, the spec is not clear and the implementation is worse. It doesn't mean "void".
What I said is typed as return type implies void.
I don't have enough knowledge to agree to or contest that.
And thanks for your compliments :) That's a living document, which I update about every 1 or 2 days.
test.nim
template foo_T1():int=10
template foo_T2():any=10
template foo_T3():auto=10
template foo_T4():auto=discard
template foo_T5():void=discard
template foo_typed1():typed=10
template foo_typed2():typed=discard
template foo_untyped1():untyped=10
template foo_untyped2():untyped=discard
echo foo_T1()
echo foo_T2()
echo foo_T3()
foo_T4()
foo_T5()
# echo foo_typed1() # Error: expression '10' is of type 'int literal(10)' and has to be discarded
foo_typed2()
echo foo_untyped1()
foo_untyped2()
every example in nim codebase that uses typed effectively returns void
Yeah, I observed that too, but I think that's just "use case". If we want make it clear, we should put "usually" in that which means it can be used for other "use case" (idk tho) .
What are the chances of keeping typed and untyped and making it actually meaningful? Like
template a: untyped =
# This actually has no type
for i in 0..9:
echo i
template b: typed =
# This has a type
procReturningSomething()
or
typed/untyped for template parameters are much more clear on their intention.
I meant return value type, not parameter type. And as for the former, the meaning of typed/untyped seems to be:
That's why I wonder what idea was behind the re-naming.
What are the chances of keeping typed and untyped and making it actually meaningful? Like
template a: untyped =
# This actually has no type
for i in 0..9:
echo i
template b: typed =
# This has a type
procReturningSomething()
no, untyped can be used to (when instantiated), return either void or non-void, so that suggestion would not work.
Eg:
template foo_typed5(foo: static bool):untyped=
when foo: 1
else: echo2 2
no, untyped can be used to (when instantiated), return either void or non-void
Correct, so it should be
typed is after type/symbol resolution and untyped is before.
Which is different from "has a certain type / is in a certain type class". Existing type(class)es should not replace typed as a return type, they should narrow down its meaning.
@timothee
auto when void or some type, else:
any when some type, else:
That would require changes to the type system, because ATM void is any is true.