Throwing exceptions from records
Ty Young
youngty1997 at gmail.com
Thu Mar 5 20:04:40 UTC 2020
On 3/5/20 1:39 PM, Vicente Romero wrote:
> Hi Ty,
>
> On 3/5/20 2:12 PM, Ty Young wrote:
>>
>> Actually going to expand this a bit after messing around with records...
>>
>>
>> How is records supposed to work beyond the most basic usage of
>> Point(int x, int y)? There isn't much information out there for the
>> more advanced usages so I'm confused.
>>
>>
>> To go into more detail, I'm trying to convert this:
>>
>>
>> https://github.com/BlueGoliath/Goliath-Nvidia-Bindings/blob/master/modules/org.goliath.bindings.nvml/src/main/java/org/goliath/bindings/nvml/functions/nvmlInit.java
>>
>>
>>
>> which doesn't seem possible as-is because you can't throw exceptions
>> from record constructors.
>
> you can't throw exceptions from _canonical_ record constructors, being
> a canonical record constructor the one with the same signature as the
> record header but you can do:
>
> record R(int i) {
> public R() throws Exception { // not a canonical constructor so
> no restriction
> this(someMethodReturningIntThatThrows()); // invoking the
> canonical constructor
> }
> }
>>
>>
>> Furthermore there seems to be some constraint where constructor
>> arguments have to match the record argument signature. Why does that
>> even matter?
>
> only the canonical but you can declare other constructors with a
> different signature
The problem is that the constructor arguments require preprocessing but
you can't do that since the canonical constructor has to be called
first. I suppose static methods could be used to do that but that feels
like a bad workaround for something that will probably be fairly common.
As a knock off effect individual class implementations are basically
worthless since every record that implements my "NativeFunction"
interface will be forced to accept the same type arguments. That might
sound like a good thing but the thing is, while they accept the same
object type, the details of those objects can be very different from one
another.
>>
>>
>> As a result, I can't convert classes like:
>>
>>
>> https://github.com/BlueGoliath/Crosspoint/blob/master/src/main/java/org/goliath/crosspoint/unions/BasicNativeUnion.java
>>
>>
>>
>> to a record.
>
> HTH,
>
> Vicente
More information about the amber-dev
mailing list