Swift equivalent for MIN and MAX macros

min and max are defined in Swift: func max<T : Comparable>(x: T, y: T, rest: T…) -> T func min<T : Comparable>(x: T, y: T, rest: T…) -> T and used like so: let min = min(1, 2) let max = max(1, 2) See this great writeup on documented & undocumented built-in functions in Swift.

How to get generic parameter class in Kotlin

For a class with generic parameter T, you cannot do this because you have no type information for T since the JVM erases the type information. Therefore code like this cannot work: class Storage<T: Any> { val snapshot: Snapshot? = … fun retrieveSomething(): T? { return snapshot?.getValue(T::class.java) // ERROR “only classes can be used…” } … Read more

Cannot explicitly specialize a generic function

I also had this problem and I found a workaround for my case. In this article the author has the same problem https://www.iphonelife.com/blog/31369/swift-programming-101-generics-practical-guide So the problem seems to be, that the compiler needs to infer the type of T somehow. But it isn’t allowed to simply use generic< type >(params…). Normally, the compiler can look … Read more

Why are there no generics in Go? [duplicate]

Note: Generics were added to Go in version 1.18. You will find the answer here: http://golang.org/doc/faq#generics Why does Go not have generic types? Generics may well be added at some point. We don’t feel an urgency for them, although we understand some programmers do. Generics are convenient but they come at a cost in complexity … Read more

How to use TypeToken + generics with Gson in Kotlin

Create this inline fun: inline fun <reified T> Gson.fromJson(json: String) = fromJson<T>(json, object: TypeToken<T>() {}.type) and then you can call it in this way: val turns = Gson().fromJson<Turns>(pref.turns) // or val turns: Turns = Gson().fromJson(pref.turns) Previous Alternatives: ALTERNATIVE 1: val turnsType = object : TypeToken<List<Turns>>() {}.type val turns = Gson().fromJson<List<Turns>>(pref.turns, turnsType) You have to put … Read more

Why doesn’t the example compile, aka how does (co-, contra-, and in-) variance work?

Generically, a covariant type parameter is one which is allowed to vary down as the class is subtyped (alternatively, vary with subtyping, hence the “co-” prefix). More concretely: trait List[+A] List[Int] is a subtype of List[AnyVal] because Int is a subtype of AnyVal. This means that you may provide an instance of List[Int] when a … Read more

What is out keyword in kotlin

List<out T> in Kotlin is equivalent to List<? extends T> in Java. List<in T> in Kotlin is equivalent to List<? super T> in Java For example in Kotlin you can do things like val value : List<Any> = listOf(1,2,3) //since List signature is List<out T> in Kotlin The reasoning is you can mark generic “out” … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)