Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Different languages handle this in different ways, but the most common seems to be adding access controls to the class itself, rather than just its members.

For instance, Java lets you say "public class" for a class visible outside its package, and just "class" otherwise. And if you're using Java 11 modules (nobody is though :( ), you can choose which packages are exported to consumers of your module.

In a similar vein, Rust has a `pub` access control that can be applied to modules, types, functions, and so on. A `pub` symbol is accessible outside the current crate; non-pub symbols are only accessible within one crate.

Of course, lots of languages don't have anything like this. The biggest offender is probably C++, although once its own version of modules is widely supported, we'll be able to control access somewhat like Java modules and Rust crates, with "partitions" serving the role of a (flattened) internal package hierarchy. Right now, if you do shared libraries, you can tightly control what the linker exports as a global symbol, and therefore control what users of your shared library can depend on -- `-fvisibility=hidden` will be your best friend!



> A `pub` symbol is accessible outside the current crate;

This is not universally true; it's more that pub makes it accessible to the enclosing scope. Wrapping in an extra "mod" so that this works in one file:

    mod foo {
        mod bar {
            pub fn baz() {
                
            }
        }
        
        pub fn foo() {
          bar::baz();  
        }
    }
    
    fn main() {
        // this is okay, because foo can call baz
        foo::foo();

        // this is not okay, because bar is private, and so even though baz is marked pub, its parent module isn't
        foo::bar::baz();

    }


Thanks for the correction! TIL :)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: