Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exercise to focus on constructors #379

Open
bencoman opened this issue Jun 21, 2019 · 2 comments
Open

Exercise to focus on constructors #379

bencoman opened this issue Jun 21, 2019 · 2 comments

Comments

@bencoman
Copy link
Contributor

bencoman commented Jun 21, 2019

Saw this comment from a newcomer on Discord...

Delphi did have constructors more similar to Smalltalk than Java. Didn't have a silly special "new" keyword.. it was a method on the class itself.

I see a few people respond with "just use a class-side method", but I think newcomers often miss the distinction and such comments fly by without sticking. It might be good to have an exercise focused specifically on analogies to "constructors" as people understand them in other languages.

Could it be said that the "instance creation" class-side protocol is equivalent to "constructors" ?

Possible reference cases:

Rectangle
Delay
Month
Color
@bencoman
Copy link
Contributor Author

Other interesting constructors...

constructors := Object allSubclasses flatCollect: [ :c | c class allMethods select: [ :m | m protocol = 'instance creation' ] ].
distribution := (constructors collect: [ :m | m selector ]) asBag.
unique := distribution valuesAndCounts keysAndValuesRemove: [ :k :v | v > 1 or: [ k implementors size > 1 ] ].
uniqueConstructors := (unique keys flatCollect: [ :symbol | symbol implementors ] ) sorted.

@samWson
Copy link
Contributor

samWson commented Jun 22, 2019

Could it be said that the "instance creation" class-side protocol is equivalent to "constructors" ?

Short answer I'd say yes. I treat methods in this protocol as a way of getting a fully initialized object in a convenient way. In my own projects I use class side initializing methods as a public interface and the proper way to get an instance of that class instead of using #new and setting up all the instance variables yourself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants