[精讚] [會員登入]
684

【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

你可能感興趣的文章

【Nexus Repository Manager】(deploy)使他人可以對遠端資料庫做讀寫 使用 Nexus Repository Manager 來讓各個工程師控制自己的 Jar 包,不會有 Github Merge Crashed 問題。

【Arduino/LinkIt 7697】實作小小的 MQTT Publish & Subscribe 若是要多點對多點傳輸資料,那使用 MQTT 即可快速的達成我們所需的目的

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

【PaperMC】從 Waterfall 切換至 Velocity waterfall 已經停止支援,最後停留在支援 Minecraft 1.21.6,之後的 Minecraft 版本便不再支援

【D語言】D language 安裝及使用 一種比較少見的程式語言

【C++】class練習 — 檢測該字串是否為迴圈 第一次的C++結構式寫法,雖然以後應該會見怪不怪,但畢竟是第一次所以還是想保存下來

隨機好文

希萌創意預計在今年7月繼東津萌米之後再次推出新遊戲--食用性少女! 今天來介紹希萌創意的心企劃案,來讓大家知道這個消息!讓大家的錢包君一起來減肥吧!Ψ(☆w☆)

高捷少女:小穹與果仁巧克力㊦ 「如果妳跟一個女生同班三年,看過她午休流口水跟狼吞虎嚥地吃午餐,就算變成高捷代言人,也很難把她當女神的啦!」她說,小穹氣得搶走她義大利麵裡的蝦子,其他人笑得花枝招展。

高捷少女:耐耐的新年驚喜③ 即使如此,夫人仍然每年都會問老爺是否能空出一週時間,但總是被回絕,除了前年以外。老爺答應夫人一定會排出空檔,他們在去年的二十三日前往澳洲。」耐耐嘆了口氣。「那一天的晚上,我打電話給媽媽時,她很高興地告

【有趣玩物】4D Toys 用三維的視野探索未知的四維世界 在三維的我們看到的四維物體的互動是怎麼樣子的呢?這遊戲光是看看就超級有趣~

【英翻中歌詞】(二創歌)哈德曼的妖怪少女 Pleasure to see you, invisible friend! 真高興見到你,無形的朋友! You think you can see