In this post, we will see how to resolve unable to share a proc between two classes
Question:I am playing around with NIM and classes. Ima trying ti have one i it function for two classes, however it seems that this result in the initialization nit to be possible. The code looks like this
Best Answer:The following syntax
Attribute|Attributesdefines a typeclass.
The nim manual states that “type classes are static constraints to be enforced at type instantiations. Type classes are not really types in themselves but are instead a system of providing generic “checks” that ultimately resolve to some singular type. Type classes do not allow for runtime type dynamism, unlike object variants or methods.”
Procedures using typeclasses are implicitly generic.
A call to a generic proc statically instantiates a specialized proc for each unique type. Therefore, you have to conditionally check what type your generic has resolved to at compile time.
Enough word vomit. Here’s example code:
Secondtype is the only function that has
something.uniqueField = 2.
If you have better answer, please add a comment about this, thank you!