GithubHelp home page GithubHelp logo

Comments (10)

yegor256 avatar yegor256 commented on August 16, 2024 3

@g4s8 but the order of parameters we can infer from the order of object attributes. In this example:

object alphabet as Book:
  Text @isbn
  Text @title
  ctor()
    alphabet:
      "978-1-51916-691-3",
      "The Alphabet"
  ctor(String i, String t):
    @isbn = i
    @title = t

We know that @isbn goes first and @title goes next. What is the point of repeating this fact again in a constructor?

from eo.

aravindps avatar aravindps commented on August 16, 2024

@yegor256 It is okay, if the main constructor is used only for setting values to all the attributes. What happens for derived attributes or constants in the object?

Player player = Player.new("a"){
	String .name
	int .overallScore
	new(String name) {
	    .name = name
	    .overallScore = 0
	}
}

If the constructor allows has named arguments, it makes sense.

Player player = Player.new(name="a"){
	String .name
	int .overallScore
	new(String name) {
	    .name = name
	    .overallScore = 0
	}
}

from eo.

mdbs99 avatar mdbs99 commented on August 16, 2024

@yegor256 always will exists a primary constructor, even if you don't create one.
I mean, all constructor we create will be a second.
So I think we can forget this ticket.

from eo.

NikoGJ avatar NikoGJ commented on August 16, 2024

In some old programming languages, programs were made of 2 distincts parts, the declaration part with the initialization of variables and the actual code which is using them.

I wonder if this "usage" could be applied to OO....
Can we imagine an object having 2 modes, an initialization mode which would be just basic linking object attributes and setup, and a runtime mode (the actual simulation mode made of method calls between objects, the one that we're used to). 2 opaque and isolated worlds.

It's just an idea though, I'm saying this because what we do when creating objects and initialize their state require a different set of methods than when we use them..

from eo.

yegor256 avatar yegor256 commented on August 16, 2024

@aravindps in this case we just create a primary ctor, which accepts all arguments and then a secondary one, which provides defaults:

Player player = Player.new("a"){
	String .name
	int .overallScore
	new(String name) {
	    new(name, 0)
	}
	new(String name, int score) {
	    .name = name
	    .overallScore = score
	}
}

from eo.

g4s8 avatar g4s8 commented on August 16, 2024

I think that we need primary constructor declaration (without any code or attribute initialization) to specify parameters order. Without it attributes initialization may be ambiguous. E.g. object from Readme:

object alphabet as Book:
  Text @isbn
  Text @title
  ctor()
    alphabet:
      "978-1-51916-691-3",
      "The Alphabet"
  ctor(String i, String t):
    @isbn = i
    @title = t
  Text asText():
    sprintf:
      "ISBN is %s, title is '%s'",
      @isbn,
      @title

It's not clear (without primary constructor) where is title and where is isbn, because both paraeters are strings:

ctor()
    alphabet:
      "978-1-51916-691-3",
      "The Alphabet"

So I suggest to declare primary constructor like this:

ctor(String isbn, String title)
  • without body
  • without colon
  • parameter names are same as attribute names

@yegor256 , what do you say?

from eo.

g4s8 avatar g4s8 commented on August 16, 2024

We know that @isbn goes first and @title goes next. What is the point of repeating this fact again in a constructor?

Looks logical, in this way we don't need primary constructors at all.

from eo.

yegor256 avatar yegor256 commented on August 16, 2024

@g4s8 I think it's important to update README too, after #112

from eo.

g4s8 avatar g4s8 commented on August 16, 2024

@yegor256 done!

from eo.

yegor256 avatar yegor256 commented on August 16, 2024

@g4s8 thanks a lot!

from eo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.