/** 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
}
}复制代码