Kotlin Contracts: assert instance on reified type parameter
up vote
3
down vote
favorite
I'm trying to write an assert function that checks if a given object is of a type T
:
@UseExperimental(ExperimentalContracts::class)
inline fun <reified T> assertIsInstance(value: Any?) {
contract {
returns() implies (value is T)
}
Assertions.assertThat(value).isInstanceOf(T::class.java)
}
The function uses AssertJ to do the concrete assertion but I'm willing to let the compiler know that after its execution, the value
is of type T
so that a smartcast is possible. It seems like this does not work because:
Error in contract description: references to type parameters are forbidden in contracts
Is there another way to achieve this behavior? What's the issue here? Will this eventually be possible?
(Using Kotlin v1.3)
generics kotlin contract assertj kotlin-reified-type-parameters
add a comment |
up vote
3
down vote
favorite
I'm trying to write an assert function that checks if a given object is of a type T
:
@UseExperimental(ExperimentalContracts::class)
inline fun <reified T> assertIsInstance(value: Any?) {
contract {
returns() implies (value is T)
}
Assertions.assertThat(value).isInstanceOf(T::class.java)
}
The function uses AssertJ to do the concrete assertion but I'm willing to let the compiler know that after its execution, the value
is of type T
so that a smartcast is possible. It seems like this does not work because:
Error in contract description: references to type parameters are forbidden in contracts
Is there another way to achieve this behavior? What's the issue here? Will this eventually be possible?
(Using Kotlin v1.3)
generics kotlin contract assertj kotlin-reified-type-parameters
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I'm trying to write an assert function that checks if a given object is of a type T
:
@UseExperimental(ExperimentalContracts::class)
inline fun <reified T> assertIsInstance(value: Any?) {
contract {
returns() implies (value is T)
}
Assertions.assertThat(value).isInstanceOf(T::class.java)
}
The function uses AssertJ to do the concrete assertion but I'm willing to let the compiler know that after its execution, the value
is of type T
so that a smartcast is possible. It seems like this does not work because:
Error in contract description: references to type parameters are forbidden in contracts
Is there another way to achieve this behavior? What's the issue here? Will this eventually be possible?
(Using Kotlin v1.3)
generics kotlin contract assertj kotlin-reified-type-parameters
I'm trying to write an assert function that checks if a given object is of a type T
:
@UseExperimental(ExperimentalContracts::class)
inline fun <reified T> assertIsInstance(value: Any?) {
contract {
returns() implies (value is T)
}
Assertions.assertThat(value).isInstanceOf(T::class.java)
}
The function uses AssertJ to do the concrete assertion but I'm willing to let the compiler know that after its execution, the value
is of type T
so that a smartcast is possible. It seems like this does not work because:
Error in contract description: references to type parameters are forbidden in contracts
Is there another way to achieve this behavior? What's the issue here? Will this eventually be possible?
(Using Kotlin v1.3)
generics kotlin contract assertj kotlin-reified-type-parameters
generics kotlin contract assertj kotlin-reified-type-parameters
edited Nov 8 at 10:34
asked Nov 8 at 10:29
s1m0nw1
23.5k53696
23.5k53696
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
At some point there were some (deeply technical) concerns regarding support of such constructions in an IDE, but it's possible that this limitation will be relaxed in the future.
thanks for the response. Any alternative approach to implement such a contract?
– s1m0nw1
Nov 8 at 12:18
add a comment |
up vote
1
down vote
This has been bugging me for a couple hours, especially since this is possible:
val x: Any = "string"
require(x is String)
val len = x.length
The compiler is clearly able to understand these, so this is likely a limitation of the contracts themselves.
I've spent a while now trying to come up with some workarounds. For reference:
@UseExperimental(ExperimentalContracts::class)
inline fun <reified T> assertIsInstance(value: Any?) {
contract {
returns() implies T::class.isInstance(value))
}
if(value !is T){
throw java.lang.IllegalArgumentException("Incorrect type");
}
}
"Unsupported construct"
@UseExperimental(ExperimentalContracts::class)
inline fun <reified T> assertIsInstance(value: Any?, condition: Boolean = value is T) {
contract {
returns() implies condition
}
if(!condition)
throw IllegalArgumentException("Incorrect type");
}
Compiles, but doesn't enable smart cast. The original motivation behind that one was placing a boolean in front of the contract, but contracts need to be the first part of a function, which made that impossible. You might as well remove the contract; it's useless in this case.
This was my last try:
@UseExperimental(ExperimentalContracts::class)
inline fun assertIsInstance(value: Any?, cls: KClass<out Any>) {
contract {
returns() implies (cls.isInstance(value))
}
if(!cls.isInstance(value))
throw IllegalArgumentException("");
}
Another "unsupported construct".
Somehow I ended up with this:
@UseExperimental(ExperimentalContracts::class)
inline fun assertIsInstance(value: Any?) {
contract {
returns() implies (value.hashCode() == 0)
}
if(value.hashCode() != 0)
throw java.lang.IllegalArgumentException();
}
But this gives a new error: only references to parameters are allowed in contract description
.
TL;DR:
It doesn't look like you can. Sneaking it in like I did in the second example doesn't trigger smart cast, and the rest don't work due to various compiler errors.
At least for now, there doesn't appear to be a way. You could of course open an issue in the Kotlin repo and ask for something like this, but for now, it doesn't appear to be possible.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
At some point there were some (deeply technical) concerns regarding support of such constructions in an IDE, but it's possible that this limitation will be relaxed in the future.
thanks for the response. Any alternative approach to implement such a contract?
– s1m0nw1
Nov 8 at 12:18
add a comment |
up vote
2
down vote
At some point there were some (deeply technical) concerns regarding support of such constructions in an IDE, but it's possible that this limitation will be relaxed in the future.
thanks for the response. Any alternative approach to implement such a contract?
– s1m0nw1
Nov 8 at 12:18
add a comment |
up vote
2
down vote
up vote
2
down vote
At some point there were some (deeply technical) concerns regarding support of such constructions in an IDE, but it's possible that this limitation will be relaxed in the future.
At some point there were some (deeply technical) concerns regarding support of such constructions in an IDE, but it's possible that this limitation will be relaxed in the future.
answered Nov 8 at 11:40
Dmitry Savvinov
212
212
thanks for the response. Any alternative approach to implement such a contract?
– s1m0nw1
Nov 8 at 12:18
add a comment |
thanks for the response. Any alternative approach to implement such a contract?
– s1m0nw1
Nov 8 at 12:18
thanks for the response. Any alternative approach to implement such a contract?
– s1m0nw1
Nov 8 at 12:18
thanks for the response. Any alternative approach to implement such a contract?
– s1m0nw1
Nov 8 at 12:18
add a comment |
up vote
1
down vote
This has been bugging me for a couple hours, especially since this is possible:
val x: Any = "string"
require(x is String)
val len = x.length
The compiler is clearly able to understand these, so this is likely a limitation of the contracts themselves.
I've spent a while now trying to come up with some workarounds. For reference:
@UseExperimental(ExperimentalContracts::class)
inline fun <reified T> assertIsInstance(value: Any?) {
contract {
returns() implies T::class.isInstance(value))
}
if(value !is T){
throw java.lang.IllegalArgumentException("Incorrect type");
}
}
"Unsupported construct"
@UseExperimental(ExperimentalContracts::class)
inline fun <reified T> assertIsInstance(value: Any?, condition: Boolean = value is T) {
contract {
returns() implies condition
}
if(!condition)
throw IllegalArgumentException("Incorrect type");
}
Compiles, but doesn't enable smart cast. The original motivation behind that one was placing a boolean in front of the contract, but contracts need to be the first part of a function, which made that impossible. You might as well remove the contract; it's useless in this case.
This was my last try:
@UseExperimental(ExperimentalContracts::class)
inline fun assertIsInstance(value: Any?, cls: KClass<out Any>) {
contract {
returns() implies (cls.isInstance(value))
}
if(!cls.isInstance(value))
throw IllegalArgumentException("");
}
Another "unsupported construct".
Somehow I ended up with this:
@UseExperimental(ExperimentalContracts::class)
inline fun assertIsInstance(value: Any?) {
contract {
returns() implies (value.hashCode() == 0)
}
if(value.hashCode() != 0)
throw java.lang.IllegalArgumentException();
}
But this gives a new error: only references to parameters are allowed in contract description
.
TL;DR:
It doesn't look like you can. Sneaking it in like I did in the second example doesn't trigger smart cast, and the rest don't work due to various compiler errors.
At least for now, there doesn't appear to be a way. You could of course open an issue in the Kotlin repo and ask for something like this, but for now, it doesn't appear to be possible.
add a comment |
up vote
1
down vote
This has been bugging me for a couple hours, especially since this is possible:
val x: Any = "string"
require(x is String)
val len = x.length
The compiler is clearly able to understand these, so this is likely a limitation of the contracts themselves.
I've spent a while now trying to come up with some workarounds. For reference:
@UseExperimental(ExperimentalContracts::class)
inline fun <reified T> assertIsInstance(value: Any?) {
contract {
returns() implies T::class.isInstance(value))
}
if(value !is T){
throw java.lang.IllegalArgumentException("Incorrect type");
}
}
"Unsupported construct"
@UseExperimental(ExperimentalContracts::class)
inline fun <reified T> assertIsInstance(value: Any?, condition: Boolean = value is T) {
contract {
returns() implies condition
}
if(!condition)
throw IllegalArgumentException("Incorrect type");
}
Compiles, but doesn't enable smart cast. The original motivation behind that one was placing a boolean in front of the contract, but contracts need to be the first part of a function, which made that impossible. You might as well remove the contract; it's useless in this case.
This was my last try:
@UseExperimental(ExperimentalContracts::class)
inline fun assertIsInstance(value: Any?, cls: KClass<out Any>) {
contract {
returns() implies (cls.isInstance(value))
}
if(!cls.isInstance(value))
throw IllegalArgumentException("");
}
Another "unsupported construct".
Somehow I ended up with this:
@UseExperimental(ExperimentalContracts::class)
inline fun assertIsInstance(value: Any?) {
contract {
returns() implies (value.hashCode() == 0)
}
if(value.hashCode() != 0)
throw java.lang.IllegalArgumentException();
}
But this gives a new error: only references to parameters are allowed in contract description
.
TL;DR:
It doesn't look like you can. Sneaking it in like I did in the second example doesn't trigger smart cast, and the rest don't work due to various compiler errors.
At least for now, there doesn't appear to be a way. You could of course open an issue in the Kotlin repo and ask for something like this, but for now, it doesn't appear to be possible.
add a comment |
up vote
1
down vote
up vote
1
down vote
This has been bugging me for a couple hours, especially since this is possible:
val x: Any = "string"
require(x is String)
val len = x.length
The compiler is clearly able to understand these, so this is likely a limitation of the contracts themselves.
I've spent a while now trying to come up with some workarounds. For reference:
@UseExperimental(ExperimentalContracts::class)
inline fun <reified T> assertIsInstance(value: Any?) {
contract {
returns() implies T::class.isInstance(value))
}
if(value !is T){
throw java.lang.IllegalArgumentException("Incorrect type");
}
}
"Unsupported construct"
@UseExperimental(ExperimentalContracts::class)
inline fun <reified T> assertIsInstance(value: Any?, condition: Boolean = value is T) {
contract {
returns() implies condition
}
if(!condition)
throw IllegalArgumentException("Incorrect type");
}
Compiles, but doesn't enable smart cast. The original motivation behind that one was placing a boolean in front of the contract, but contracts need to be the first part of a function, which made that impossible. You might as well remove the contract; it's useless in this case.
This was my last try:
@UseExperimental(ExperimentalContracts::class)
inline fun assertIsInstance(value: Any?, cls: KClass<out Any>) {
contract {
returns() implies (cls.isInstance(value))
}
if(!cls.isInstance(value))
throw IllegalArgumentException("");
}
Another "unsupported construct".
Somehow I ended up with this:
@UseExperimental(ExperimentalContracts::class)
inline fun assertIsInstance(value: Any?) {
contract {
returns() implies (value.hashCode() == 0)
}
if(value.hashCode() != 0)
throw java.lang.IllegalArgumentException();
}
But this gives a new error: only references to parameters are allowed in contract description
.
TL;DR:
It doesn't look like you can. Sneaking it in like I did in the second example doesn't trigger smart cast, and the rest don't work due to various compiler errors.
At least for now, there doesn't appear to be a way. You could of course open an issue in the Kotlin repo and ask for something like this, but for now, it doesn't appear to be possible.
This has been bugging me for a couple hours, especially since this is possible:
val x: Any = "string"
require(x is String)
val len = x.length
The compiler is clearly able to understand these, so this is likely a limitation of the contracts themselves.
I've spent a while now trying to come up with some workarounds. For reference:
@UseExperimental(ExperimentalContracts::class)
inline fun <reified T> assertIsInstance(value: Any?) {
contract {
returns() implies T::class.isInstance(value))
}
if(value !is T){
throw java.lang.IllegalArgumentException("Incorrect type");
}
}
"Unsupported construct"
@UseExperimental(ExperimentalContracts::class)
inline fun <reified T> assertIsInstance(value: Any?, condition: Boolean = value is T) {
contract {
returns() implies condition
}
if(!condition)
throw IllegalArgumentException("Incorrect type");
}
Compiles, but doesn't enable smart cast. The original motivation behind that one was placing a boolean in front of the contract, but contracts need to be the first part of a function, which made that impossible. You might as well remove the contract; it's useless in this case.
This was my last try:
@UseExperimental(ExperimentalContracts::class)
inline fun assertIsInstance(value: Any?, cls: KClass<out Any>) {
contract {
returns() implies (cls.isInstance(value))
}
if(!cls.isInstance(value))
throw IllegalArgumentException("");
}
Another "unsupported construct".
Somehow I ended up with this:
@UseExperimental(ExperimentalContracts::class)
inline fun assertIsInstance(value: Any?) {
contract {
returns() implies (value.hashCode() == 0)
}
if(value.hashCode() != 0)
throw java.lang.IllegalArgumentException();
}
But this gives a new error: only references to parameters are allowed in contract description
.
TL;DR:
It doesn't look like you can. Sneaking it in like I did in the second example doesn't trigger smart cast, and the rest don't work due to various compiler errors.
At least for now, there doesn't appear to be a way. You could of course open an issue in the Kotlin repo and ask for something like this, but for now, it doesn't appear to be possible.
answered Nov 8 at 15:23
Zoe
10.2k73475
10.2k73475
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53205855%2fkotlin-contracts-assert-instance-on-reified-type-parameter%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown