-
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
Implement interfaces/traits instead of concepts #243
Comments
Nim have a documentation for an unimplemented interface design built on top of EDIT: Provided a render of the docs below:
It's shelved due to lack of interest (in implementing) IIRC, but I think it's time we should revisit the idea. /cc @Araq since he said he has a concepts RFC pending. |
can't you use nim's OOP features for that? eg see std/streams FileStream inheriting Stream and using interface (eg |
This RFC is stale because it has been open for 1095 days with no activity. Contribute a fix or comment on the issue, or it will be closed in 30 days. |
The longer I use Nim, the more I feel the seriousness that Nim lacks Java-like interfaces.
Nim is already a better C, but Nim is not C. The reason why Nim and C are different is that there is no concept of encapsulation in C. There is no information hiding in C and you can freely access any field of any struct. However, it is different in Nim because of encapsulation. The result of encapsulation is that you cannot access the fields of an object as flexibly as C.
Here is a common example. Object
M
is created in module A to handle certain things. Later, objectN
was created in module B. objectN
needs some interoperation with objectM
. However, because of encapsulation,N
cannot access some fields ofM
, which hinders this interoperability. To support this inter-module interoperability while keeping information hiding,interface
orabstract class
is a commonly used method. They provide a declaration to defer certain operations on objectM
to the implementation objects.I think one of the main reason that Nim's lack of popularity is lack of interfaces. This makes it difficult for people who use Nim to write software to work together, and it is difficult to produce things such as plug-ins, middleware, and so on, which are an important factor for the prosperity of an industry.
I think that interface is not just a matter of style choice for Nim, but an urgent and important thing that can have a key impact on Nim's popularity and growth. In addition, I have some ideas about how to implement the interface, that is, to adopt the trait scheme of rust. Below I use a program to explain this.
Original solution - none interface
The example here describes a multi-producer-single-consumer task queue. Each thread acts as a producer or a consumer. Producers produce data quickly, and consumers consume data slightly slower. Producer and consumer cooperate through a notification mechanism. When a producer makes a new piece of data in the queue, it
signal()
the consumer; the consumer receives the producer's signal throughwait()
.Based on different implementation strategies, the behavior of
signal()
andwait()
will be different:eventfd
as the notification mechanism,signal()
sends a count signal, andwait()
receives the countersignal()
does nothing;wait()
checks the read position and write position, and then returns the amount that can be consumedIn order to make these strategies flexible, the implementation of
signal()
andwait()
was deferred until the queue was instantiated.The following is my vision of the Nim interface/trait
Testing:
The text was updated successfully, but these errors were encountered: