Skip to content
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

generic to should work instead of non-generic ofInt etc #10

Closed
timotheecour opened this issue Sep 23, 2020 · 5 comments
Closed

generic to should work instead of non-generic ofInt etc #10

timotheecour opened this issue Sep 23, 2020 · 5 comments

Comments

@timotheecour
Copy link
Contributor

let a1 = mybson.to(int) # doesn't work but should, for generic code
let a2 = old["time"].ofInt # works but not generic
@mashingan
Copy link
Owner

The to macro works exclusively for BsonDocument to Type. I could overload to to convert more flat types but refrained to do so.

You can provide your own overloaded to for flat types like:

template to(b: BsonBase, typ: typedesc): untyped =
  var r: typ = b
  r

let b5 = 5.toBson
let i5 = b5.to(int)
doAssert i5 == 5

Note that we can immediately doAssert b5 == 5 above as the ofInt (and all variants of ofType ) is converter.

I don't think will add this to the lib, should this be added as example instead?

@timotheecour
Copy link
Contributor Author

timotheecour commented Sep 26, 2020

@mashingan
custom operators like !> are usually frowned upon, for good reasons:

  • not standard (unlike things like +, => etc)
  • not searchable, not discoverable

operators do make sense in certain situations like std/wrapnils because of technical reasons (operator precedence), I can give more details and point to relevant discussion if needed.

I really like the previous bson name, why was it changed to !> ?

Visually !> can be (forcefully) looked like b.

this is a stretch ;-)

it looks like you did that to avoid the parens in

bson({ insertId: 8})
=>
!>{ insertId: 8}

but the parens don't seem needed even with non operators, eg:

import std/json
template foo(t: untyped): untyped = %* t
let s = foo {"a1": 12}
echo s.pretty

@mashingan
Copy link
Owner

mashingan commented Sep 27, 2020

custom operators like !> are usually frowned upon, for good reasons:
it looks like you did that to avoid the parens in

the bson is still working, and many internal implementations are still using it.
Yes, it's added for convenience to avoid the parenthesis, last time I tried to omit the parenthesis, it was failed. IIRC, I tried it like bson{....} and failed. Can't remember whether it's exactly so or not.

this is a stretch ;-)

Haha, maybe I can revert this but I'll see for several releases whether this is really convenient or just my misunderstanding.
It's added to conveniently define inline bson used as argument in several APIs. Well, if it's to disappear, it won't be in for long though.

@timotheecour
Copy link
Contributor Author

Yes, it's added for convenience to avoid the parenthesis, last time I tried to omit the parenthesis, it was failed. IIRC, I tried it like bson{....} and failed. Can't remember whether it's exactly so or not.

that's because you need a space, this works and is what I'm using:

bson { insertId: 8}

unless I'm missing something, I'd recommend to just use bson and remove or deprecate !> (but optionally, change some examples to use the parenthesis-less version as shown above)

@mashingan
Copy link
Owner

Reasonable, I don't want to add more aliases and the !> indeed doesn't do much to be included in the lib.
I'll revert/remove the !> in the next tagged version.
I'll update the examples in readme soon as it doesn't need the tagged version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants