-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
[json] add asJson as alias for %*
#10011
Conversation
Is this let config = toJson({
"id": 5000,
"protocol": "http",
"timeout": 5.0,
"params": [1, 2, 3]
}) more readable than this? let config = %*{
"id": 5000,
"protocol": "http",
"timeout": 5.0,
"params": [1, 2, 3]
} I quite like the |
You can easily do template toJson(x: untyped): JsonNode = json.`%*`(x) in your own code. But don't make us update our code, %* is fine even though Leibniz didn't know about it. |
yes, IMO it's more readable, making it clear it returns a json as discussed here: unary operators are often best replaced by explicit names, eg:
I don't quite follow that argument; there lots of macros that are named using regular alphanumeric characters that also take untyped arguments. |
15f3a63
to
cea9542
Compare
%*
: toJson].}%*
But it is the most popular, especially in the web development space. It makes sense to make json as frictionless as possible.
You're taking a couple upvotes on a thread and using that as proof that you're right. 6 upvotes is not consensus that explicit names are better than unary operators in all cases. Plenty of people like
The convention for procs named I really don't think Nim is any danger of operator abuse. It uses symbols judicially to make common operations shorter, and uses readable names in most cases. |
that # lib/js/jsffi.nim
proc toJs*[T](val: T): JsObject {. importcpp: "(#)" .}
## Converts a value of any type to type JsObject
# lib/core/typeinfo.nim:
proc toAny*[T](x: var T): Any {.inline.} =
## constructs a ``Any`` object from `x`. This captures `x`'s address, so
# etc (toOpenArray, toFloat...) or even untyped: #lib/pure/collections/sequtils.nim:
template toSeq*(iter: untyped): untyped (although that
Thank got we don't have an operator for |
|
@GULPF I'm fine with other name suggestions, so long they contain the word
|
@timotheecour |
%*
%*
all right, I've now used Using
|
I'm with Timothee, I prefer explicit searchable names over operator unless operators are used pervasively and become a convention. The Json module should be the example library that shows how a serialisation library should be done and what kind of API can be expected from it. I would prefer if we standardize on short words like: macro to*(x: untyped, T: type JsonNode): JsonNode =
...
macro as*(x: untyped, T: type JsonNode): JsonNode =
...
macro from*(x: JsonNode, T: type Foo): Foo =
...
macro parse*(x: JsonNode, T: type Foo): Foo =
... This way YAML, protobuf and even compression libraries can have the same API. |
@timotheecour Let me explain better. If I read A common beginner bug in using the json library is transforming something like let configJson = %*{
"id": 5000,
"protocol": "http",
"timeout": 5.0,
"params": [1, 2, 3]
} into the following form let config = {
"id": 5000,
"protocol": "http",
"timeout": 5.0,
"params": [1, 2, 3]
}
let configJson = %*config and then wondering why this does not compile, having "just" moved Of course we know that the reason this does not compile is that Now, I think that if the macro was actually called |
@andreaferretti I've already made the change from
I think using
I can't agree with that because there's more than 1 type that one may convert an AST to. In other industries, protobuf is actually more common (eg, almost everything at google is serialized as protobuf; likewise some domains will deal with Bson, msgpack etc). Since we're gonna have no choice but to use I'm (reluctantly) ok to not deprecate side note (shouldn't sidetrack this PR)/cc @mratsim
one issue with
so, if we're also adding a generic alternative as you suggest, it could be instead called
but that would be in addition to the simple |
I closed this for a reason. Pointless bikeshedding. |
toJson
is more readable / searchable / guessable; furthermore easier to use in method call syntax because of required quoting:foo.toJson vs foo.
%*