Kotlin - Function's Invoke Operator Overloading - operator-overloading

I am currently studying Kotlin - Operator Overloading
I am trying to understand (with an example) how does operator overloading work for a function's invoke() function
The pre-test
Kotlin's Extension Functions
fun exampleOfExtensionFunction() {
fun Int.randomize(): Int {
return Random(this.toLong()).nextInt()
}
val randomizedFive = 5.randomize()
println("$randomizedFive")
}
Prints :
-1157408321
In Kotlin, functions can be declared as variables with types
fun exampleOfFunctionType() {
val printNumber: (number: Int) -> Unit
printNumber = { number ->
println("[$number = ${number.toString(16).toUpperCase()} = ${number.toString(2)}]")
}
printNumber(1023)
}
Prints :
[1023 = 3FF = 1111111111]
Kotlin allows operator overloading with both extension and member functions
fun exampleOfOperatorOverloadingUsingExtensionFunction() {
class MyType() {
val strings: ArrayList<String> = ArrayList<String>()
override fun toString(): String {
val joiner: StringJoiner = StringJoiner(" , ", "{ ", " }")
for (string in strings) {
joiner.add("\"$string\"")
}
return joiner.toString()
}
}
operator fun MyType.contains(stringToCheck: String): Boolean {
for (stringElement in strings) {
if (stringElement == stringToCheck) return true
}
return false
}
val myType = MyType()
myType.strings.add("one")
myType.strings.add("two")
myType.strings.add("three")
println("$myType")
println("(myType.contains(\"four\")) = ${myType.contains("four")} , (\"three\" in myType) = ${"three" in myType}")
}
Prints :
{ "one" , "two" , "three" }
(myType.contains("four")) = false , ("three" in myType) = true
Test Attempt
Based on the above. I tried to create an example of a function's invoke() operator overloading using the type (Boolean, Boolean, Boolean) -> Boolean as a receiver type for an extension function invoke(flag1: Boolean, flag2: Boolean, flag3: Boolean). This however did not work as expected.
fun attemptFunctionInvokeOperatorOverloading() {
operator fun ((Boolean, Boolean, Boolean) -> Boolean).invoke(flag1: Boolean, flag2: Boolean, flag3: Boolean): Boolean {
println("Overloaded invoke operator")
return flag1 && flag2 && flag3
}
var func1: ((Boolean, Boolean, Boolean) -> Boolean) = { flag1, flag2, flag3 ->
println("func1 body")
flag1 && flag2 && flag3
}
fun func2(flag1: Boolean, flag2: Boolean, flag3: Boolean): Boolean {
println("func2 body")
return flag1 && flag2 && flag3
}
func1(true, true, false)
func2(true, true, true)
}
Prints :
func1 body
func2 body
Expected :
Overloaded invoke operator
Overloaded invoke operator
Another question :
What exactly is this? (If it isn't operator overloading)
operator fun ((Boolean, Boolean, Boolean) -> Boolean).invoke(flag1: Boolean, flag2: Boolean, flag3: Boolean): Boolean {
println("Overloaded invoke operator")
return flag1 && flag2 && flag3
}

As said in another answer, invoke is defined on the function object itself, so you can't override it with an extension method.
I think the deeper problem here, though, is a slight misunderstanding of the purpose of this feature. Let's look at plus for the + operator instead.
I think you'll agree that attempting to define operator fun Int.plus(b: Int): Int { /* ... */} makes no sense, because overriding the default + operator for ints is a pretty dangerous thing to do, yes?
However, if you define a complex number class:
data class Complex(real: Double, img: Double)
Then it's perfectly reasonable to define this to sum complex numbers:
operator fun Complex.plus(other: Complex): Complex {
val real = this.real + other.real
val img = this.img + other.img
return Complex(real, img)
}
So, same thing with invoke and (): The meaning of () is that it's whatever is analogous to invoking a function for your type, and overriding invoke on something that's already a function is just asking for trouble. What you want to use it for instead is to provide function-like syntax for your own objects.
For example, imagine you define an interface like so:
interface MyCallback {
fun call(ctx: MyContext)
}
Which you use the usual way:
callback.call(ctx)
But with an implementation of the invoke operator overload, you get to use it as a function:
operator fun MyCallback.invoke(ctx: Context) = this.call(ctx)
/* Elsewhere... */
callback(ctx)
Hope that clarifies how you use invoke/()

Your issue has to to with resolution precedence. According to the Kotlin docs:
If a class has a member function, and an extension function is defined which has the same receiver type, the same name and is applicable to given arguments, the member always wins.
So your operator fun ((Boolean, Boolean, Boolean) -> Boolean).invoke(...) extension function is never called because the member invoke takes precedence.
Another Answer
It is indeed operator overloading, but through an extension. Again, since (Boolean, Boolean, Boolean) -> Boolean already has a fun invoke(Boolean, Boolean, Boolean): Boolean defined, your extension loses.

Related

kotlin's javaclass.isPrimitive fails if parameter compile-time is not actual type

The following code
fun foo(value:Double) {
if(!value.javaClass.isPrimitive) {
println("try again")
return
}
}
println("that's nice")
}
fun main() {
foo(0.0)
}
displays:
"that's nice"
but setting value type as Any:
fun foo(value:Any) {
if(!value.javaClass.isPrimitive) {
println("try again")
return
}
println("that's nice")
}
fun main() {
foo(0.0)
}
will display: "try again"
even though valueruntime type is Double,
link for testing : https://pl.kotl.in/HkghkAkF4
quote from https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.jvm/java-class.html:
inline val T.javaClass: Class
Returns the runtime Java class of this object
which from https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#isPrimitive should give me right
EDIT: removed unnecessary run{}
When you declare the type as Double, that's a primitive double (rather than the Double wrapper class).
When you declare the type as Any, the method then accepts any object as a parameter, so even if you pass in a primitive double, it will be auto-boxed into a wrapper object for Double and will no longer be a primitive.
You can see this by running this snippet on play.kotlinlang.org:
fun main() {
useDouble(3.0)
useAny(3.0)
}
fun useDouble(value: Double) = println("${value.javaClass.name}")
fun useAny(value: Any) = println("${value.javaClass.name}")
Prints:
double
java.lang.Double
isn't there a method like "isPrimitive" for wrapper classes
Not directly, but you can check whether a value belongs to a wrapper class by
value::class.javaPrimitiveType != null
Or if you just have a clazz: Class<T>,
clazz.kotlin.javaPrimitiveType != null

How to initialize a field by calling a method

The following class refuses to compile:
class InitTest { // Class 'InitTest' must either be declared abstract
// or implement abstract member 'v: Int'
var v: Int
def int(v : Int) = {
this.v = v
}
}
I was kind of surprise by that we can't just leave values "uninitialized". In Java, it would be assigned with null. In Scala, it does not compile. How to do this in Scala?
You can do this:
class InitTest {
var v: Int = _
def int(v : Int) = {
this.v = v
}
}
Since v has a value type, there is no way of assigning null to it. However, Scala lets you use _ to represent the "zeroed" value. For numbers, that is zero and for pointers that is null. Good way of representing uninitialized values.

Swift 2.0 Generics and Type Safety Issues

I was doing this tutorial http://blog.teamtreehouse.com/introduction-learn-power-swift-generics and I came upon this code;
func someFunction<T, U>(a: T, b: U) {}
The problem is when I call the function using
someFunction<String, Int>(1, "Test")
I get an error saying "cannot explicitly specialize a generic function".
I then change it to
someFunction(1,b: "Test")
and now there is no error. The problem is that there is now no type safety. (Is there something wrong with the code, as it was written before swift 2.0?) What is the best way to re-introduce type safety?
The declaration is completely generic and is specifying that any two types can be used.
func someFunction<T, U>(a: T, b: U) {}
It is not that there is no type safety in Swift, this is how you express a generic without any type constraints.
You get what you ask for.
If you wanted to constrain the function to String and Int, you would have written it as
func someFunction(a:String, b:Int)
Generics are more often used with collections, protocols and classes. Basic types rarely need them :
func someFunction<T:Comparable, U:Comparable>(a:T, b:U) -> Bool
{ return (a < b) || (a > b) }
ok, see this 'self explanatory' example. try it in playground and play with it a little bit.
func foo<T>(t: T)->T {
return t
}
// here the resulting type is infered by compiler
let i = foo(1) // 1
let j: Int = foo(1) // 1
let t = foo("alpha") // "alpha"
// if you declare it self ..
//let s: String = foo(1) // error: cannot convert value of type 'Int' to expected argument type 'String'
/* this fails to compile!
func bar<T>(t:T)->Int {
return t.count
}
*/
/* this fails to compile too !!
func bar0<T:CollectionType>(t:T)->Int {
return t.count
}
*/
func bar<T:CollectionType>(t:T)->T.Index.Distance {
return t.count
}
let arr = [1,2,3]
let k:Int = bar(arr) // 3
print(k.dynamicType) // Int
// and even more interesting
let l = bar(arr)
print(l.dynamicType) // Int

Swift - Assigning Overloaded Function to Variable

I am getting a compile time error that myFunc reference is ambiguous.
func f (s: String) -> String { return "version 1: " + s }
func f(sourceString s: String) -> String { return "version 2: " + s }
var myFunc: (String)-> String = f as (sourceString : String)->String
How can I explicitly reference each version of the overloaded function, f, in the example above? If I comment out either declaration of func f it will compile and work. But I would like to know how to reference each of the functions if both are declared. Thanks.
I don't know how to do exactly what you want, but maybe this helps:
var myFunc1: (String)-> String = { s in f(sourceString: s) }
var myFunc2: (String)-> String = { s in f(s) }
You can now call:
let s1 = myFunc1("one") // returns "version 2: one"
let s2 = myFunc2("two") // returns "version 1: two"
Interesting one this. I don’t think it’s possible without doing something along the lines of #marcos’s suggestion. The problem you is you can “cast away” the names in tuples:
let named_pair = (s: "hello", i: 1)
named_pair.s // hello
let anon_pair = named_pair as (String,Int)
// or anon_pair: (String,Int) = named_pair, if you prefer
anon_pair.s // no such member 's'
Now suppose you define two functions, identical except one has named arguments:
func f(s: String, i: Int) { println("_: \(s)") }
func f(#s: String, #i: Int) { println("s: \(s)") }
You can then call it via tuples with named vs unnamed arguments:
f(named_pair) // prints s: hello
f(anon_pair) // prints _: hello
// but if you try to call a named argument function with unnamed tuples:
func g(# s: String, # i: Int) { println("s: \(s)") }
g(anon_pair) // compiler error
let h = g
h(anon_pair) // compiler error
h(named_pair) // works
But because you can cast away these names you can do this:
// compiles and runs just fine...
(g as (String,Int)->())(anon_pair)
let k: (String,Int)->() = g
// as does this
k(anon_pair)
And this ability to do this means it’s not possible to use a type to disambiguate an function overloaded only by argument names, as far as I can tell.
Referencing func f (s: String) -> String { return "version 1: " + s }:
let myFunction = f(s:)
Referencing func f(sourceString s: String) -> String { return "version 2: " + s }:
let myFunction = f(sourceString:)
Referencing func anotherFunction(_ param: Any) {}:
let myFunction = anotherFunction(_:)
If you haven't overloaded the function, you don't need to explicity write out the parameter names when referencing the function.
Number of arguments should vary.
If the number of arguments are same then their data types should
vary.
Example
func f(x : String) -> NSString {
return a
}
func f(x : UInt) -> NSString {
return "{\(x)}"
}
I don't think you can. You can call one or the other:
println(f("test")) // version 1: test
println(f(sourceString: "test")) // version 2: test

Swift functions accepting tuples

Is it possible to pass in a tuple into a function as long as their types match up?
When I try it, I get a missing argument in parameter error:
var myTuple = ("Text",10,"More Text")
func myFunction(a:String, b:Int, c:String) {
// etc...
}
myFunction(myTuple)
It was possible, although was deprecated in Swift 2.2:
In Swift 2.1 and earlier it was possible to use a carefully crafted tuple to fill the parameters of a function. So, if you had a function that took two parameters, you could call it with a two-element tuple as long as the tuple had the correct types and element names.
...
This syntax — affectionately called “tuple splat syntax” — is the antithesis of idiomatic Swift’s self-documenting, readable style, and so it’s deprecated in Swift 2.2.
https://swift.org/blog/swift-2-2-new-features/
I came here wanting to know how to pass a tuple as a function parameter. The answers here focus on a different case. I'm not entirely clear what the OP was after.
In any case, here is how to pass a tuple as a parameter. And, for good measure, how to do it variadically.
func acceptTuple(tuple : (Int, String)) {
print("The Int is: \(tuple.0)")
print("The String is '\(tuple.1)'")
}
acceptTuple((45, "zebras"))
// Outputs:
// The Int is: 45
// The String is 'zebras'
func acceptTuples(tuples : (Int, String) ...) {
var index = 0
// note: you can't use the (index, tuple) pattern in the for loop,
// the compiler thinks you're trying to unpack the tuple, hence
/// use of a manual index
for tuple in tuples {
print("[\(index)] - Int is: \(tuple.0)")
print("[\(index)] - String is '\(tuple.1)'")
index++
}
}
acceptTuples((45, "zebras"), (17, "armadillos"), (12, "caterpillars"))
//Outputs
//[0] - Int is: 45
//[0] - String is 'zebras'
//[1] - Int is: 17
//[1] - String is 'armadillos'
//[2] - Int is: 12
//[2] - String is 'caterpillars'
Passing tuples in can be a quick and convenient approach, saving you from having to create wrappers etc. For example, I have a use case where I am passing a set of tokens and parameters to create a game level. Tuples makes this nice and compact:
// function signature
class func makeLevel(target: String, tokens: (TokenType, String)...) -> GameLevel
// The function is in the class Level. TokenType here is an Enum.
// example use:
let level = Level("Zoo Station", tokens:
(.Label, "Zebra"),
(.Bat, "LeftShape"),
(.RayTube, "HighPowered"),
(.Bat, "RightShape"),
(.GravityWell, "4"),
(.Accelerator, "Alpha"))
Yes, it's possible under these conditions:
the tuple must be immutable
the number of values in the tuple, their type, and their order must match the parameters expected by the function
named parameters must match external names in the function signature
non-named parameters must match parameters without external name in the function signature
So, your code is ok, the only thing you have to do is turning the tuple into an immutable one (i.e. using let and not var):
let myTuple = ("Text", 10, "More Text")
func myFunction(a:String, b:Int, c:String) {
// etc...
}
myFunction(myTuple)
One more example with external names:
let myTuple = ("Text", paramB: 10, paramC: "More Text")
func myFunction(a:String, paramB b:Int, paramC c:String) {
// etc...
}
myFunction(myTuple)
In your tuple, it appears as though you must name them and then refer to them as such:
so your code should be
var myTuple = (val1: "Text", val2: 10, val3: "More Text")
func myFunction(a:String, b:Int, c:String) {
// etc...
}
myFunction(myTuple.val1, myTuple.val2, myTuple.val3)
The tuple has named values (val1, val2, val3) which you set and then reference, when you pass in myTuple, to the function myFunction(), it appears as though you are just filling 1 of the 3 available arguements - and with the wrong type to boot! This is the equivalent of storing the types in a tuple, then taking them out for a function call. However, if you want a function to actually take a tuple as a parameter, see below:
var myTuple = (val1: "Text", val2: 10, val3: "More Text")
func tupleFunc(a:(String, Int, String)) {
}
tupleFunc(myTuple)
Yes, but that's the wrong structure: you're passing three variables called a, b, and c rather than a tuple with those components.
You need parentheses around the whole thing:
var myTuple = ("Text", 10, "More Text")
func myFunction(a:(x: String, y: Int, z: String)) {
println(a)
}
myFunction(myTuple)
You can use the following feature: Swift allows you to pass a function (f1) with any number of parameters (but without inout parameters) as a parameter of type (TIn) -> TOut to another function. In this case, TIn will represent a tuple from the parameters of the function f1:
precedencegroup ApplyArgumentPrecedence {
higherThan: BitwiseShiftPrecedence
}
infix operator <- :ApplyArgumentPrecedence
func <-<TIn, TOut>(f: ((TIn) -> TOut), arg: TIn) -> TOut {
return f(arg)
}
func sum(_ a: Int, _ b: Int) -> Int {
return a + b
}
print(sum <- (40, 2))
In swift 3.0, we should not able to pass the tuple directly to the function.If we did so, it shows the error message as "This type has been removed in swift 3.0"
func sum(x: Int, y: Int) -> Int
return x+y }
let params = (x: 1, y: 1)
let x = params.0
let y = params.1
sum(x: x, y: y)
Hope it helps you!!
The best option for now seems to be to just save it to a compound variable or use the build in dot syntax
let (val1, val2) = (1, 2)
func f(first: Int, second: Int) { }
f(first: val1, second: val2)
let vals = (1, 2)
f(first: vals.0, second: vals.1)
That feature called implicit tuple splat was removed in swift 3.
You can find more detailed explanation on the removal proposal here
Some suggestions to keep using tuple as an argument is by doing so:
func f1(_ a : (Int, Int)) { ... }
let x = (1, 2)
f1(x)
func f2<T>(_ a : T) -> T { ... }
let x = (1, 2)
f2(x)