spierce7
11/15/2019, 12:50 AMliminal
11/15/2019, 3:13 AMinterface Foo<T : Any>
and interface Foo<T>
?df
11/15/2019, 10:02 AMFudge
11/15/2019, 6:53 PMyawkat
11/16/2019, 10:22 AMnuhkoca
11/16/2019, 4:22 PMyml or yaml
files?
I have below block in my config.yml however it throws below error:
mapping values are not allowed here in "<unicode string>", line 95, column 18: filters:
workflows:
version: 2
build_and_test:
jobs:
- build_and_setup
filters:
branches:
only: master
I use CircleCI
and in the official documentation, it shows the same usage.
https://circleci.com/docs/2.0/configuration-reference/#full-exampleDerek Peirce
11/16/2019, 9:38 PMclass Appender<out T1>(private val value: T1) {
fun <T2 : T3, T3> addTo(list: List<T2>): List<T3> = (list + value) as List<T3>
}
fun main() {
val appender = Appender(2.0)
val list = listOf(1)
val combined = appender.addTo<Int, Number>(list)
println(combined)
}
Suppose I have an Appender
class that creates a list by appending values to another list. The above code works for this, but:
- It's necessary to specify T3
- There's no guarantee here that T3
is a supertype of T1
, just T2
From what I've seen, I can specify `T3 : T1, but not my aim of T1 : T3
. Additionally, I would like T3
to be inferred automatically, similar to how listOf(1) + listOf(2.0)
gives List<Any>
by default, but can be specified as List<Number>
without issue. In particular, I would like Appender<Nothing>
to return List<T2>
without having to specify addTo<T2, T2>
.
(Obviously, Appender<Nothing>
wouldn't be useful, but my use-case has other side effects. The method is also used to implement an interface, so converting it to a static extension method as is done for List + List
isn't an option.)
Does anyone know how this problem could be solved, or at least simplified, in Kotlin?magisu
11/17/2019, 7:00 PMmzgreen
11/17/2019, 9:54 PMJakub Gwóźdź
11/18/2019, 8:13 AMpublic class GenericsJava {
public static Condition testJooqGenerics(Table<?> table, List<Object> list) {
Field<Object> myColumn = table.field("MY_COLUMN", Object.class);
return <http://myColumn.in|myColumn.in>(list);
}
}
works well in Java, the Kotlin version object GenericsKotlin {
fun testJooqGenerics(table: Table<*>, list: List<Any?>?): Condition {
val myColumn: Field<Any> = table.field("MY_COLUMN", Any::class.java)
return myColumn.`in`(list)
}
}
yelds Overload resolution ambiguity: @Support public abstract fun `in`(vararg p0: Any!): Condition! defined in org.jooq.Field
@Support public abstract fun `in`(p0: (Mutable)Collection<*>!): Condition! defined in org.jooq.Field
Is there any way to work around this API issue?henrik
11/18/2019, 9:13 AMgtrefs
11/18/2019, 1:11 PMShumilin Alexandr
11/18/2019, 1:39 PMStephan Schroeder
11/18/2019, 2:21 PMsealed class Data<T: Any> {
class Single<T: Any>@JsonCreator constructor(@JsonValue val single: T?): Data<T>()
class ListOf<T: Any>@JsonCreator constructor(@JsonValue val list: List<T>): Data<T>()
}
how do I created a correct typeRef for these to methods:
private fun <T: Any> readResponseWithSingleData(
jsonMapper: ObjectMapper,
json: String
): Response<T, Data.Single<T>> {
val typeRef = object : TypeReference<Response<T, Data.Single<T>>>() {}
return jsonMapper.readValue<Response<T, Data.Single<T>>>(json, typeRef)
}
private fun <T : Any> readResponseWithListOfData(
jsonMapper: ObjectMapper,
json: String
): Response<T, Data.ListOf<T>> {
val typeRef = object : TypeReference<Response<T, Data.ListOf<T>>>() {}
return jsonMapper.readValue<Response<T, Data.ListOf<T>>>(json, typeRef)
}
The current code will compile but because of type erasure the real type T will be discarded and a LinkedHashMap
is always used instead (which causes a ClassCast at runtime.)
I can’t make these methods inline and use reified types because I pass both methods as parameter.
I’m fairly certains that the answer is to use Jackson.TypeFactory.constructParametricType (https://fasterxml.github.io/jackson-databind/javadoc/2.9/com/fasterxml/jackson/databind/type/TypeFactory.html#constructParametricType-java.lang.Class-java.lang.Class...- )
It’s just that the generic parameters T is so deep enbedded (it’s a bit more complex that the example provided in the API with List<Set<T>>
). What would the answer be assuming that I’d pass a third argument clazz: Class<T>
to both methods?
Response
looks like this:
data class Response<T: Any, D: Data<T>> @JsonCreator constructor(
@JsonProperty("data") val data: Map<String, D>?,
@JsonProperty("errors") val errors: List<String>?
)
Fudge
11/18/2019, 4:38 PMZac Sweers
11/18/2019, 4:54 PMHullaballoonatic
11/18/2019, 5:23 PMgetFoo
defined but no foo
, you cannot add val foo: Foo = ...
to your kotlin class because of an "accidental override." Is there a way to tell the compiler you intended to override getFoo
? or am I required to use the syntactically awful private val foo: Foo = ...
override fun getFoo(): Foo = foo
adams2
11/18/2019, 5:24 PM.javaClass
and ::class.java
return different valuesuser
11/18/2019, 7:53 PMuser
11/18/2019, 8:08 PMstandinga
11/18/2019, 11:56 PMSlackbot
11/19/2019, 3:22 AMFudge
11/19/2019, 10:02 AMsuper
method in an overriden extension method?
open class Foo {
open fun Int.bar() {
println("Bar")
}
}
class FooImpl : Foo() {
override fun Int.bar() {
super.bar() // Unresolved reference
}
}
In this case, how can I call Foo#bar
inside FooImpl#bar
?Hexa
11/19/2019, 10:22 AMSergey Chelombitko
11/19/2019, 11:12 AMClass name may only contain letters and digits
steenooo
11/19/2019, 11:56 AMConstantin Weisser
11/19/2019, 12:34 PMJoan Colmenero
11/19/2019, 3:35 PMgetOrElse
on my ResultWrapper
class?
sealed class ResultWrapper<out T> {
data class Success<out T : Any>(val value: T) : ResultWrapper<T>()
data class Failure<out T : Any>(val failure: Throwable) : ResultWrapper<T>()
inline fun <B> fold(ifFailure: (Throwable) -> B, ifSuccess: (T) -> B): B =
when (this) {
is Failure -> ifFailure(failure)
is Success -> ifSuccess(value)
}
companion object {
fun <T : Any> just(t: T): ResultWrapper<T> = Success(t)
fun <T : Any> raise(t: Throwable): ResultWrapper<Throwable> = Failure(t)
}
}
I'm getting a Success(value=MyModel(....))
And I need to do an Assert.assertEquals(expectedResult, result)
where expectedResult
is MyModel
and Result
is Success(value=MyModel(....))
.
Any idea?Ashutosh Panda
11/19/2019, 4:47 PMimport kotlin.random.Random
var number=3
var rollDice:(Int)->Int={ number:Int->Random.nextInt(1 , 13)/number}
fun roll(number,operation:(Int)->Int):Int
{
return rollDice(number)
}
println("${roll(number,rollDice)}")
bascan
11/19/2019, 4:48 PMtailrec fun increment(i: Int): Int =
if (i == 3) i
else i + increment(i + 1)