I'm designing my own programming language for fun, and I'm thinking of making it fully Object-oriented (no statics, no globals, no class variables, no class methods), so I need to find a way to completely eliminate the needs of class methods, but, first, I need to make sure that I know all the problems that could only be solved by using class methods.
So, usually, class methods is used for creating helpers, and named constructors, yes? (Anything else?).
Currently, this is what I have to replace those with Object-oriented designs:
For example: the File helpers, I'm thinking of designing it like this:
class File def initialize(path) @path = path end def copy(destination) # Copy file from @path to destination # Then open destination path by creating new file instance end end
I'm actually using C, but I wrote the example in Ruby so it will be easier to understand.
No class methods, and you could chain it like this:
If its too long, I could just drop the "new" method like what Python do:
Math helpers can be replaced with mixins:
module Math def pow(value) @value = @value ** value end end class Integer include Math def initialize(value) @value = value end end
Call it like this:
In case you still need some helpers:
module Helpers # Some block of codes end class Object include Helpers end
And now it's accessible everywhere.
To add more helpers, just modify the Helpers module:
module Helpers # Add more helpers end
We can create factory class instead of named constructor:
class Lexer def initialize(source) @source = source end end class LexerFactory def create_from_file(file) return Lexer.new(File.read(file)) end def create_from_string(string) return Lexer.new(string) end end class Base def initialize(lexer_factory) @lexer_factory = lexer_factory end def lexer @lexer_factory end end lexer = Base.new(LexerFactory.new).lexer.create_from_file("directory/file")
So my questions is: What am I missing? What you can't do without class methods, and class variables? What problems that could only be solved by using those?