Home > Backend Development > Golang > Why Does Assigning Value Literals to Generic Struct Fields in Go Sometimes Result in 'IncompatibleAssign' Errors?

Why Does Assigning Value Literals to Generic Struct Fields in Go Sometimes Result in 'IncompatibleAssign' Errors?

Susan Sarandon
Release: 2024-12-19 06:54:20
Original
626 people have browsed it

Why Does Assigning Value Literals to Generic Struct Fields in Go Sometimes Result in

Assigning Value Literals to Generic Struct Fields: Troubleshooting IncompatibleAssignment Errors

In Go, generic types can be defined with constraints that restrict the allowed types for their fields. However, when assigning value literals to such fields, certain constraints can trigger an "IncompatibleAssign" error.

Consider the following scenario:

type constraint interface {
    ~float32 | ~float64
}

type foo[T constraint] struct {
    val T
}

func (f *foo[float64]) setValToPi() {
    f.val = 3.14
}
Copy after login

This code compiles without errors because the constraint interface includes both ~float32 and ~float64. However, if we modify the constraint to also include ~int:

type constraint interface {
    ~float32 | ~float64 | ~int
}

type foo[T constraint] struct {
    val T
}

func (f *foo[float64]) setValToPi() {
    f.val = 3.14 // IncompatibleAssign: cannot use 3.14 (untyped float constant) as float64 value in assignment
}
Copy after login

We encounter an error because the value literal 3.14 (an untyped floating-point constant) cannot be assigned to all possible instances of foo[T], specifically those where T is ~int.

The issue arises because the method declaration:

func (f *foo[float64]) setValToPi() {
    // ...
}
Copy after login

is merely a declaration. It does not instantiate the generic type foo. The identifier float64 within the square brackets is a type parameter name, not a fixed type.

Therefore, within the method, the only known information about the type of val is that it is constrained by constraint. In this case, constraint is the union ~float32 | ~float64 | ~int, meaning the value 3.14 cannot be assigned to the ~int instance of foo[T].

Solution:

To resolve this issue, we have several options:

  1. Declare the method as:
func (f *foo[T]) setValToPi() {
    // ...
}
Copy after login

This will result in the same error, but with T instead of float64.

  1. Declare the method as:
func (f *foo[T]) SetValue(val T) {
    f.val = val
}
Copy after login

This accepts a value of the type parameter type, allowing the assignment of value literals like 3.14 to any subtype within the constraint.

  1. Use any/interface{} as the field type and implement a custom method to check and convert the assigned value within the method:
type foo struct {
    val interface{}
}

func (f *foo) SetPi() {
    f.val = 3.14
}
Copy after login

The above is the detailed content of Why Does Assigning Value Literals to Generic Struct Fields in Go Sometimes Result in 'IncompatibleAssign' Errors?. 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