How to Handle Constrained Types as Arguments in Go 1.18 Generics?

Susan Sarandon
Release: 2024-10-27 08:08:31
Original
312 people have browsed it

How to Handle Constrained Types as Arguments in Go 1.18 Generics?

Using Constrained Types as Arguments in Go 1.18

In Go 1.18 generics, constrained types allow developers to define custom data types with specified constraints. However, when using constrained types as arguments to functions that expect concrete types, type conversion is required.

Consider the following example:

<code class="go">type Pokemon interface {
    ReceiveDamage(float64)
    InflictDamage(Pokemon)
}

type Float interface {
    float32 | float64
}

type Charmander[F Float] struct {
    Health      F
    AttackPower F
}</code>
Copy after login

Here, Charmander implements the Pokemon interface with a generic type parameter F that can be instantiated with either float32 or float64. However, attempts to use AttackPower (a constrained type) as a float64 argument in other.ReceiveDamage() result in an error:

<code class="go">func (c *Charmander[float64]) InflictDamage(other Pokemon) {
    other.ReceiveDamage(c.AttackPower)
}</code>
Copy after login

Type Conversion for Compatibility

To resolve this issue, type conversion must be used to ensure compatibility between the constrained type and the concrete type expected by the function. This is because F is not equivalent to float64 even when constrained to float64.

The corrected code becomes:

<code class="go">func (c *Charmander[T]) InflictDamage(other Pokemon) {
    other.ReceiveDamage(float64(c.AttackPower))
}</code>
Copy after login

Similarly, the ReceiveDamage() method is modified to handle a constrained type (Health) by converting the damage argument to the constrained type:

<code class="go">func (c *Charmander[T]) ReceiveDamage(damage float64) {
    c.Health -= T(damage)
}</code>
Copy after login

Precision Considerations

It is important to note that the conversion from float64 to float32 (when F is instantiated with float32) can result in loss of precision. This may need to be considered in specific use cases.

The above is the detailed content of How to Handle Constrained Types as Arguments in Go 1.18 Generics?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!