Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
3  
This question looks similar to "why use an operator for addition if you can just use a function/method named add". I would say, go ahead and leave it out and see if you get into any real trouble or inconvenience. It is your language, so you can add them later on if you find you want them after all. –  Bart van Ingen Schenau May 3 '13 at 17:53
3  
If you have a class with no variables or methods...what do your classes do? –  Jonathan Rich May 3 '13 at 17:59
2  
fully Object-oriented (no statics, no globals, no class variables, no class methods) <-- This doesn't make any sense at all.. It's like saying "a car without wheels, doors, windows, or a gas peddle", that's a rock, not a car. –  Jimmy Hoffa May 3 '13 at 18:04
3  
How would you implement singleton without statics? –  svick May 3 '13 at 18:19
4  
Design your language, on paper. Use your language to solve real world problems, again on paper. After you've actually used your language a couple of dozen times, go and develop your language. Otherwise, you're reinventing PHP. –  Gilbert Le Blanc May 3 '13 at 19:14
show 7 more comments

closed as not constructive by Eric King, MichaelT, Frank Shearar, Steven A. Lowe, Bart van Ingen Schenau May 4 '13 at 7:55

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

up vote 5 down vote accepted

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?

share|improve this answer
    
It is all just sugar, unless of course you're flipping switches on the front panel. But sugar sure does help the medicine go down more smoothly. –  Robert Harvey May 3 '13 at 21:30
    
"If you try hard enough, you can program without nearly every single modern programming construct". That answer my question. Thanks. –  random_guy May 4 '13 at 9:18
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.