-
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
Procs Bound Exclusively to Objects #426
Comments
@n0bra1n3r Yeah, good catch! As the issue states, operators are even more problematic than standard procs. |
Just copying what other major programming language do to Nim doesn't seems good idea unless there is a good reason.
For people who is not familiar with these feature, that just makes learning Nim harder.
If a module has so many procs and you want to import a few of them because importing them all cause problem, I think you should split the module to smaller multiple modules.
Can your RFC reduce a number of procs need to be implemented?
tables module defines multiple types ( import tables
var t = {'a': 5, 'b': 9, 'c': 13}.toTable
t.del 'a'
echo t.len
var s = @[0, 1]
s.del 0
echo s.len
var ct: CountTable[char]
ct.inc 'a'
ct.inc 'b'
ct.del 'a'
echo ct.len Even if there are same name and same parameter type procs in different module, you can disambiguate a call by adding module name like After you wrote following code, when you write import tables
var
a = {1: "one", 2: "two"}.toTable Anyway, If books.nim defines |
More or less a duplicate of #380 |
should this be reopened? #380 this was closed but the solution only considers the operations when the name is overloaded. |
Procs Bound Exclusively to Objects
Abstract
Some procs should optionally be bound exclusively to objects. In practice this would mean that certain procs would only be accessible via object dot notation, rather than accessible directly from the current module.
This could make type imports more convenient and more like how other popular languages do it. This would also clean up the list of current definitions within the current module.
Motivation
Description
There should be an option to bind procs directly to a specific type and not just leave them as "unbinded" procs. Currently procs operating on objects will always populate the definitions of the current module, even if this is not always necessary nor wanted. As such, intellisense will start suggesting a lot of stuff, even if some of it may be completely impractical (for example, objects that have complicated "constructors"). Not importing these procs is not an option, because otherwise dot notation will not work at all.
Object fields can already not be accessed without the instance and dot notation.
These procs could also be callable via the type itself and would thus act akin to static methods in C#.
Alternatively, this could be added as an option to the import command, where
from module import Book.*
(note the .*) would import all related procs toBook
, but only via dot notation. This would remove the need for cases such as:from module import Book, Book_proc_0, Book_proc_1, ..., Book_proc_n
Implementation alternatives:
from module import Book.*
.Downsides:
Examples
Before
books.nim:
example.nim:
example_alt.nim:
After
Note: changes marked with
#####
books.nim:
example.nim:
example_alt.nim:
Backward incompatibility
This would depend on the implementation.
Other
A discussion regarding the problems with code completion when using unbinded functions in Julia.
Even if this would not be implemented in its entirety, I believe that the "augmented import command" could be worth considering.
The text was updated successfully, but these errors were encountered: