-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[meta issue] make all Nim APIs generic: ctor, init, to, streams, types, etc #41
Comments
yes this is the thing that surprised me the most when using the json module. I thought it was a joke. |
I prefer a langauge with a low barrier to entry though. A standard library does not have to be a strange beast that uses every feature the language offers. |
For json just use proc toJsonNode[T](val: T): JsonNode = %val I wouldn't know about how necessary rewriting the newLit procs is, but if you want a nnk -> type transform you can just do proc litNodeFromType(T: typedesc): NimNodeKind {.compiletime.} =
var default: T
let node = newLit(default)
return node.kind
static:
echo litNodeFromType(int) And you could rewrite them anyway with the language as is const litNodeToType: array[nnkCharLit..nnkStrLit, typedesc] = [
char, int, int8, int16, int32, int64, uint, uint8,
uint16, uint32, uint64, float, float32, float64,
(when compiles(float128): float128 else: BiggestFloat), string
]
proc litNodeFromType(T: typedesc): NimNodeKind {.compiletime.} =
let i = litNodeToType.find(T)
if i != -1:
result = litNodeToType[low(litNodeToType) + i]
proc newLit[T](i: T): NimNode {.compiletime.} =
result = newNimNode(litNodeFromType(T))
result.val = i
static:
echo repr(newLit(3))
echo repr(newLit('a')) |
Low barrier of entry is good thing. However:
Here's some concrete data points I hope you'll appreciate:
Certain features should definitely only be used sparingly, eg when alternatives are worse, eg from worst to mildest:
IMO whenever there's code duplication that just depends on type, it's a good candidate for generics |
You're applying the results of a D survey to Nim, perhaps we should look at the results of the Nim survey? If this is a pain point in Nim then we should see plenty of mentions of "generic" in there, but the only ones I could find are to do with stability, i.e. "please stabilise generics and concepts!". There is one comment which states "My only negative observation is that Nim's generics are not as good as D's." but that's it. Perhaps we can interpret that as meaning "Nim should offer more generic APIs in the stdlib" but I'm not so certain. Happy to grep for anything else. |
If you use a voting system one day, think about the "randomized Condorcet voting" |
As I wrote here, I propose:
|
I'd really like to see this be adopted to the entire stdlib for Nim v2. It makes it easier to write macros and generics, and it looks much cleaner. The last part is quite subjective of course but I find EDIT: By the way I believe we should do this with |
All these will simplify some API's by making them generic with less duplication:
read
proc to streams Nim#7481 readBool => read[bool]newLit
, etc)NOTE about
generic
meaningNim generics can be overloaded (eg:
proc foo[T:int|bool]()
or even withproc foo[T:isConceptBar]()
unlike java generics, so the term can be confusing for some and indeed it tripped a few people recently (eg https://github.com/nim-lang/Nim/issues/7430#issuecomment-377473190 , nim-lang/Nim#3502 (comment) ), maybe Nim docs could clarify this aspect. The point is we want to have generic (in Nim's sense of overloadable generics) APIs as much as possible, as legacy API's likeparseBool
make generic code hard to use (API writer needs special case for each type eg bool), reminiscent of C and go.tell-tell sign that generic api's are needed:
lots of very similar code blocks repeated for several distinct types
eg: in macros.nim:
could become simply:
motivation
non-generic API's (eg json newJBool) is viral: it causes calling functions to also be non-generic, or pushes the complexity on them to wrap them into a generic API
how to rewrite API's (typedesc vs generics) ?
https://github.com/nim-lang/Nim/issues/7517 [RFC] guidelines for when to use typedesc vs generics
The text was updated successfully, but these errors were encountered: