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

Helper macro for NotImplementedError #13078

Open
HertzDevil opened this issue Feb 15, 2023 · 4 comments
Open

Helper macro for NotImplementedError #13078

HertzDevil opened this issue Feb 15, 2023 · 4 comments

Comments

@HertzDevil
Copy link
Contributor

The standard library and the compiler together raise NotImplementedError in 141 places, and their message is almost always the fully qualified type name followed by the method name. That calls for a refactor:

macro not_implemented!
  ::raise(::NotImplementedError.new(\{{ "#{@type}##{@def.name}" }}))
end

Then we can write things such as:

class Process::Status
  def exit_signal : Signal
    {% if flag?(:unix) %}
      Signal.from_value(signal_code)
    {% else %}
      not_implemented!
    {% end %}
  end
end

# on Windows:
$?.exit_signal # Unhandled exception: Not Implemented: Process::Status#exit_signal (NotImplementedError)

A more robust macro should at least also check for class methods and top-level defs when producing that exception message, such as WinError.value and the string_build_via_utf16 spec helper. (Yet other things, like file-private defs, #11764, and methods on Class itself are also possible.)

The above defines not_implemented! as a public, top-level macro, since third-party libraries may write platform-specific checks similar to the ones we have in the standard library. If we keep this internally it would be Crystal.not_implemented! instead.

@yxhuvud
Copy link
Contributor

yxhuvud commented Feb 15, 2023

Related in many ways: #4749

@straight-shoota
Copy link
Member

A more robust macro should at least also check for class methods and top-level defs when producing that exception message

I figure the compiler should probably provide a method to get the fully qualified name of a method. Basically exposing something like Def#short_reference, optionally with arguments maybe.

@crysbot
Copy link
Collaborator

crysbot commented Feb 3, 2025

This issue has been mentioned on Crystal Forum. There might be relevant details there:

https://forum.crystal-lang.org/t/rfc-annotating-methods-that-raise-notimplementederror/7692/1

@hugopl
Copy link
Contributor

hugopl commented Feb 10, 2025

I already use a macro like that in projects of mine.

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

No branches or pull requests

5 participants