[Kotlin]Guard and If

๐Ÿ‘‰๐Ÿป ์Šค์œ„ํ”„ํŠธ์˜ Guard์™€ ์ฝ”ํ‹€๋ฆฐ์˜ If๋ฅผ ๋น„๊ตํ•˜๊ธฐ ์œ„ํ•œ ์„ค๋ช…์ž…๋‹ˆ๋‹ค.
Here’s a comparison between Swift’s Guard and Kotlin’s If.

๐Ÿ‘‰๐Ÿป ์ฝ”ํ‹€๋ฆฐ์—๋Š” Guard๋ฌธ๋ฒ•์ด ์—†์Šต๋‹ˆ๋‹ค.
There is no guard syntax in Kotlin.

๐Ÿ‘‰๐Ÿป ์Šค์œ„ํ”„ํŠธ์˜ Guard๋ฌธ๋ฒ•๊ณผ IF๊ฐ€ ์–ด๋–ป๊ฒŒ ๋‹ค๋ฅธ์ง€ ๋น„๊ตํ•ด ๋ณด์„ธ์š”
Compare how Guard and IF statements differ in Swift.

๐Ÿ‘‰๐Ÿป ์ฝ”ํ‹€๋ฆฐ์—์„œ๋Š” ์Šค์œ„ํ”„ํŠธ์˜ Guard๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด If๋ฌธ์œผ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
In Kotlin, Swift’s Guard can be expressed as an If statement as follows:

        val v = value ?: return      // guard let
        if (v <= 0) return           // guard value > 0
        if (v == 999) return         // guard value != 999

๐Ÿ‘‰๐Ÿป์ฝ”๋“œ / Code

package com.freelifemakers.guardif

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Build
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.freelifemakers.guardif.ui.theme.GuardIfTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            GuardIfTheme {
                Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
                    ContentView(modifier = Modifier.padding(innerPadding))
                }
            }
        }
    }
}

@Composable
fun ContentView(modifier: Modifier = Modifier) {

    // SwiftUI์˜ @State
    var text by remember { mutableStateOf("") }

    // Swift guard ํŒจํ„ด โ†’ Kotlin if + early return
    fun process(value: Int?) {
        val v = value ?: return      // guard let
        if (v <= 0) return           // guard value > 0
        if (v == 999) return         // guard value != 999

        println("Print Value: $v")
        text = "$v"
    }

    // ์•„๋ž˜์ฒ˜๋Ÿผ ์‹คํ–‰ํ•ด๋„ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.
    // It also works if you run it like below.

    // process(1)

    // SwiftUI onAppear โ†’ Compose LaunchedEffect
    LaunchedEffect(Unit) {
        process(2)
    }

    // UI
    Column(
        modifier = modifier
            .fillMaxSize()
            .padding(16.dp),
        verticalArrangement = Arrangement.Center, // ์ˆ˜์ง ์ค‘์•™ ์ •๋ ฌ / vertical center alignment
        horizontalAlignment = Alignment.CenterHorizontally  // ์ˆ˜ํ‰ ์ค‘์•™ ์ •๋ ฌ / horizontal center alignment
    ) {
        Icon(
            imageVector = Icons.Default.Build,
            contentDescription = "Globe"
        )
        Spacer(modifier = Modifier.height(16.dp))
        Text(text = "Text1: $text")
    }
}

@Preview(showBackground = true)
@Composable
fun ContentViewPreview() {
    GuardIfTheme {
        ContentView()
    }
}

๐Ÿ‘‰๐Ÿป ์Šคํฌ๋ฆฐ์ƒท/ScreenShot

Leave a Reply