Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Catching Kotlin

Catching Kotlin

This talk was given at the Denver Droids Meetup.
http://www.meetup.com/DenverDroids/events/232743206/

Brandon Davis

August 17, 2016
Tweet

Other Decks in Programming

Transcript

  1. Features • Type inference • No primitives • “Object” is

    now “Any” • “void” is now “Unit” • Everything is final unless declared open • This causes problems with mocking for unit testing • https://github.com/nhaarman/mockito-kotlin
  2. no modifier is public private protected internal is package private

    Visibility Modifiers public private protected no modifier is package private
  3. Variables and Constants var i: Int = 0
 val j:

    Int = 0 int i = 0;
 final int j = 0;
  4. Variables and Constants var i = 0
 val j =

    0 int i = 0;
 final int j = 0;
  5. Methods fun isGreat(langName: String): Boolean {
 return isKotlin(langName)
 } public

    boolean isGreat(String langName) {
 return isKotlin(langName);
 }
  6. Methods fun updateTable(name: String, listener: NetworkListener = null) {
 //Do

    stuff
 } public void updateTable(String name) {
 updateTable(name, null);
 }
 
 public void updateTable(String name, NetworkListener listener) {
 //Do stuff
 }
  7. Classes class MainClass(type: String) : BaseClass {
 private val type:

    String 
 
 init {
 this.type = type
 //Do stuff
 }
 } public class MainClass extends BaseClass {
 private String type;
 
 public MainClass(String type) {
 this.type = type;
 //Do stuff
 }
 }
  8. Classes class MainClass(val type: String) : BaseClass {
 …
 }

    public class MainClass extends BaseClass {
 private String type;
 
 public MainClass(String type) {
 this.type = type;
 }
 }
  9. Classes class CustomView : View, MyInterface {
 
 constructor(ctx: Context?)

    : super(ctx)
 
 constructor(ctx: Context?, attrs: AttributeSet?) : super(ctx, attrs) {
 myMethod()
 }
 } public class CustomView extends View implements MyInterface {
 public CustomView(Context ctx) {
 super(ctx);
 }
 
 public CustomView(Context ctx, AttributeSet attrs) {
 super(ctx, attrs);
 myMethod();
 }
 }
  10. Expressions val value = if (isKotlin) {
 "Woohoo"
 } else

    {
 "Sad day"
 } String value;
 
 if (isKotlin()) {
 value = "Woohoo";
 } else {
 value = "Sad day";
 }
  11. Expressions val myInt: Int = try {
 parseInt(input)
 } catch

    (e: Exception) {
 0
 } int myInt;
 
 try {
 myInt = parseInt(input);
 } catch (Exception e) {
 myInt = 0;
 }
  12. Null Safety - ? var myString: String? = null
 myString?.replace("Foo",

    "Bar", true)
 
 var myOtherString = "FooBar"
 myOtherString.replace("Foo", "Bar", true) if (myString != null) {
 myString.replace("Foo", "Bar");
 }
  13. Null Safety - ?: val myString = otherString? "Foo" ?:

    "Bar" String myString;
 if (otherString != null) {
 myString = "Foo";
 } else {
 myString = "Bar";
 }
  14. Null Safety - let myString?.let {
 it.replace("Foo", "Bar")
 } if

    (myString != null) {
 myString.replace("Foo", "Bar");
 }
  15. Null Safety - !! var myString = null
 val length

    = myString!!.length String myString = null;
 int length = myString.length();
  16. Casts - as val activity = myClass as? Activity
 val

    resources = activity?.resources if (myClass instanceof Activity) {
 Resources resources = ((Activity) myClass).getResources();
 }
  17. Casts - is if (myClass is Activity) {
 val resources

    = myClass.resources
 } if (myClass instanceof Activity) {
 Resources resources = ((Activity) myClass).getResources();
 }
  18. Lambdas myButton.setOnClickListener(object : OnClickListener {
 override fun onClick(v: View) {


    //Do stuff
 }
 } myButton.setOnClickListener(new OnClickListener() {
 @Override
 public void onClick(View v) {
 //Do stuff
 }
 });
  19. Collections channelList?.flatMap { it.episodeList }?.map { it.delete() } if (channelList

    != null) {
 for (Channel channel : channelList) {
 for (Episode episode : channel.getEpisodeList()) {
 episode.delete();
 }
 }
 }
  20. Collections ArrayList newList = new ArrayList();
 for (Channel channel :

    channelList) {
 if (channel.getTitle().startsWith("Fox")) {
 newList.add(channel);
 }
 } val channels = channelList.filter { it.title.startsWith("Fox") } 
 val channelsWithTitle = channelList.filterNot { it.title == null }
 val channel = channelList.find { it.title.equals("Channel 1") }
  21. Data Classes public class Person {
 private String firstName;
 


    public String getFirstName() {
 return firstName;
 }
 
 public void setFirstName(String firstName){
 this.firstName=firstName;
 }
 
 @Override
 public boolean equals(Object obj) {…}
 
 @Override
 public int hashCode() {…}
 
 @Override
 public String toString() {…}
 }
  22. Kotlin Android Extensions public class MyActivity extends Activity {
 @Override


    public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 TextView someText = (TextView) findViewById(R.id.text_view);
 someText.setText("Hello, world!");
 }
 }
  23. Kotlin Android Extensions import kotlinx.android.synthetic.main.activity_main.text_view
 
 class MyActivity : Activity()

    {
 override fun onCreate(savedInstanceState: Bundle?) {
 super.onCreate(savedInstanceState)
 setContentView(R.layout.activity_main)
 text_view.setText(“Hello, world!")
 }
 }
  24. Setup buildscript {
 repositories {
 jcenter()
 }
 dependencies {
 classpath

    'org.jetbrains.kotlin:kotlin-gradle-plugin:1.0.3'
 classpath 'org.jetbrains.kotlin:kotlin-android-extensions:1.0.3'
 }
 } dependencies {
 compile 'org.jetbrains.kotlin:kotlin-stdlib:1.0.3' testCompile 'org.jetbrains.kotlin:kotlin-test-junit:1.0.3'
 }
  25. Further Reading • Try Kotlin • Kotlin Documentation • Kotlin

    for Android Developers - Antonio Leiva • Interal memo on Kotlin at Square - Jake Wharton • How I fell in love with a programming language - Dan Kim • Making Safer Apps - Eric Young
  26. Further Watching • Android Development with Kotlin - Jake Wharton

    • Advancing Android Development with the Kotlin Language - Jake Wharton