Hi, I'm new to Nim, familiar with C, C++, Python, Java, Lisp, Haskell, Go, shell.
I know your project aspires to have intuitive syntax like Python.
I was looking at some examples of Nim code, and would like to offer some constructive feedback on the procedure/process definitions:
proc factorial(n: int): int =
if n == 0:
return 1
else:
return n * factorial(n - 1)
let result = factorial(5)
echo "Factorial of 5 is ", result
You have a type definition in the parameter field. Reading it, this syntax doesn't immediately work well. Colon is used in most languages for associative data, and in English to mean "pay attention to the following". Since type definitions are more of a hint to humans, the use of parentheses inside the parameter definition would be more intuitive, as type definition is more similar to a parenthetical commentary than "pay attention to the following" which draws attention away from the main point (variable name) and to the auxiliary information (type), while the return data type would be most intuitively expressed using a common notation for 'yields': ->
proc factorial (n (int)) -> int
or even listing the function type abstract after the function parameter section:
proc factorial(n) (int -> int)
I am aware the type is vital information for the compiler, but it is not interesting to the reader, who would infer the type just by reading the code.
Welcome, AMDphreak!
First off, Nim is not a young language; this decision was made ages ago. Second, there's millions of lines of Nim at this point. Your proposals would break all of that to accommodate a fairly superficial change that, as described, takes 2-3 times as many characters to convey the same, unambiguous meaning.
Kotlin is not a small language and defines functions in almost the exact style as Nim. https://kotlinlang.org/docs/functions.html
When you're dealing with toy functions like factorial it might be less noisy for you to see int -> int
As soon as your inputs get more sophisticated, this quickly becomes a nightmare. Here's a signature from the standard library,
proc setCookie(key, value: string; domain = ""; path = ""; expires = "";
noName = false; secure = false; httpOnly = false;
maxAge = none(int); sameSite = SameSite.Default): string
Would you really prefer,
proc setCookie(key, value; domain = ""; path = ""; expires = "";
noName = false; secure = false; httpOnly = false;
maxAge = none(); sameSite = SameSite.Default) (string, string, string, string, string, boolean, boolean, boolean, Option[int], SameSite -> string)
I would not.
Since type definitions are more of a hint to humans, the use of parentheses inside the parameter definition would be more intuitive
nim is statically typed language and types are required by compiler it is not a hint for human
If you like this syntax, you can use the sugar library to simulate it.
import sugar
let f = (x: int) -> int => x * x
let hF = (x: int, y: (int) -> int) =>
y(x) + 1
proc hFunc(x: int, y: (int) -> int): int =
return y(x) + 1
echo f(1000)
echo hF(1000, f)
echo hF(1000, (x: int) => (
var xx = 1
xx = x * x
return xx * x
))
echo hF(1000, (x: int) => hF(100, f) * x)
echo hFunc(1000, (x: int) => hF(100, f) * x)