/** Shifts this value left by [bits]. */
public infix fun shl(bitCount: Int): Int
/** Shifts this value right by [bits], filling the leftmost bits with copies of the sign bit. */
public infix fun shr(bitCount: Int): Int
/** Shifts this value right by [bits], filling the leftmost bits with zeros. */
public infix fun ushr(bitCount: Int): Int
/**
* Creates a tuple of type [Pair] from this and [that].
*
* This can be useful for creating [Map] literals with less noise, for example:
* @sample samples.collections.Maps.Instantiation.mapFromPairs
*/
public infix fun <A, B> A.to(that: B): Pair<A, B> = Pair(this, that)
/**
* Returns a set containing all elements that are contained by both this set and the specified collection.
*
* The returned set preserves the element iteration order of the original collection.
*/
public infix fun <T> Iterable<T>.intersect(other: Iterable<T>): Set<T> {
val set = this.toMutableSet()
set.retainAll(other)
return set
}
/**
* Returns a progression from this value down to the specified [to] value with the step -1.
*
* The [to] value has to be less than this value.
*/
public infix fun Short.downTo(to: Short): IntProgression {
return IntProgression.fromClosedRange(this.toInt(), to.toInt(), -1)
}
`複製代碼
從源碼中能夠看出 infix 的用法:當咱們定義一個類的函數或者擴展函數的時候,若是這個函數接受的參數和本身是同一類的,而且又反回值那麼就採用中綴表示法html
lambda 表達式java
//簡單改造view的setOnClickListenter
class Main2Activity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
var view = View()
view.setOnClickListenter {
it->it.toHome()
}
}
}
class View{
fun toHome() {
}
fun setOnClickListenter(listerner: (v:View) -> Unit) {
listerner(this);
}
}複製代碼
/**
* Calls the specified function [block] with the given [receiver] as its receiver and returns its result.
*/
@kotlin.internal.InlineOnly
public inline fun <T, R> with(receiver: T, block: T.() -> R): R = receiver.block()
/**
* Calls the specified function [block] with `this` value as its receiver and returns `this` value.
*/
@kotlin.internal.InlineOnly
public inline fun <T> T.apply(block: T.() -> Unit): T { block(); return this }
/**
* Calls the specified function [block] with `this` value as its argument and returns `this` value.
*/
@kotlin.internal.InlineOnly
@SinceKotlin("1.1")
public inline fun <T> T.also(block: (T) -> Unit): T { block(this); return this }複製代碼
然而這裏幾個函數在咱們項目中會常常用到,好比這樣:如今有一本書,我要更具他如今的價格來分類,分完之,還要的獲得這麼書的名字git
class Main2Activity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
var book = Book()
var name = book.apply {
var price = getPrice()
if (price < 50) {
Toast.makeText(this@Main2Activity,"小於50", Toast.LENGTH_LONG).show()//this@Main2Activity 是this表達式
type = "便宜書"
}else{
Toast.makeText(this@Main2Activity,"大於50", Toast.LENGTH_LONG).show()
type = "貴書"
}
}.getName()
}
}
class Book{
var type:String? = null
fun getName():String {
return "youxin"
}
fun getPrice():Int {
return 100
}
}複製代碼