You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Sep 20, 2022. It is now read-only.
The compiler is behaving as intended (although the error message in the Playground should arguably read "main.fo" instead of "main.go").
When you write a generic function, it means that any type can be substituted in place of the type parameters. In your case, Sum is a function which accepts a slice of any type T. The += operator does not work for every type, so this is an error in the function definition.
What you probably want to do is constrain the type parameter in Sum so that it only accepts types which work with the += operator. This is not possible in Fo right now, but you can see #11 for a discussion of how this might work in the future.
There are two main routes we could take. One is that Fo will come with a predefined sum type called numeric which consists of all built-in numeric types (int, uint, float64, etc). In this case, you could rewrite the function with a type constraint on T:
funcSum[Tnumeric](slice []T) T {
varsumTfor_, item:=rangeslice {
// Since T must be a member of the numeric type, we know the += operator will work.sum+=item
}
returnsum
}
The other route is that we don't define a numeric type (and possibly don't allow sum types to be used as constraints, only interfaces). In this case, you would need to use wrapper types such as Int, Uint, Float32, etc. which have an Add method defined. You would then need to define an Adder interface which consists of a single method Add (or import an existing definition). Constraining the parameter T so that it must satisfy the Adder interface would look like this:
funcSum[TAdder](slice []T) T {
varsumTfor_, item:=rangeslice {
// Since T must be of type Adder, we know that it has an Add method.sum=sum.Add(item)
}
returnsum
}
@thewizardplusplus I'm going to close this. If you want to contribute to the discussion about type constraints in general, please do so on #11. If you have any other questions about your specific example, feel free to ask here on this issue.
Why doesn't this code work?
https://play.folang.org/p/BAoAfXkEfwH
It outputs:
main.go:8:3: invalid operation: operator + not defined for sum (variable of type T)
.The text was updated successfully, but these errors were encountered: