Skip to content
This repository has been archived by the owner on Sep 17, 2019. It is now read-only.

Commit

Permalink
M14 code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
svtk committed Sep 28, 2015
1 parent 43b8364 commit ae8c342
Show file tree
Hide file tree
Showing 12 changed files with 41 additions and 41 deletions.
4 changes: 2 additions & 2 deletions src/i_introduction/_7_Data_Classes/DataClasses.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ fun useFromJava() {
class Person2(_name: String, _age: Int) { //_name, _age are constructor parameters
val name: String = _name //property initialization is part of the constructor
get(): String {
return $name // You can access the backing field of property with '$' + property name
return field // You can access the backing field of property using 'field'
}

val age: Int = _age
get(): Int {
return $age
return field
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/i_introduction/_8_Extension_Functions/JavaCode8.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package i_introduction._8_Extension_Functions;

import i_introduction._8_Extension_Functions.StringExtensions.StringExtensionsPackage;
import i_introduction._8_Extension_Functions.StringExtensions.ExtensionFunctionsKt;
import util.JavaCode;

public class JavaCode8 extends JavaCode {
public void useExtension() {
char c = StringExtensionsPackage.lastChar("abc");
char c = ExtensionFunctionsKt.lastChar("abc");
}
}
2 changes: 1 addition & 1 deletion src/ii_conventions/_11_Comparison_.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fun compareStrings(s1: String?, s2: String?) {
}

interface B {
fun compareTo(other: B): Int
operator fun compareTo(other: B): Int
}

fun test(b1: B, b2: B) {
Expand Down
2 changes: 1 addition & 1 deletion src/ii_conventions/_14_InRange.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package ii_conventions
import util.TODO

interface Container<E> {
fun contains(element: E): Boolean
operator fun contains(element: E): Boolean
}

fun inConvention(container: Container<String>) {
Expand Down
6 changes: 3 additions & 3 deletions src/ii_conventions/_15_OperatorsOverloading.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ fun infixNotation() {
// Predefined operators can be overloaded by the corresponding names.
// A complete list of operators can be found in the syntax/OperatorOverloading.kt file.
interface A {
fun plus(a: A): A
fun times(a: A): A
fun not(): A
operator fun plus(a: A): A
operator fun times(a: A): A
operator fun not(): A
}

fun use(a1: A, a2: A) {
Expand Down
4 changes: 2 additions & 2 deletions src/ii_conventions/_16_MultiAssignment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ fun multiAssignPair(pair: Pair<Int, String>) {
}

class MyPair {
fun component1(): Int = 1
fun component2(): String = "a"
operator fun component1(): Int = 1
operator fun component2(): String = "a"
}

fun howItWorks() {
Expand Down
6 changes: 3 additions & 3 deletions src/ii_conventions/_17_Invoke_.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package ii_conventions
import util.TODO

interface My {
fun invoke(i: Int)
operator fun invoke(i: Int)
}

// Objects with 'invoke' function can by invokable as a function
// Objects with 'invoke' function marked as 'operator' can by invokable as a function
fun testTypeWithInvokeMember(my: My) {
my(1)

Expand All @@ -24,7 +24,7 @@ fun testFunctionType(f: (Int) -> Int) {

// You can add an 'invoke' extension for any class,
// but it's better not to overdo it
fun Int.invoke() { println(this) }
operator fun Int.invoke() { println(this) }

fun testTypeWithInvokeExtension() {
1() //huh?..
Expand Down
6 changes: 3 additions & 3 deletions src/iii_properties/_18_Properties_.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ fun usage(sp: SimpleProperty) {

class PropertiesWithCustomAccessors {
var generatedByDefault: Int = 0
set(value: Int) { $generatedByDefault = value }
get() = $generatedByDefault
set(value: Int) { field = value }
get() = field

val propertyWithoutBackingField: Int
get() = 42

val infiniteRecursion: Int? = Random().nextInt()
get() = if ($infiniteRecursion!! < 42) null else infiniteRecursion
get() = if (field!! < 42) null else infiniteRecursion
}

class PropertyExample() {
Expand Down
40 changes: 20 additions & 20 deletions src/syntax/operatorOverloading.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package syntax.operatorOverloading
fun compareStrings(s1: String?, s2: String?) {
s1 == s2
// is compiled to
s1?.equals(s2) ?: s2.identityEquals(null)
s1?.equals(s2) ?: s2 === null
}

interface C {
fun compareTo(other: C): Int
operator fun compareTo(other: C): Int
}

fun test(c1: C, c2: C) {
Expand All @@ -23,19 +23,19 @@ fun test(c1: C, c2: C) {
interface A
interface B {
//unary operations
fun plus()
fun minus()
operator fun plus()
operator fun minus()

fun inc(): B
fun dec(): B
operator fun inc(): B
operator fun dec(): B

//binary operations
fun plus(a: A): B
fun minus(a: A): B
fun times(a: A): B
fun div(a: A): B
fun mod(a: A): B
fun rangeTo(a: A): B
operator fun plus(a: A): B
operator fun minus(a: A): B
operator fun times(a: A): B
operator fun div(a: A): B
operator fun mod(a: A): B
operator fun rangeTo(a: A): B
}

@Suppress("UNUSED_CHANGED_VALUE", "UNUSED_VALUE")
Expand All @@ -60,11 +60,11 @@ fun binaryAndUnaryOperations(a: A, b: B) {
}

interface D {
fun plusAssign(a: A)
fun minusAssign(a: A)
fun timesAssign(a: A)
fun divAssign(a: A)
fun modAssign(a: A)
operator fun plusAssign(a: A)
operator fun minusAssign(a: A)
operator fun timesAssign(a: A)
operator fun divAssign(a: A)
operator fun modAssign(a: A)
}

fun assignmentOperations(d: D, a: A) {
Expand All @@ -74,7 +74,7 @@ fun assignmentOperations(d: D, a: A) {
}

interface MyCollection<E> {
fun contains(e: E): Boolean
operator fun contains(e: E): Boolean
}

fun conventionForIn(c: MyCollection<A>, a: A) {
Expand All @@ -88,8 +88,8 @@ fun conventionForIn(c: MyCollection<A>, a: A) {
}

interface MyMap<K, V> {
fun get(k: K): V
fun set(k: K, v: V)
operator fun get(k: K): V
operator fun set(k: K, v: V)
}

fun conventionForGet(map: MyMap<A, B>, a: A, b: B) {
Expand Down
4 changes: 2 additions & 2 deletions src/syntax/properties.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ class A {
var propertyWithCustomAccessors: Int = 1
set(v: Int) {
println("setter")
$propertyWithCustomAccessors = v
field = v
}
get() {
println("getter")
return $propertyWithCustomAccessors
return field
}

val propertyWithoutBackingField: Int
Expand Down
2 changes: 1 addition & 1 deletion test/ii_conventions/_11_Comparison.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ class _11_Comparison {
Assert.assertTrue("The date ${first.s} should be after ${second.s}", first > second)
}

fun MyDate.compareTo(other: MyDate): Int = todoTask11()
operator fun MyDate.compareTo(other: MyDate): Int = todoTask11()
}
2 changes: 1 addition & 1 deletion test/ii_conventions/_17_Invoke.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ class _17_Invoke {
testInvokable(0) { it }
}

fun Invokable.invoke() = todoTask17()
operator fun Invokable.invoke() = todoTask17()
fun Invokable.getNumberOfInvocations() = todoTask17()
}

0 comments on commit ae8c342

Please sign in to comment.