Talk:Object oriented programming in Nasal
Jump to navigation
Jump to search
Emulating proper Encapsulation via MetaProgramming
Even without modifying the C code, we could probably do something along these lines, and make the ctor compile() the class to internally bind() the public/protected/private namespaces before returning the object:
var Position3D = {
# public methods are bound to the outer Position3D namespace
public: {
setAlt: func(feet) me.altitude_ft=feet,
},
# protected methods are only bound via parents lookup (need to check this)
protected: {
foo: func {},
},
# are only visible to methods
private: {
latitude_deg:, longitude_deg:, altitude_ft:,
},
};
Position3D.new = func {
var m = { parents: [Position3D],
};
foreach(var symbol; keys(Position3D.public) ) {
m[symbol]=symbol;
bind(m[symbol], m, Position3D);
}
return m;
};
So there would be no members directly inside the Position3D hash - they would need to be bound via the ctor
inline vs. separate methods
according to some fgplot feedback, one of the "Nasal OO road blocks" seems to be the two different methods of declaring classes, i.e. as a single hash with comma-separated fields/methods, and as separate funcs that are added to a hash - the latter seems to feel more natural to most people, especially those with previous programming experience. We may want to cover this eventually