Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Help Collisions, Isolate the Worlds

Help Collisions, Isolate the Worlds

The talk about https://bugs.ruby-lang.org/issues/19744
Why and how we will (not) introduce namespaces in Ruby
松江Ruby会議10

Satoshi Tagomori

September 16, 2023
Tweet

More Decks by Satoshi Tagomori

Other Decks in Programming

Transcript

  1. Satoshi Tagomori a.k.a., @tagomoris দߐRubyձٞ10 - Sep 16, 2023 Help

    Collisions, Isolate the Worlds Names, Modules, Versions, and NameSpaces
  2. Satoshi Tagomori (@tagomoris) Independent developer Technical Consultant OSS Maintainer •

    Fluentd • MessagePack / msgpack-ruby • Norikra, Woothee, … tagomoris
  3. Collisions Names, Modules, Versions Name Collisions: No one can use

    a name in two ways. Module Collisions: Modules can be modified, globally, from anywhere. Version Collisions: Two different versions of a module can’t be loaded.
  4. Name Collisions No one can use a name in two

    ways. • A structured namespace: • Foo::Bar::Baz • Major scenario: • Top-level “Configuration” in gems • Typical RoR classes vs Ruby, Gems, etc (like “Guild”) • Two apps in a process (in the context of “Modular monolith”)
  5. Module Collisions Modules can be modi fi ed, globally, from

    anywhere. • A single Module/Class instance • Oj.default_options
  6. Version Collisions Two di ff erent versions of a library

    can’t be loaded. • Dependency hell • Module A depends on C ver 1.5.x • Module B depends on C ver 1.6.x • The App depends on A and B
  7. NameSpaces in Practice N languages, N ways • Library space

    • Global NameSpace (+ Version) • Local names only • Names in code • Library name • Local alias • Class loader
  8. NameSpaces in languages Many ways, Many hells… • JavaScript/TypeScript (npm)

    • Local names + Local Aliases • Java (classes w/ FQDN) • Global NameSpace + Library name • Class loaders (oops….) • Kotlin • Global NameSpace + Local Alias • Python (pip) • Global NameSpace (+ Local Alias) • Go • Local names + Local Aliases
  9. Libraries and NameSpaces Libraries can’t be re-de fi ned •

    Once a library lives globally, it can’t be locally • Ruby has a huge library set - RubyGems
  10. NameSpace Requirements What is it? • One native extension libraries

    of two versions at a time • Same name, Di ff erent (native) code, Di ff erent access path (local name) • Isolate libraries in speci fi ed namespaces at the required time (“on read”) Ruby Process LibraryX version: a.b.c “x.so” LibraryX version: x.y.z “x.so” ns1::X ns2::X
  11. NameSpace Requirements What is it? • One native extension libraries

    of two versions at a time • Same name, Di ff erent (native) code, Di ff erent access path (local name) • Isolate libraries in speci fi ed namespaces at the required time (“on read”) Ruby Process LibraryX version: a.b.c “x.so” LibraryX version: x.y.z “x.so” ns1::X X
  12. My Proposal: module NameSpace < Module Respects load( fi lename,

    wrap) • The wrap of load(filename, wrap): • works as a simple namespace • Introduce a sub-class of Module: NameSpace • works like the wrap of load • has instance methods #require, #load • Dependencies will be required/loaded in the namespace recursively
  13. Hard Things, TODOs, Designs, … “How This Language Should Be?”

    • Hard Things: Native extension MAY need to be updated, in case • Native extensions using others’ C-functions
 https://github.com/tagomoris/sequel_pg/pull/1 • TODOs: Upgrade path SHOULD be de fi ned • How can we mark a gem is namespace-ready? • How can we determine the namespace of a library? (RubyGems/Bundler) • Designs • What’s the namespace in Ruby? • Problems around the top-level names in namespaces
  14. Top-level Names in NameSpaces Should it be global? Or local?

    • “ns1::X” - A top-level name “X” de fi ned in a namespace “ns1” • It MUST be so, to isolate “ns1::X” from the “X” • “X” in the code isolated in “ns1” • It should be “ns1::X”, of course • But many gems add methods on “String” by “class String”… • “::X” in the code isolated in “ns1” • Should it be “ns1::X”? or the “X”? • “class ::String” should be possible
  15. The TODOs I need discussions • To Go? Or No

    Go? • In this way? Or another way? Or NO WAY? • Which way to go? • How can we break codes IN NAMESPACES? (Not globally)