Hello,
Reading Nim's book I see that != is template, which seems simple, but it is still not clear why it was prefered over func.
The only idea I have is from the not obvious template feature - they are always inline. maybe to reduce amount of func instances from generics, but all the explanations are a bit flimsy.
What is the real reason for it?
I think that inline proc would do exactly the same. I know that inline is not strict.
So, does the inline the answer?
func is also not bad - I mean that its definition sometimes is better to read than template
Well inline proc is simply a hint to the C compiler, which is free to ignore it. So why rely on that when Nim has a perfectly good way of doing it itself?
func has the same issue as proc, it's an extra call, and calls are "expensive".
I think that it is about "the only idea" I wrote in initial message. It is good reason, but not obvious from the book/manual
Another question is untyped
What is the real reason for it?
to show off the language feature ;)
The only question left for me - why params are untyped?
to show off the language feature ;)
IIRC I needed != and an implementation of generics did not exist yet. ;-)
A proc here would (if it wasn't optimized by your C compiler) lead to an extra function call
By this argument, we should turn all reasonably short non-recursive procs into templates.
Composition, composition, composition. Procs are procedures, and templates are compositional primitives.
And as this beautifully laid out proof about pi written in verse also proves is: When you write in verse; you think twice before noting down something ugly. This really applies to Nims minimalism, and also the kinds of programs one can, wants to, or should write. This applies quadrupaly so to threading Dsl's as the space of möglich or plausible parallel programs far, far, exceeds the useful, interesting, or sane ones; Which are best written with program specific constraints, and not as a general abstraction as that widens the applicability to an unwieldly breadth of verse and into crappy, but functional prose.
any problem domain that rely on ... NaN
NaN gates and Flip FLOPS by tom7, paper video abuses ieee754 compliance to its limits
there's no equality comparisons involved, but many of the operations rely on ieee754-compliant min and max, all of which work:
import strformat
func `not`(f:float):float = Inf - max(f,1.0)
func `or`(a,b:float):float = max(a,b)
func `and`(a,b:float):float = a*b
func `xor`(a,b:float):float = abs(min(b,-a) + max(b,-Inf))
func nand(a,b:float):float = Inf - max(a + b, -Inf)
func nor(a,b:float):float = -Inf / max(b, max(a, -1.0))
func toBool(f:float):bool = f == Inf
for (a_float,a_bool) in [(Nan,false),(Inf,true)]:
echo &"not {a_float} == not {a_bool}\t\t\t{(not a_float).toBool == not a_bool}"
for (b_float,b_bool) in [(Nan,false),(Inf,true)]:
echo &"{a_float} or {b_float} == {a_bool} or {b_bool}\t\t{(a_float or b_float).toBool == (a_bool or b_bool)}"
echo &"{a_float} and {b_float} == {a_bool} and {b_bool}\t\t{(a_float and b_float).toBool == (a_bool and b_bool)}"
echo &"{a_float} xor {b_float} == {a_bool} xor {b_bool}\t\t{(a_float xor b_float).toBool == (a_bool xor b_bool)}"
echo &"{a_float} nand {b_float} == not({a_bool} and {b_bool})\t{(a_float.nand b_float).toBool == not(a_bool and b_bool)}"
echo &"{a_float} nor {b_float} == not({a_bool} or {b_bool})\t{(a_float.nor b_float).toBool == not(a_bool or b_bool)}"