class: named constructor .new instead of callable class - #622
Closed
lneto wants to merge 1 commit into
Closed
Conversation
The OOP-style modules were callable classes: require returned a class you invoked
to construct (rt(), inet.tcp()). That is inconsistent with the rest of lunatik,
where you require a table and call a named constructor (socket.new, rcu.table,
data.new), and it makes construction read as an anonymous call. And the __call
constructor only worked one subclass deep: a grandchild class (an nl80211 built
on genl) was not callable, since __call is not inherited as a metamethod.
Now class{} returns a table, :extend{} derives a subclass, .new(...) builds an
instance running the class's :init(...), and close is the to-be-closed handler.
Constructors move from :__call to :init; callers construct with rt.new(),
genl.new(), inet.tcp.new(), unix.stream.new(path). No __call, so subclassing
works at any depth.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The OOP-style modules (
socket.inet/unix,netlink.rt/genl) were callable classes:requirereturned a class you invoked to construct, e.g.require("netlink.rt")()/inet.tcp(). Two problems:socket.new,rcu.table,data.new). The callable form reads as an anonymous call, and calling a method on the class by mistake fails cryptically (on an internalsequencefield) instead of clearly.__callonly works one subclass deep. A grandchild class (e.g. annl80211built ongenl) is not callable, because__callis looked up on the metatable by rawget and is not inherited. That forced a hand-rolledsetmetatablein the grandchild.New shape
class{}returns a table;:extend{}derives a specialization;.new(...)builds an instance, running the class's:init(...);closeis wired as the to-be-closed handler. Constructors move from:__callto:init.No
__call, so subclassing works at any depth — this dissolves the grandchild problem (thenl80211setmetatablehack goes away when that branch adopts this).Validated in the kernel (without a module reload):
rt.new()/genl.new()drive the netlink dumps, andunix.stream.new(path)/inet.tcp.new()construct working sockets. The full socket and netlink suites should be run on merge.Open branches that construct these (
nl80211#617,netfailover#618,floodguard#619) rebase onto this and adopt.new.🤖 Generated with Claude Code