crystal icon indicating copy to clipboard operation
crystal copied to clipboard

Document how to change base type of an enum

Open Blacksmoke16 opened this issue 5 years ago • 8 comments

  • ~Add TypeNode.base_type for Enum types~
  • ~Add Enum.base_type that uses the macro method~
  • Mention the ability to change the base type in the Enum API docs

Blacksmoke16 avatar Oct 06 '20 03:10 Blacksmoke16

Umm, not sure why specs failed. They passed locally for me. Some caching issue?

Blacksmoke16 avatar Oct 06 '20 03:10 Blacksmoke16

std specs also run with the current compiler, and base_type is not defined there. You can implement it by doing typeof(values.first)

asterite avatar Oct 06 '20 12:10 asterite

~@asterite But how are all the other macro methods useable? {% if @type.has_attribute?("Flags") %} is a TypeNode method and that doesn't cause things to break.~

EDIT: NVM, I misread. I suppose, but think this macro method would be most ideal :/. Maybe I'll add a todo to update it after the next release.

Blacksmoke16 avatar Oct 06 '20 12:10 Blacksmoke16

Can you show a use case for this? typeof(value) already works to determine the base type. So this is essentially just adding a dedicated method for this? Where is this needed?

straight-shoota avatar Nov 15 '20 11:11 straight-shoota

@straight-shoota It originally came from some chat in Gitter. https://gitter.im/crystal-lang/crystal?at=5f7afbbc6e0eb8446967c41e

I'd maybe ask @HertzDevil what he was doing it for. But IMO, there should be a better API for getting that type than doing a typeof of one of the values given it's known at compile time anyway.

Blacksmoke16 avatar Nov 15 '20 15:11 Blacksmoke16

Unless there's a common use case I'd say type(value) is totally fine. No need for adding an extra part to the API. typeof is evaluated at compile time, so there's no runtime impact.

straight-shoota avatar Nov 21 '20 11:11 straight-shoota

Will leave it up to @HertzDevil then. If he doesn't have any specific use cases then I'm fine with closing, or at least revert the new method and keep the doc updates.

Blacksmoke16 avatar Nov 21 '20 15:11 Blacksmoke16

I kinda forgot about this, but I just reverted the methods and just made this document how to change the base type of an enum.

Blacksmoke16 avatar Aug 13 '21 15:08 Blacksmoke16