-
-
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
[WIP] first pass at fixing #7430: universal to conversion template #7488
Conversation
I think |
I think the idea was to not only support parsing, but general conversion though |
Meh, general conversion is too general. Think about error handling/reporting. |
@Araq how about: # only for parsing strings
proc parse(s: string; dest: typedesc[bool]): bool = s.parseBool
echo "true".parse(bool) # for other conversions we have `to` conversion procs/templates defined in their own modules, for eg
var a:A
echo a.to(B) somewhere (eg in # most generic
template to*[T](s: string; dest: typedesc[T]): auto = s.parse(dest)
# still works
echo "true".to(bool) that way user can choose the most generic
can you elaborate what you'd want for that? |
I agree with @Araq. Think about the following: "tru".to(bool) # Error? False?
"{{{".to(JsonNode) # Definitely an error.
|
I think the semantics should be obvious here: "tru".to(bool) # throws Exception (this is not javascript where everything unsafely converts to everything ;-) )
"{{{".to(JsonNode) # throws Exception but again, see the proposal for having
can you elaborate here (say w an example)? the input argument is not known at compile time, so we'd get RT errors when conversion fails
other conversions not involving input string can also raise an exception, eg: 257.to(uint8) # throws Exception (same as D: rdmd --eval='257.to!(ubyte).writeln;' std/conv.d(1449): Conversion positive overflow
classA_instance.to(classB) # throws Exception (depending on typeid(classA_instance) and it's relation to classB)
jsonNode.to(NimType) # throws Exception if can't be deserialized from jsonNode
protoMsg.to(NimType) # throws Exception if can't be deserialized from protoMsg likewise with user defined types where it would make sense to throw |
I don't see the need for |
I think until we can ascertain how useful this is in the wild, this should go into a Nimble package. Further down the line we can decide whether it belongs in the stdlib once we see how useful it is. The stdlib is a gentle thing, we want to be conservative, it's much easier to add things to it than it is to remove. |
I agree. But I still think we could implement the |
Adding |
here's a 1st pass at https://github.com/nim-lang/Nim/issues/7430 ; I (or others) can add more conversions once we agree on design principles below
design principles:
to
overload should be in the module where the types / conversions are defined, egs.to(JsonNode)
goes inlib/pure/json.nim
,s.to(bool)
goes inlib/pure/strutils.nim
etcthis is preferable to having a dedicated hodge-podge
conv.nim
module that would import everything needed and wrap for egparseFoo
toto(Foo)
; that would not scale (eg wouldn't work with nimble packages, which can't be imported byconv.nim
) ;the existing
parseFoo(a:string)
toFoo(...)
etc should become thin wrappers around the correspondingto
proc/template (theto
would contain the actual implementation) and eventually deprecatedeg: see this PR for parseBool becoming thin wrapper around
s.to(bool)
prefer
proc
totemplate
whenever possiblewe should standardize ASAP on ideal signature, eg the name of arguments matters in case generic code uses named parameter syntax (eg:
to(s:foo, Bar)
; so I suggest:which is short and generic (
s
standing forsource
)to
and not even defineparseFoo
ortoFoo