This post is related to issues addressed in 
<https://github.com/nim-lang/RFCs/issues/380> and it attempts to solve them. 
I'll quote some statements from there (copyrights not mentioned)

A remarkable observation:

> Instead of attaching syms to types, I think we should attach scopes (the 
> declaration scope) to syms, so that symbols available at declaration will 
> always be available at instantiation.

This is a description of a sym as a scope predicate, or, said otherwise, it has 
a scope predicate. Predicates are available in Nim already, they are called 
concepts. Scope concepts update the scope, they have an effect. This can be 
expressed within different ways. I choose the star "*" symbol (a pragma could 
do the same) The example is taken from the RFC as well and it looks like this:
    
    
    # Module objs.nim
    
    import hashes
    
    type
      Obj* = object
        x*, y*: int
        z*: string # to be ignored for equality
    
    proc `==`*(a, b: Obj): bool =
      a.x == b.x and a.y == b.y
    
    proc hash*(a: Obj): Hash =
      $!(hash(a.x) &! hash(a.y))
    
    
    # Module  bindtable.nim
    import tables,hashes
    export tables  # potentially redundant
    
    type
      BTable[T,V] = concept
        type U* = Table[T,V]
        Self : U
        proc `==`*(a, b: T): bool
        proc hash*(a: T): Hash
        #an example for a proc with the eigentype U
        #proc getval*(t: U, k: int) : int
    
    
    
    # main.nim
    
    from objs import Obj
    import bindtable
    
    var t: BTable[Obj, int]
    t[Obj(x: 3, y: 4, z: "debug")] = 34
    echo t[Obj(x: 3, y: 4, z: "ignored")]
    
    
    
    
    Run

Reply via email to