objectionary / eo

EOLANG, an Experimental Pure Object-Oriented Programming Language Based on 𝜑-calculus
https://www.eolang.org
MIT License
1.01k stars 126 forks source link

Meatier code example: Command line input #32

Closed nqafield closed 7 years ago

nqafield commented 7 years ago

I've been trying to work out how a slightly more involved program might look. I wanted to try and write a program that would ask for an integer from the user and then square it and print the result. Should still be pretty simple, right? But I'm really struggling for some reason. :(

Any suggestions?

Edit: What I was trying to come up with was an executable example. Something that would be "complete", at least at the top level. I assume the "Hello, world" example is supposed to be complete in this sense. It just needs some explanation. Trying to come up with a "meatier" example was an attempt to provide that explanation, and go a bit further. (NB: I'm not suggesting the example would have to be perfect and never change, just that it would be our current best guess of an executable example that is a bit more real-world and interactive.)

gukoff commented 7 years ago

Speaking of our current syntax, the following example comes to mind:

stdout
  print:
    integer:
      stdin line:
    power:
      2
    asString:

Here, one-level statements integer, power and asString mean carrying.

nqafield commented 7 years ago

Can you explain this example a bit @gukoff? I'm not really following it.

Maybe add some console output? e.g. "Please enter an integer to square" and "The square of %s is %s". (It might help me understand what's going on.)

Also, I was hoping we could use this example to help explain what's going on in the "Hello, world" example, so could you use the working syntax that's in the readme? (Or if you think it's not possible with that syntax then can you explain why?)

DronMDF commented 7 years ago

Is object method presint in eo? formatted print should be hi level object about stdout. stdout have low level write method.

object int_from_stdin as Int:
     value():
          formatted_out: stdout
             .print("Please enter an integer to square :")
             .flush()
          formatted_in: stdin
             .as_int()

object power_app as Application:
     run():
           formatted_out: stdout
                  .format("The square is {square}")
                  .where("square", power: int_from_stdin: , 2)

I can without variables. But I cannot without code sequence... :)

nqafield commented 7 years ago

I can without variables. But I cannot without code sequence... :)

Yes, that's the tricky thing isn't it! No variables, no procedural-ness. :)

Interesting example though @DronMDF, but I have some questions.

[I know it's easy to "criticise" and harder to actually produce, so forgive me. I'm just trying to understand and explore rather than actually criticise! (See here and here for my pathetic attempts, if you want a laugh.) :grin:]

1) What would it look like from the top level, as a code example of the "Hello, world" kind (an actual executable program)?

import org.eolang.cli
import org.eolang.???

cli:
  printed:
    [...] # What goes here?

2) Both of your objects have no constructors. Do we expect to see some objects that are composed of nothing?

3) What do your value() and run() methods return?

DronMDF commented 7 years ago

I do not have integral picture.

1,3) In hello world example a look only object initialization. Not anybody invoke this. Where a return value in max example? This object ctor with if object. What type an if object?

2) @yegor256 say about ctor only for initialization of attribute, not for code. Can there be objects without attributes? Or this is a procedural thinking?

Composition of object this is a first step of object programming. Second step is invocation. Where i'm wrong?

nqafield commented 7 years ago

Absolutely @DronMDF. I don't have the whole picture either, I'm just trying to get there. :)

In the "Hello, world" example I think there is an assumption that the cli object automatically invokes stuff. (If it doesn't, then yes, it's not an executable example. And this is exactly why I was trying to explore another code example...) [I've made an edit to the original issue to better explain what it was about.]

The max example returns an Int and then there is a single statement that is that return value.

The if object takes three things in its constructor (condition, then, else). Its type is the same as the then and else arguments I guess. I also assume it has to automatically invoke itself somehow. [Edit: I'm not sure this makes any sense.]

I agree that there's no code in constructors. I was just wondering whether there should always be a constructor, otherwise the object isn't actually composed of anything.

nqafield commented 7 years ago

Proposing a clarification regarding objects always being composed of/encapsulating at least one other object. #33

nqafield commented 7 years ago

https://gitter.im/yegor256/elegantobjects?at=58356d349a5479d0484f94e6

yegor256 commented 7 years ago

@pa9ey I'll try to come up with some bigger example soon. I actually think that the best way to make a real example is to create a simple compiler and try to create a simple application :) I'm thinking about doing it now, just to demonstrate the concept

nqafield commented 7 years ago

I think an outcome for this issue is likely to be addressed by #58 so if there are no objections I will close this issue.