[精讚] [會員登入]
707

【Java】[debug]多維泛型的大坑

incompatible types error

分享此文連結 //n.sfs.tw/16412

分享連結 【Java】[debug]多維泛型的大坑@小編過路君子
(文章歡迎轉載,務必尊重版權註明連結來源)
2024-05-25 13:41:22 最後編修
2024-05-25 12:26:35 By 過路君子
 

大家好,這裡是還在 Java 裡持續有新發現的小編過路君子

近期開始對 Java 中的 interface 感興趣,interface 的寫法邏輯又不同於一般我們在寫 Java

 

 

直接看程式碼:

import java.util.Objects;
import java.util.ArrayList;


public class Main
{
    // @param <T>: the type of the input to the function
    // @param <R>: the type of the plane array result of the function
    //
    //
    // e.g. PlaneArrayFunction<String, Boolean> example;
    //
    //      We need input a String value,
    //      and we gonna get the return value which type is ArrayList<ArrayList<Boolean>>.
    @FunctionalInterface
    public interface PlaneArrayFunction<T, R>
    {
        public ArrayList<ArrayList<R>> apply(T t);

        // @param <V>: the type of input to the {@code before} function,
        //             and to the composed function
        default <V> PlaneArrayFunction<V, R> compose(PlaneArrayFunction<? super V, ? extends T> before)
        {
            Objects.requireNonNull(before);

            return (V v) ->
            {
                ArrayList<ArrayList<T>> repeater = before.apply(v);

                // For test, so we didn't do anything.
                return new ArrayList<ArrayList<R>>();
            };
        }
    }

    public static void main(String[] args) {}
}

從上面的程式碼我們可以很清楚地發現,該泛型吃兩個變數型態,分別是 T 和 R。

其中 T 為傳入值型態,R 為回傳二維陣列的基礎型態。

也就是說我們雖然宣告為 R,但最終我們的執行結果型態卻是 ArrayList<ArrayList<R>> 的資料型態。

 

這樣講似乎仍然太謎語人,舉個實際的例子,但為了方便理解,小編將其適度簡化。

假設小編這樣宣告:PlaneArrayFunction<String, Integer> func = ...;

當我們執行 func.apply(7) 的時候會取得一個 ArrayList<ArrayList<String>> 這樣。

 

但是當我們編譯以上程式碼的時候,卻得到以下的錯誤:

Main.java:28: error: incompatible types: ArrayList<ArrayList<CAP#1>> cannot be converted to ArrayList<ArrayList<R>>
              ArrayList<ArrayList<R>> repeater = before.apply(v);
                                                             ^
  where T,R are type-variables:
    T extends Object declared in interface PlaneArrayFunction
    R extends Object declared in interface PlaneArrayFunction
  where CAP#1 is a fresh type-variable:
    CAP#1 extends T from capture of ? extends T

這就神奇了,竟然 ArrayList<ArrayList<T>> 接不住 ArrayList<ArrayList<? extends T>>。

 

長話短說,是因為 ArrayList<T> 並不繼承 ArrayList<? extends T>。

那 ArrayList<T> 繼承誰呢?對,就是 List<T>,而非 List<? extends T>。

這兩個物件雖然很像,但對於 Java 來說是兩個完全不同的東西,固當然無法接住。

 

 

 

後記

那最後小編怎麼解決這個錯誤呢?很簡單也很暴力,直接限制死輸入資料型態就可以順利接住了。

從 ? extends T 改成 T 即可。

default <V> PlaneArrayFunction<V, R> compose(PlaneArrayFunction<? super V, T> before)
END

你可能感興趣的文章

【C++】SFML安裝於Microsoft Visual Studio 2019 此方法僅適用單個C++專案,因為一次就適用整台電腦的方式有人寫了,而且寫得非常不錯。

【C++】一些好用的C++小功能 —— 壹 不太重要,但是可以增加程式質量以及可以讓程式看起來好像很厲害

【Minecraft】[CoreProtect|BungeeCord]如何重新命名世界或維度名稱 當只有一個伺服器的時候,問題往往處理起來非常簡單,但是一旦當伺服器成長至兩台以上,事情便開始有趣了起來

【CoreProtect】自行升級至 Minecraft 1.21.x、1.22.x 或更高版本 不知道為什麼,官方竟然無預警停止更新,我們只好自行救濟

【PaperMC - API】如何發送指令到伺服器內 How to sending or executing commands to server

【Java】[debug]多維泛型的大坑 incompatible types error

隨機好文

高捷少女:購票大作戰③  歐巴桑露出懷疑的表情。「怎麼了,您跟夏尼爾小姐不是朋友嗎?只要打電話確認就好了,不是嗎?」「是……是這樣沒錯……可是……」小穹支支吾吾地說。我的確是潔西塔的朋友,但

高捷少女:美麗島的守護者③ 小雅閉上眼睛,思索在高捷發生的點點滴滴。她心意已決,在高捷的日子的確也有快樂的部分,不過她相信換個方向是更好的決定。有關高捷的所有美好回憶,小雅決定保留在心裡就好,繼續在高捷工作只會讓自己更痛苦而已,

婕兒──她的青春② 艾米直搖頭。「我真不敢相信,小穹妳都二十幾歲了,為什麼能想出這種故事呀?」「婕兒也是二十幾歲啊,妳想想她現在是什麼樣子?」小穹不滿地指向火車的方向。

【英翻中歌詞】(二創歌)少女秘封俱樂部 Out there past the stars... that world is ours, once lost to history.  (Don’t you know?~) 在星輝之外...那屬

【英翻中歌詞】(二創歌)佐渡的二岩 Outside, inside, inside, outside 由外而內,由內而外 Find the line and watch me