Quick actions

cmd+k|ctrl+k

Navigation

Languages

`step` is explicitly trusted by `DB`

Snippet info

Language

Raku

Visibility

unlisted

Author

raiph.mellor

Created

2021-10-12T19:40:04.10617Z

Updated

2021-12-14T15:28:20.950926Z

# https://www.reddit.com/r/ProgrammingLanguages/comments/q6nlq4/a_new_kind_of_scope/hgebrip

    class step { ... }            # Predeclare `step` to allow `trusts step`.
    
    class DB {
      method !query(\sql) { sql } # `!` marks method as private, except that...
      trusts step;                # ...`step` is explicitly trusted by `DB`.
    }

    package step {
      our sub getName(\api) {     # `our` allows outside code to call `getName`.
        my \sql = 'some sql';
        api!DB::query(sql);       # `trusts step` in `DB` means code in `step`
                                  # package can call private `Database` methods
                                  # (but they must be fully qualified).
      }
    }

    sub processNewHire {
      my \db = DB.new;  
#      db.query;                  # Uncommenting would yield a run-time error.

      step::getName(db);          # (If `getName` was explicitly exported, and
                                  # then explicitly imported, it could be called
                                  # without the `step::` qualifier.) 
    }

    say processNewHire;           # some sql
INFO