Quantcast

Weird behavior with Java generics

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Weird behavior with Java generics

Shannon Carey
I'm using a Java library parquet-avro-1.8.1 which has a method:

public class ParquetReader<T> implements Closeable {
  public static <T> Builder<T> builder(Path file) {
    return new Builder<T>(file);
  }
}
...
public static class Builder<T> {
  public ParquetReader<T> build() throws IOException {
    return new ParquetReader<T>(conf, file, getReadSupport(), filter);
  }
}

When I use that method from Scala 2.11, for example I get:

val builder: Builder[GenericRecord] = AvroParquetReader.builder[GenericRecord](new Path(tmpFile.getPath))
val wtf: ParquetReader[_] = builder.build()
val read: Any = wtf.read()

The generic type somehow gets unbound or otherwise disappears. Why? What can I do about it? Can anyone point me to the relevant documentation which would explain the behavior so that I can read up on it?

Thanks for any help!

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Weird behavior with Java generics

Shannon Carey
Never mind! The builder was not extending its parent class correctly. Has been fixed in a newer version https://github.com/apache/parquet-mr/commit/630830476a6270e317e84229996a6bf92bd903ca

Thanks anyway :)

On Thursday, March 2, 2017 at 5:06:31 PM UTC-6, Shannon Carey wrote:
I'm using a Java library parquet-avro-1.8.1 which has a method:

public class ParquetReader<T> implements Closeable {
  public static <T> Builder<T> builder(Path file) {
    return new Builder<T>(file);
  }
}
...
public static class Builder<T> {
  public ParquetReader<T> build() throws IOException {
    return new ParquetReader<T>(conf, file, getReadSupport(), filter);
  }
}

When I use that method from Scala 2.11, for example I get:

val builder: Builder[GenericRecord] = AvroParquetReader.builder[GenericRecord](new Path(tmpFile.getPath))
val wtf: ParquetReader[_] = builder.build()
val read: Any = wtf.read()

The generic type somehow gets unbound or otherwise disappears. Why? What can I do about it? Can anyone point me to the relevant documentation which would explain the behavior so that I can read up on it?

Thanks for any help!

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Weird behavior with Java generics

Adriaan Moors-7
In reply to this post by Shannon Carey
Does the following work?

val builder = AvroParquetReader.builder[GenericRecord](new Path(tmpFile.getPath))
val wtf = builder.build()
val read = wtf.read()

In general, the compiler does not add bounds to wildcards for you, as common usage is to simply leave off the type entirely (we've tried, but this Javaism is hard to reconcile with other Scala code patterns). 

If you'd like to write the type explicitly, it would look something like ParquetReader[_ <: GenericRecord], but I wouldn't recommend that.

cheers
adriaan


On Thu, Mar 2, 2017 at 3:06 PM Shannon Carey <[hidden email]> wrote:
I'm using a Java library parquet-avro-1.8.1 which has a method:

public class ParquetReader<T> implements Closeable {
  public static <T> Builder<T> builder(Path file) {
    return new Builder<T>(file);
  }
}
...
public static class Builder<T> {
  public ParquetReader<T> build() throws IOException {
    return new ParquetReader<T>(conf, file, getReadSupport(), filter);
  }
}

When I use that method from Scala 2.11, for example I get:

val builder: Builder[GenericRecord] = AvroParquetReader.builder[GenericRecord](new Path(tmpFile.getPath))
val wtf: ParquetReader[_] = builder.build()
val read: Any = wtf.read()

The generic type somehow gets unbound or otherwise disappears. Why? What can I do about it? Can anyone point me to the relevant documentation which would explain the behavior so that I can read up on it?

Thanks for any help!

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Weird behavior with Java generics

Adriaan Moors-7
Ah, glad it works!

On Thu, Mar 2, 2017 at 3:29 PM Adriaan Moors <[hidden email]> wrote:
Does the following work?

val builder = AvroParquetReader.builder[GenericRecord](new Path(tmpFile.getPath))
val wtf = builder.build()
val read = wtf.read()

In general, the compiler does not add bounds to wildcards for you, as common usage is to simply leave off the type entirely (we've tried, but this Javaism is hard to reconcile with other Scala code patterns). 

If you'd like to write the type explicitly, it would look something like ParquetReader[_ <: GenericRecord], but I wouldn't recommend that.

cheers
adriaan


On Thu, Mar 2, 2017 at 3:06 PM Shannon Carey <[hidden email]> wrote:
I'm using a Java library parquet-avro-1.8.1 which has a method:

public class ParquetReader<T> implements Closeable {
  public static <T> Builder<T> builder(Path file) {
    return new Builder<T>(file);
  }
}
...
public static class Builder<T> {
  public ParquetReader<T> build() throws IOException {
    return new ParquetReader<T>(conf, file, getReadSupport(), filter);
  }
}

When I use that method from Scala 2.11, for example I get:

val builder: Builder[GenericRecord] = AvroParquetReader.builder[GenericRecord](new Path(tmpFile.getPath))
val wtf: ParquetReader[_] = builder.build()
val read: Any = wtf.read()

The generic type somehow gets unbound or otherwise disappears. Why? What can I do about it? Can anyone point me to the relevant documentation which would explain the behavior so that I can read up on it?

Thanks for any help!

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Loading...