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:
File.new("directory/file").copy("new_path").copy("other_path")
If its too long, I could just drop the “new” method like what Python do:
File("directory/file").copy("new_path").copy("other_path")
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:
Integer.new(100).pow(10).pow(10).pow(10)
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?
12
Is it possible to create a programming language or write object-oriented code without static/class methods/variables? Absolutely. Case study: the Javascript programming language. The key insight is that, if you squint a bit and hold your head at an awkward language, classes start looking like objects. Special objects that do funny things when you poke them a certain way, sure, but objects nonetheless. Add in a prototype system (as Javascript does), and suddenly you have not only the standard approaches for object-oriented code reuse, but many others too. And all without special support for statics — instead, just add a field to your “class” (which is actually an object) and you have a static/class variable. Add a method to your “class” and you have a static/class method.
But what about globals? Well, I’m not exactly sure what you mean by globals, so let’s generalize and talk about free variables, or variables from some enclosing scope (including the global one, which is the “enclosing-est” scope there is 🙂 ). Can you imagine how hard it would be to write a program without free variables? It’s not fun 🙁 , but people have tried and succeeded, and you might find these articles interesting.
When you ask:
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?
I think the real question you’re getting at is “what are the minimal constructs needed to create a Turing-complete programming language?” or possibly “what are the minimal constructs needed to create a practical programming language?”
Those kinds of questions are awfully difficult to answer. Like, PhD-worthy, which is why people spend their lives researching answers to them. Perhaps learning about formal systems of computation, such as a lambda calculus, would help you to understand these questions better. But ultimately, they’re slippery slope questions, and nearly impossible to answer when you throw practical considerations (so that real people can use it) into the mix. If you try hard enough, you can program without nearly every single modern programming construct — does that mean it’s all just sugar?
2