当前位置:网站首页>[kotlin puzzle] what happens if you overload an arithmetic operator in the kotlin class and declare the operator as an extension function?
[kotlin puzzle] what happens if you overload an arithmetic operator in the kotlin class and declare the operator as an extension function?
2022-07-03 09:22:00 【Android old monkey】
stay Kotlin Class overloads an arithmetic operator , And declare the operator as an extension function ?
example

Call at this time ’+' Operator

An error is as follows

Interpret errors Because the receiver type cannot be matched, the applicable... Cannot be found from the candidate methods
Analyze errors
Analyze three different ways of overloading plus function
The first one is : Overload in the form of standard extension functions plus function

hold kotlin Decompile the code into java Code
After decompilation, we can find that plus Function in PointKt Class , This is also in line with what we said in the previous chapter “ Extension functions are not part of a class , It's declared outside the class ”, stay java In, it declares this function in a class with similar class names , And it's a static function . As an extension function plus There are two parameters ,kotlin Calling ’+' At the time java Is compiled into the following figure
The second kind : Overloaded in the form of standard member functions plus function
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-1Pu5hK0N-1654782243961)(https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/933eddfdc1b446388a393590ce56407d~tplv-k3u1fbpfcp-zoom-in-crop-mark:1956:0:0:0.image?)]
hold kotlin Decompile the code into java Code

After decompilation, we can find that as a member function plus Now there is only one parameter , and kotlin Calling ’+' At the time java Is compiled into the following figure

The third kind of : The member function form is mixed with the extension function form

hold kotlin Decompile the code into java Code

General analysis
The first two forms are no problem , Let's look at the third usage . Put the third kind of java The code is the same as the first two java Combined with the code, we can find out some connections and differences between them
Connection and difference
It's the same as the second one plus The same function is Point Class member functions , But there are two parameters It's the same as the first one plus Like a function, it has two parameters , But none of the first plus Functional static Statement
The third compiled function is like stitching the writing methods of the first two functions . Previously, we have given the first and second methods in calling plus What a function looks like , And we can also infer the appearance of the third call :point.plus(?,new Point(3,4)). At this point, I was thinking that since the error prompts us that the receiver type cannot match , So why can't the compiler pass itself into the function as an argument ? I can try to write an operator overloaded with two parameters plus Function? ?
give the result as follows
stay kotlin This arithmetic operator can only have one parameter
I guess kotlin When calling arithmetic operators in the compiler , If it is a member function, it will only provide the writing method of one parameter , If it is an extension function, use the writing method of providing two parameters . The third way of writing plus Function is both a member function and requires two parameters , This may be the reason why the third writing method cannot run successfully
from equals Method we can see that it inherits from Any Class implementation always takes precedence over extension functions , And our situation, though not inherited from Any class , But is there a priority difference ?
The above is just my own idea , If you have different opinions, you can discuss , If there are errors, please also point out
边栏推荐
- [set theory] order relation (chain | anti chain | chain and anti chain example | chain and anti chain theorem | chain and anti chain inference | good order relation)
- Matlab dichotomy to find the optimal solution
- 即时通讯IM,是时代进步的逆流?看看JNPF怎么说
- Tag paste operator (#)
- 图像修复方法研究综述----论文笔记
- 【Kotlin疑惑】在Kotlin类中重载一个算术运算符,并把该运算符声明为扩展函数会发生什么?
- LeetCode 30. Concatenate substrings of all words
- Linxu learning (4) -- Yum and apt commands
- Uc/os self-study from 0
- Jenkins learning (I) -- Jenkins installation
猜你喜欢
![[kotlin learning] classes, objects and interfaces - define class inheritance structure](/img/66/34396e51c59504ebbc6b6eb9831209.png)
[kotlin learning] classes, objects and interfaces - define class inheritance structure

网络安全必会的基础知识
![[point cloud processing paper crazy reading classic version 11] - mining point cloud local structures by kernel correlation and graph pooling](/img/40/e0c7bad60b19cafa467c229419ac21.png)
[point cloud processing paper crazy reading classic version 11] - mining point cloud local structures by kernel correlation and graph pooling

Crawler career from scratch (IV): climb the bullet curtain of station B through API

State compression DP acwing 91 Shortest Hamilton path

【Kotlin学习】高阶函数的控制流——lambda的返回语句和匿名函数
![[point cloud processing paper crazy reading frontier version 11] - unsupervised point cloud pre training via occlusion completion](/img/76/b92fe4549cacba15c113993a07abb8.png)
[point cloud processing paper crazy reading frontier version 11] - unsupervised point cloud pre training via occlusion completion

We have a common name, XX Gong

LeetCode 30. Concatenate substrings of all words

2022-1-6 Niuke net brush sword finger offer
随机推荐
[point cloud processing paper crazy reading classic version 9] - pointwise revolutionary neural networks
Crawler career from scratch (IV): climb the bullet curtain of station B through API
2022-2-13 learning xiangniuke project - version control
What is an excellent fast development framework like?
传统企业数字化转型需要经过哪几个阶段?
Vs2019 configuration opencv3 detailed graphic tutorial and implementation of test code
LeetCode 30. 串联所有单词的子串
We have a common name, XX Gong
[point cloud processing paper crazy reading cutting-edge version 12] - adaptive graph revolution for point cloud analysis
What are the stages of traditional enterprise digital transformation?
LeetCode 535. Encryption and decryption of tinyurl
LeetCode 1089. Duplicate zero
Serializer rewrite: update and create methods
【点云处理之论文狂读前沿版12】—— Adaptive Graph Convolution for Point Cloud Analysis
LeetCode 715. Range 模块
[point cloud processing paper crazy reading frontier version 10] - mvtn: multi view transformation network for 3D shape recognition
Construction of simple database learning environment
[advanced feature learning on point clouds using multi resolution features and learning]
LeetCode 438. Find all letter ectopic words in the string
【点云处理之论文狂读经典版11】—— Mining Point Cloud Local Structures by Kernel Correlation and Graph Pooling