Giter Site home page Giter Site logo

Comments (8)

Gummibeer avatar Gummibeer commented on September 7, 2024 1

I also prefer TEnumValue in this case.

from enum.

Gummibeer avatar Gummibeer commented on September 7, 2024

Hey,
At first: I'm not against this!

But even if value is an integer the construct still accepts integer and string. As it's instantiatable by the value and methodname.

Which is also always required for StatusEnum::DRAFT().

So it would be more correct to adjust the type of read-only property $value. 🤔

from enum.

AAllport avatar AAllport commented on September 7, 2024

Hi Gummibeer,

I see what you're saying (in regards to the instantiation). Although not quite following with that last line.

But in regards to how it will affect the DX,

If the following were replaced with templates:

Then when calling StatusEnum::DRAFT()->value you can remove the ambiguity that it may be an int or a string.
This enforcement (albeit only caught by the IDE and static analysis) can greatly help in a type-safe environment, where a downstream, maybe relying on it being backed by ints.

Could also prevent issues where you're storing ints in your database, and a newcomer comes to the team and attempts to add a string to the value set.
Granted, these issues should also be fixed in tests & code reviews. just feels any opportunity to catch this sort of issue would be awesome!

from enum.

Gummibeer avatar Gummibeer commented on September 7, 2024

I'm not really into templates - but so far I got this would allow a user to override @template EnumValue with int only instead of the packages default int|string and this would adjust everywhere in the corresponding class!?

I totally like type-safety and if I got it right: go ahead!
If I haven't got it: please correct me. 🙈

For the __construct() we could use EnumValue|string? 🤔 This would make it clear that we allow whatever your value is and the string on top.

from enum.

AAllport avatar AAllport commented on September 7, 2024

I'm not really into templates - but so far I got this would allow a user to override @template EnumValue

That's correct, I believe it's often described as PHP's answer to Generics!
I'm also I planning on contributing to Laravel to get an uptake in this: laravel/framework#34110

Hadn't considered using EnumValue|string for the constructor, but this is a great idea!

PR Inbound!

from enum.

Gummibeer avatar Gummibeer commented on September 7, 2024

One thing, if it's possible I would prefer a namespaced template to prevent conflicts.
If not can we go with SpatieEnumValue?

from enum.

AAllport avatar AAllport commented on September 7, 2024

Worth noting: https://psalm.dev/docs/annotating_code/templated_annotations/

The names of your templated types (e.g. TKey, TValue don't matter outside the scope of the class or function in which they're declared.

(So, I was originally gonna be lazy and use T) I believe it's the same behaviour with PHPStan

Happy to use SpatieEnumValue, but may be better to use either TEnumValue, TSpatieEnumValue to maintain the convention of using T to dictate it's a template?
I have no preference as such, but personally think TEnumValue looks better!

Thoughts?

from enum.

AAllport avatar AAllport commented on September 7, 2024

Also, (following some reading) looks like it'll need a slight update to spatie/laravel-enum for the inheritance (happy to sort myself also)!

See:

from enum.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.