[精讚] [會員登入]
769

【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

你可能感興趣的文章

【無料版模】﹝CSS&HTML﹞製作epub電子書版模免費下載&附使用教學 一個專門用來製作epub的簡單CSS檔案,基本上已經可以做出一本還不錯的電子書,讓妳的電子書不在只有預設的字體、樣式

【Wicket】[nonce]如何導入css以及javascript 如果只是用一般的 HTML link 或是 script 標籤來寫,會被 CSP 擋住,禁止加載資源,那我們除了關閉 CSP 以外還有沒有辦法來解決呢?

【Raspberry Pi/樹梅派】(gcc 10) 如何安裝 gcc & g++ 需要的時間非常久,不愧是gcc的編譯

【!@#$%^&*()_+ language】!@#$%^&*()_+ 程式語言介紹 一款看起來像是在鬧的程式語言

【Kali Linux】[history -c]如何清除歷史記錄 非bash環境會使history部分功能變的非法

【Maven】如何開始撰寫 Minecraft PaperMC server插件 萬事起頭難,要開始寫一個插件最難的一步就是要怎麼開始寫

隨機好文

高捷少女:小穹與果仁巧克力㊥ 艾咪臉上泛起笑容說:「妳知道嗎?就跟花語一樣,每一種巧克力也都有各自的涵義:薄荷巧克力代表初戀;卡通巧克力代表天真爛漫;而果仁巧克力代表的是窩心,還有想陪伴對方的心情,這在德國是女生之間在慶生時會彼此

高捷少女:美麗島的守護者⑤  雖然暫時不用怕牠們了,可是一直躲在這裡終究不是辦法,小雅心想。她看看四周,這間更衣室沒有窗戶或後門,她不禁著急起來,不可能一直躲在這裡面,但從門出去只會被群貓圍攻而已。小雅低下頭苦思該怎麼辦,過了不

【歌評】蓮台野夜行 - 幻視之夜 ~ Ghostly Eyes 若有什麼是在聽到boss曲前的鋪襯,那一定就是每個系列的道中曲

【歌評】蓮台野夜行 - 幻想的永遠祭 蓮台野的探險雖然結束了,但是,誰知道是不是一段新的探險的序曲呢?

【想法】關於網路上的謾罵這檔事 網路是個自由的世界,每個人都享有平等的發言權,但是,請永遠記得,在網路上漫罵別人一定會留下證據的,就算之後刪文,你怎能確保對方沒有截圖存證?