项目作者: GrottoPress

项目描述 :
Crystal annotations
高级语言: Crystal
项目地址: git://github.com/GrottoPress/annotation.cr.git
创建时间: 2018-10-21T21:20:46Z
项目社区:https://github.com/GrottoPress/annotation.cr

开源协议:MIT License

下载


Crystal Annotations

Crystal supports user-defined annotations. This shard leverages this capability to add certain annotations.

Annotations supported include:

@[Final]

Marks a type or method as final. Final types cannot be inheritted, final methods cannot be overridden.

The inheriting method should have the same type restrictions as the inherited method, in order for this annotation to prevent the override.

This means the parameter types and order should be the same for the inheriting method as for the inherited method.

A difference in type restrictions of the parameters is considered an overload, not an override.

@[Override]

Marks a method as overriding an existing method in it’s super class. This means a mistyped method name in the subclass gives a compile-time error, instead of being silently added as a new method.

The inheriting method should have the same name and type restrictions as the inherited method, in order for this annotation to ensure an override.

This means the parameter names, types and order should be the same for the inheriting method as it is for the inherited method.

Installation

Add this to your application’s shard.yml:

  1. dependencies:
  2. annotation:
  3. github: GrottoPress/annotation.cr

Usage

  1. require "annotation"
  2. ##
  3. # 'Final' class
  4. ##
  5. @[Final]
  6. class Parent
  7. end
  8. # Error!: Cannot inherit final type
  9. class Child < Parent
  10. end
  11. ##
  12. # 'Final' method
  13. ##
  14. class Parent
  15. @[Final]
  16. def final_method(a : String) : Nil
  17. end
  18. end
  19. class Child < Parent
  20. # OK: This is an overload
  21. def final_method(c : UInt8) : Bool
  22. end
  23. # OK: This is an overload
  24. def final_method(a : String, b : String) : Nil
  25. end
  26. # Error!: Cannot override final method
  27. def final_method(a : String) : Nil
  28. end
  29. # Error!: Cannot override final method
  30. def final_method(b : String) : Bool
  31. true
  32. end
  33. # Error!: Cannot override final method
  34. def final_method(c) : Bool
  35. true
  36. end
  37. end
  38. ##
  39. # 'Override'
  40. ##
  41. class Parent
  42. def my_method(a : String) : Nil
  43. end
  44. end
  45. class Child < Parent
  46. # OK
  47. @[Override]
  48. def my_method(a : String) : Nil
  49. end
  50. # OK
  51. @[Override]
  52. def my_method(b)
  53. end
  54. # OK
  55. @[Override]
  56. def my_method(a : String) : Bool
  57. false
  58. end
  59. # OK
  60. @[Override]
  61. def my_method(b : String) : Bool
  62. false
  63. end
  64. # Error!: Method must exist
  65. @[Override]
  66. def non_existent
  67. end
  68. # Error!: This an overload
  69. @[Override]
  70. def my_method(a, b)
  71. end
  72. # Error!: This an overload
  73. @[Override]
  74. def my_method(a : Int32) : Nil
  75. end
  76. end

Contributing

  1. Fork it
  2. Switch to the master branch: git checkout master
  3. Create your feature branch: git checkout -b my-new-feature
  4. Make your changes, updating changelog and documentation as appropriate.
  5. Commit your changes: git commit
  6. Push to the branch: git push origin my-new-feature
  7. Submit a new Pull Request against the GrottoPress:master branch.