-
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
Converted parameters with to[T, toT]
types
#549
Comments
This is already achievable with concepts: type ToInt = concept x
int(x) is int
proc foo(x: ToInt): int = 2 * x
doAssert foo(2.Natural) == 4
###
type
ToString = concept x
string(x) is string
Stringlet = distinct string
proc add(x: var string; s: ToString) =
x &= string(s)
var s: string = "what follows was "
s.add("not a string".Stringlet)
doAssert s == "what follows was not a string" |
Ahh! idk that. fancy |
Wait, I just discovered that you are doing explicit converting with |
Related #168 |
this is also why we recommend to never use Indeed, what this RFC lacks a way to deal with narrowing conversions safely - these are more common than one would thing, ie string-to-int can obviously fail, as can many other common ones - in other words, for this to fly, it would ideally be constrained to non-narrowing conversions (ie conversions that don't panic or raise exceptions) |
Abstract
A proc can declare that some certain parameter of it can take arguments of different types and convert them to certain types.
Motivation
Sometimes converters are used to simplify calling some routines, like here. But now that converters are global, they sometimes cause troubles, like here, leading to ambigious calls.
Now that converters are frequently used only for some specific routines, why not make routines, instead of types, have converters? We can already do similar things with generic,
auto
type,when
and overloading, but this RFC can make it simpler to write such a conversion.Description
Parameters can have "converters" (not Nim
converter
s). This is likevarargs
, which can already convert arguments to certain types.Like
varargs
, we can make this a special type,to
:It is also useful to convert with certain routines:
Like
varargs
, overloaded procs are used on demand:If this proposal has downsides, I think:
varargs
already existing, it is still easy to understand.Code Examples
No response
Backwards Compatibility
No response
The text was updated successfully, but these errors were encountered: