![]() Return t1 + t2 // ❌ compile-time error: invalid operation: operator + not defined on t1 (variable of type T constrained by any) Go does not allow attaching generic parameters to an interface methods, so something like this is not valid Go code: The reason for that is these methods operator not only with the type of the Stream ( T), but with one or two more generic types ( R, U, A). U reduce(U identity, BiFunction accumulator, BinaryOperator combiner).R collect(Supplier supplier, BiConsumer accumulator, BiConsumer combiner).Methods that cannot be part of the interfaceĭue to a limitation in the Go implementation of generics, some of the methods that are part of the Java Stream interface cannot be part of the Go interface. So I have only defined one ToArray method that receives no arguments and returns a T (where T is the original generic type, hence preserving the original generic type). This is not the case in Go, and we do not need to receive an array generator to be able to preserve the original generic type. That is because Java generics are just compile-type checks, and all generic information is erased at runtime. One that receives no arguments and returns an Object (hence erasing the original generic type), and one that receives an array generator and returns an array of the same type (hence saving the original generic type). (I could have made MapToInt(.) int32 and MapToLong(.) int64, but I think that is not needed unless you care that much about memory usage.Īnother difference is that the Java interface defines two toArray methods. ![]() Go does not have a type like long instead, we use int64 hence the second method is not really needed. ![]() I have decided to keep only (flat)mapToInt which returns a stream of int64 (or just an int64). I have also decided to omit some methods from the Go interface.įor example, in Java, we have (flat)mapToInt and (flat)mapToLong, which return IntStream/Int and LongStream/Long.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |