Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #36211

Re: class parameter with generic type on a factory

Path csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From Andreas Leitgeb <avl@logic.at>
Newsgroups comp.lang.java.programmer
Subject Re: class parameter with generic type on a factory
Date Sun, 22 Jan 2017 11:02:18 -0000 (UTC)
Organization A noiseless patient Spider
Lines 21
Message-ID <slrno8949c.pvm.avl@logic.at> (permalink)
References <39e83ab1-6a6a-4211-8f3b-0237d56d4202@googlegroups.com> <o61avt$tmt$1@gioia.aioe.org>
Reply-To avl@logic.at
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding 8bit
Injection-Date Sun, 22 Jan 2017 11:02:18 -0000 (UTC)
Injection-Info mx02.eternal-september.org; posting-host="3c9ab00deec5f49fd49821abc74efa11"; logging-data="13166"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/62y2CKlQxuUVhX4MhKnDR"
User-Agent slrn/1.0.2 (Linux)
Cancel-Lock sha1:8g8+3vVmh0mxZ6wssX518r0/E94=
Xref csiph.com comp.lang.java.programmer:36211

Show key headers only | View raw


Arne Vajhøj <arne@vajhoej.dk> wrote:
> Some experimentation ended up with this:
>    public static interface MyInterface<T> ...
>    public static class MyClass<T> implements MyInterface<T> ...
>    public static class MyFactory<T> {
>       public MyFactory(Class<? extends MyClass<T>> clz) ...
>    }

Indeed, the factory method can specify a type-parameterized class for
its argument, which removes the "dirty" unchecked cast from inside the
factory, but there is no way to completely get rid of the unchcked cast.

>    public static void main(String[] args) throws [...] {
>       @SuppressWarnings("unchecked")
>       MyFactory<String> mf = new MyFactory<String>(
>          (Class<? extends MyClass<String>>)MyClass.class
>       );

Actually, I think leaving the cast inside the factory makes for cleaner
code.

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

class parameter with generic type on a factory Philipp Kraus <philipp.kraus.flashpixx@gmail.com> - 2017-01-21 08:19 -0800
  Re: class parameter with generic type on a factory Andreas Leitgeb <avl@logic.at> - 2017-01-21 19:33 +0000
    Re: class parameter with generic type on a factory Philipp Kraus <philipp.kraus.flashpixx@gmail.com> - 2017-01-21 11:51 -0800
      Re: class parameter with generic type on a factory Andreas Leitgeb <avl@logic.at> - 2017-01-21 20:11 +0000
  Re: class parameter with generic type on a factory Arne Vajhøj <arne@vajhoej.dk> - 2017-01-21 23:04 -0500
    Re: class parameter with generic type on a factory Andreas Leitgeb <avl@logic.at> - 2017-01-22 11:02 +0000
      Re: class parameter with generic type on a factory Arne Vajhøj <arne@vajhoej.dk> - 2017-01-22 14:00 -0500
  Re: class parameter with generic type on a factory Marcel Mueller <news.5.maazl@spamgourmet.org> - 2017-01-22 09:14 +0100

csiph-web