I've recently introduced Nim to a number of colleagues (mostly with a C++ and Go background), and I want to share some of the feedback I got.
Surprise: you cannot print the type of an expression
echo 1
echo type(1) # does not compile
This seems easy to fix with the following patch (not sure if that is correct), but I wonder why something like this not enabled by default given that the type() proc is exported from system.nim.
diff --git a/lib/system.nim b/lib/system.nim
index 9b41253..ce41373 100644
--- a/lib/system.nim
+++ b/lib/system.nim
@@ -168,6 +168,8 @@ proc `type`*(x: untyped): typeDesc {.magic: "TypeOf", noSideEffect, compileTime.
## Builtin 'type' operator for accessing the type of an expression.
## Cannot be overloaded.
discard
+from typetraits import nil
+proc `$`*(t: typedesc): string = typetraits.name(t)
proc `not` *(x: bool): bool {.magic: "Not", noSideEffect.}
## Boolean not; returns true iff ``x == false``.
@markus-oberhumer, I know that araq does not like to import modules into system. Is it reasonable to expect import typetraits before $ can work on types? I think so.
See #5839
I looked at the typetraits module. Everything from typetraits is implemented with magics and it only provides four procedures that are defined on typedescriptors. I suggest moving all methods from typetraits to system. typetraits can still hang around as an empty module that reexports the system procedures it had earlier for backward compatibility, but the import should be deprecated. I don't think there will be any mentionable cost to doing that.
I constantly forget the name of the typetraits module whenever I need to print the name of a typedesc.
This seems to be addressed by git commit 9565da11e5014583a9afe0da6c4e2b83980a3e20.
But given that proc type()*: (x: untyped) is in module system I still think that everything from typetraits should be moved to system.
I personally think that the system module is too large currently and that we should be moving things out of it, not into it.
What would be better is making the compiler smart enough to give you suggestions of procedures from other modules that fit your call.
Yes I think that this smartness of the compiler would be very great, but that would be another issue.
And you might be right that there is some unnecessary stuff in the system module that could also be in another module, but to stringify a type is not one of those bloat functions, it just improves accessability.
Things where I think they are not necessary in the system module are more of these things:
cint, cstring, ...
stmt, expr, ... (deprecated)
PFrame, TFrame (debuger api, should be debugger package)
on, off, ... (just there to allow inconsistent boolen names)
internalNew
ze, ze64, ..., (Nim has now proper unsigned integers, you never need this)
+%, *%, ... (Nim has proper unsigned integers, you never need this)
I'd love to get this merged!
What's the status of this one?
I think nothing has changed here, @Araq does not want it in system.
Relevant PR https://github.com/nim-lang/Nim/pull/7100
Most helpful comment
I looked at the typetraits module. Everything from
typetraitsis implemented with magics and it only provides four procedures that are defined on typedescriptors. I suggest moving all methods fromtypetraitstosystem.typetraitscan still hang around as an empty module that reexports the system procedures it had earlier for backward compatibility, but the import should be deprecated. I don't think there will be any mentionable cost to doing that.I constantly forget the name of the typetraits module whenever I need to print the name of a typedesc.