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

Correctly enforcing abstract! #3388

Closed
marcandre opened this issue Aug 26, 2020 · 0 comments · Fixed by #6888
Closed

Correctly enforcing abstract! #3388

marcandre opened this issue Aug 26, 2020 · 0 comments · Fixed by #6888
Labels
enhancement New feature or surprising current feature good first issue Good for newcomers runtime Related to the sorbet-runtime gem

Comments

@marcandre
Copy link

marcandre commented Aug 26, 2020

It seems that sorbet enforces abstract classes not being instantiated by defining an initialize that raises an error

This initialize apparently does not check what is the class being instantiated, is there a good reason for this?

Moreover, why not define a class method new that raises instead (only if the receiver is the current abstract class)?

The reasons to go this way are:

  1. There are valid reasons to have an abstract class that need initialization and/or initial parameters. sorbet shouldn't mess with initialize

  2. There are valid cases of an abstract class that does not require special initialization and/or additional parameters. sorbet shouldn't force having a trivial initialize method

  3. Calling super from initialize is considered by many a good practice. sorbet should not break this.

Example:

class Foo
  def self.new(*)
    return super unless self == Foo

    raise 'Can not instantiate abstract class'
  end

  def initialize
    @registry = {}
    puts "initialized, ok"
  end
end

class Bar < Foo
end

Foo.new rescue puts 'raised, ok'
Bar.new and puts 'ok'
# prints "ok" 3 times...
@marcandre marcandre added enhancement New feature or surprising current feature unconfirmed This issue has not yet been confirmed by the Sorbet team labels Aug 26, 2020
@jez jez added runtime Related to the sorbet-runtime gem good first issue Good for newcomers and removed unconfirmed This issue has not yet been confirmed by the Sorbet team labels Feb 18, 2023
@jez jez closed this as completed in #6888 May 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or surprising current feature good first issue Good for newcomers runtime Related to the sorbet-runtime gem
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants