Is there any difference between empty block and UnitTry-with-resources in KotlinIn Kotlin, what is the idiomatic way to deal with nullable values, referencing or converting themKotlin - Property initialization using “by lazy” vs. “lateinit”Coding convention for empty functions that need to be overridden in KotlinUnion type between Any? and Unitwhat is `open` keyword for fields in Kotlin?Simplifying return from multiple nested blocks in KotlinWhat's the difference between HashMap and MutableMap in KotlinWhat is the difference between not-null checks in Kotlin?How and when does kotlin let run?
First Number to Contain Each Letter
Has Rey's new lightsaber been seen before in canon or legends?
How does Harry wear the invisibility cloak?
What is this red bug infesting some trees in southern Germany?
Does immunity to damage from nonmagical attacks negate a rogue's Sneak Attack damage?
Why do we need explainable AI?
Time to call the bluff
Confusion in understanding control system?
How does speed affect lift?
How to generate all 3×3 matrices with a,a,a,a,b,b,b,c,c?
Which is the best password hashing algorithm in .NET Core?
Spare the Dying during Rage Beyond Death
Punishment in pacifist society
Is it rude to ask my opponent to resign an online game when they have a lost endgame?
Can I sleep overnight at Stansted Airport
How to find better food in airports
'This one' as a pronoun
Tiny image scraper for xkcd.com
Planet that’s 90% water or more?
Does this bike use hydraulic brakes?
Case Studies and Real Problems for Teaching Optimization and Modelling
Why did the Joi advertisement trigger K?
How to anonymously report the Establishment Clause being broken?
Everyone for non livings
Is there any difference between empty block and Unit
Try-with-resources in KotlinIn Kotlin, what is the idiomatic way to deal with nullable values, referencing or converting themKotlin - Property initialization using “by lazy” vs. “lateinit”Coding convention for empty functions that need to be overridden in KotlinUnion type between Any? and Unitwhat is `open` keyword for fields in Kotlin?Simplifying return from multiple nested blocks in KotlinWhat's the difference between HashMap and MutableMap in KotlinWhat is the difference between not-null checks in Kotlin?How and when does kotlin let run?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Is there any difference between empty block and returning Unit?
for example, we should make empty block to method do nothing
void ThisMethodDoesNothing()
In kotlin, we can write code above but this way
fun ThisMethodDoesNothing() = Unit
Please let me know if this is duplicated, thanks.
kotlin
add a comment |
Is there any difference between empty block and returning Unit?
for example, we should make empty block to method do nothing
void ThisMethodDoesNothing()
In kotlin, we can write code above but this way
fun ThisMethodDoesNothing() = Unit
Please let me know if this is duplicated, thanks.
kotlin
add a comment |
Is there any difference between empty block and returning Unit?
for example, we should make empty block to method do nothing
void ThisMethodDoesNothing()
In kotlin, we can write code above but this way
fun ThisMethodDoesNothing() = Unit
Please let me know if this is duplicated, thanks.
kotlin
Is there any difference between empty block and returning Unit?
for example, we should make empty block to method do nothing
void ThisMethodDoesNothing()
In kotlin, we can write code above but this way
fun ThisMethodDoesNothing() = Unit
Please let me know if this is duplicated, thanks.
kotlin
kotlin
asked Mar 28 at 3:25
SlaneRSlaneR
4393 silver badges13 bronze badges
4393 silver badges13 bronze badges
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I figured that the two probably did the same thing, but I had no way to be sure. I really wanted to know. So I created a class to put each of these in (since Java must have a class around any code), changed the Kotlin version so that the attributes on the method were the same, and that gave me this:
a.java
public class a
public final void ThisMethodDoesNothing()
b.kt
class b
fun ThisMethodDoesNothing() = Unit
Then I compiled each of these, and then decompiled them with javad -c
, and got:
> javap -c com.inlet.ifserver.a
Compiled from "a.java"
public class com.inlet.ifserver.a
public com.inlet.ifserver.a();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public final void ThisMethodDoesNothing();
Code:
0: return
> javap -c com.inlet.ifserver.b
Compiled from "b.kt"
public final class com.inlet.ifserver.b
public final void ThisMethodDoesNothing();
Code:
0: return
public com.inlet.ifserver.b();
Code:
0: aload_0
1: invokespecial #11 // Method java/lang/Object."<init>":()V
4: return
So I think the answer is a resounding YES! Both do absolutely nothing.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55389695%2fis-there-any-difference-between-empty-block-and-unit%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I figured that the two probably did the same thing, but I had no way to be sure. I really wanted to know. So I created a class to put each of these in (since Java must have a class around any code), changed the Kotlin version so that the attributes on the method were the same, and that gave me this:
a.java
public class a
public final void ThisMethodDoesNothing()
b.kt
class b
fun ThisMethodDoesNothing() = Unit
Then I compiled each of these, and then decompiled them with javad -c
, and got:
> javap -c com.inlet.ifserver.a
Compiled from "a.java"
public class com.inlet.ifserver.a
public com.inlet.ifserver.a();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public final void ThisMethodDoesNothing();
Code:
0: return
> javap -c com.inlet.ifserver.b
Compiled from "b.kt"
public final class com.inlet.ifserver.b
public final void ThisMethodDoesNothing();
Code:
0: return
public com.inlet.ifserver.b();
Code:
0: aload_0
1: invokespecial #11 // Method java/lang/Object."<init>":()V
4: return
So I think the answer is a resounding YES! Both do absolutely nothing.
add a comment |
I figured that the two probably did the same thing, but I had no way to be sure. I really wanted to know. So I created a class to put each of these in (since Java must have a class around any code), changed the Kotlin version so that the attributes on the method were the same, and that gave me this:
a.java
public class a
public final void ThisMethodDoesNothing()
b.kt
class b
fun ThisMethodDoesNothing() = Unit
Then I compiled each of these, and then decompiled them with javad -c
, and got:
> javap -c com.inlet.ifserver.a
Compiled from "a.java"
public class com.inlet.ifserver.a
public com.inlet.ifserver.a();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public final void ThisMethodDoesNothing();
Code:
0: return
> javap -c com.inlet.ifserver.b
Compiled from "b.kt"
public final class com.inlet.ifserver.b
public final void ThisMethodDoesNothing();
Code:
0: return
public com.inlet.ifserver.b();
Code:
0: aload_0
1: invokespecial #11 // Method java/lang/Object."<init>":()V
4: return
So I think the answer is a resounding YES! Both do absolutely nothing.
add a comment |
I figured that the two probably did the same thing, but I had no way to be sure. I really wanted to know. So I created a class to put each of these in (since Java must have a class around any code), changed the Kotlin version so that the attributes on the method were the same, and that gave me this:
a.java
public class a
public final void ThisMethodDoesNothing()
b.kt
class b
fun ThisMethodDoesNothing() = Unit
Then I compiled each of these, and then decompiled them with javad -c
, and got:
> javap -c com.inlet.ifserver.a
Compiled from "a.java"
public class com.inlet.ifserver.a
public com.inlet.ifserver.a();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public final void ThisMethodDoesNothing();
Code:
0: return
> javap -c com.inlet.ifserver.b
Compiled from "b.kt"
public final class com.inlet.ifserver.b
public final void ThisMethodDoesNothing();
Code:
0: return
public com.inlet.ifserver.b();
Code:
0: aload_0
1: invokespecial #11 // Method java/lang/Object."<init>":()V
4: return
So I think the answer is a resounding YES! Both do absolutely nothing.
I figured that the two probably did the same thing, but I had no way to be sure. I really wanted to know. So I created a class to put each of these in (since Java must have a class around any code), changed the Kotlin version so that the attributes on the method were the same, and that gave me this:
a.java
public class a
public final void ThisMethodDoesNothing()
b.kt
class b
fun ThisMethodDoesNothing() = Unit
Then I compiled each of these, and then decompiled them with javad -c
, and got:
> javap -c com.inlet.ifserver.a
Compiled from "a.java"
public class com.inlet.ifserver.a
public com.inlet.ifserver.a();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public final void ThisMethodDoesNothing();
Code:
0: return
> javap -c com.inlet.ifserver.b
Compiled from "b.kt"
public final class com.inlet.ifserver.b
public final void ThisMethodDoesNothing();
Code:
0: return
public com.inlet.ifserver.b();
Code:
0: aload_0
1: invokespecial #11 // Method java/lang/Object."<init>":()V
4: return
So I think the answer is a resounding YES! Both do absolutely nothing.
answered Mar 28 at 4:10
SteveSteve
4,8101 gold badge8 silver badges31 bronze badges
4,8101 gold badge8 silver badges31 bronze badges
add a comment |
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55389695%2fis-there-any-difference-between-empty-block-and-unit%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown