0Pricing
Scala for Backend Engineering & Functional Programming · Ders

Örtük Dönüşümler

Dikkatli kullanın

Örtük Dönüşümler, CoddyKit'te ücretsiz bir Scala for Backend Engineering & Functional Programming dersidir. Bu, 4 dersinin 3. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Scala for Backend Engineering & Functional Programming öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Scala for Backend Engineering & Functional Programming kursu toplamda 4 dersten oluşur.

Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.

What is an implicit conversion?

An implicit conversion automatically transforms a value of one type into another when the compiler needs it. It can make APIs more convenient, but it must be used with care because it hides what is happening.

object Main {
  import scala.language.implicitConversions
  implicit def intToString(x: Int): String = s"number-$x"
  def main(args: Array[String]): Unit = {
    val s: String = 42
    println(s)
  }
}

How the compiler uses them

When a value's type does not match what is expected, the compiler searches for an implicit conversion that fixes the mismatch and inserts it silently.

object Main {
  import scala.language.implicitConversions
  implicit def doubleToInt(d: Double): Int = d.toInt
  def addTen(x: Int): Int = x + 10
  def main(args: Array[String]): Unit = {
    println(addTen(3.9))
  }
}

Enabling the feature

Defining implicit conversions requires importing scala.language.implicitConversions, or the compiler warns you. This is a deliberate friction to discourage overuse.

object Main {
  import scala.language.implicitConversions
  case class Celsius(value: Double)
  implicit def doubleToCelsius(d: Double): Celsius = Celsius(d)
  def describe(c: Celsius): String = s"${c.value} C"
  def main(args: Array[String]): Unit = {
    println(describe(36.6))
  }
}

Wrapping for extra methods (old style)

Before extension methods, implicit conversions to a wrapper class were the way to 'add' methods to existing types, the so-called pimp-my-library pattern.

object Main {
  import scala.language.implicitConversions
  class RichInt2(x: Int) {
    def squared: Int = x * x
  }
  implicit def toRich(x: Int): RichInt2 = new RichInt2(x)
  def main(args: Array[String]): Unit = {
    println(5.squared)
  }
}

The danger: silent surprises

Because conversions are invisible at the call site, they can cause confusing behavior and bugs that are hard to trace. A typo might compile by accidentally triggering a conversion.

object Main {
  import scala.language.implicitConversions
  implicit def boolToInt(b: Boolean): Int = if (b) 1 else 0
  def main(args: Array[String]): Unit = {
    val total = true + 4 // surprising! becomes 1 + 4
    println(total)
  }
}

Prefer explicit conversion

Often the safest choice is a plain named method instead of an implicit. The code is longer but obvious to readers.

object Main {
  case class Celsius(value: Double)
  def fromDouble(d: Double): Celsius = Celsius(d)
  def main(args: Array[String]): Unit = {
    val c = fromDouble(20.5)
    println(c)
  }
}

Scala 3: the Conversion type class

Scala 3 makes conversions explicit via a given Conversion[A, B] instance. This is clearer about intent and still requires the language import.

import scala.language.implicitConversions

object Main:
  given Conversion[Int, String] = (x: Int) => s"val-$x"
  def main(args: Array[String]): Unit =
    val s: String = 7
    println(s)

Conversions only apply once

The compiler applies at most one implicit conversion at a time. It will not chain two conversions to bridge a gap, which limits surprises.

object Main {
  import scala.language.implicitConversions
  implicit def intToStr(x: Int): String = x.toString
  def needString(s: String): Int = s.length
  def main(args: Array[String]): Unit = {
    println(needString(12345))
  }
}

Where conversions are found

Like other implicits, conversions are resolved from local scope, imports, and companion objects. Keeping them in a clearly named import makes their presence visible.

object Conversions:
  import scala.language.implicitConversions
  implicit def strToLen(s: String): Int = s.length

object Main:
  import Conversions._
  def main(args: Array[String]): Unit =
    val n: Int = "hello"
    println(n)

When conversions are reasonable

Legitimate uses include interoperating with Java types, numeric widening helpers, and library boundaries. Outside those, prefer extension methods or explicit functions.

object Main {
  import scala.language.implicitConversions
  case class Meters(v: Double)
  implicit def metersToDouble(m: Meters): Double = m.v
  def main(args: Array[String]): Unit = {
    val d: Double = Meters(3.5)
    println(d * 2)
  }
}

Rule of thumb

If a reader cannot tell from the code that a conversion happens, that is a warning sign. Reach for implicit conversions last, after extension methods and explicit calls.

object Main {
  // Explicit and obvious - usually better
  case class Price(cents: Int)
  def toDollars(p: Price): Double = p.cents / 100.0
  def main(args: Array[String]): Unit = {
    println(toDollars(Price(1599)))
  }
}

Quick Check

What is the main risk of implicit conversions?

Recap

You learned implicit conversions and their caveats:

  • They auto-convert one type to another when types mismatch
  • Require import scala.language.implicitConversions
  • Scala 3 uses given Conversion[A, B]
  • Only one conversion is applied at a time
  • Use them sparingly; prefer extension methods or explicit calls

Sıkça Sorulan Sorular

“Örtük Dönüşümler” dersi ücretsiz mi?

Evet — “Örtük Dönüşümler” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Scala for Backend Engineering & Functional Programming kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Scala for Backend Engineering & Functional Programming kursu toplamda 4 dersten oluşur.

“Örtük Dönüşümler” dersinde ne öğreneceğim?

Dikkatli kullanın Scala for Backend Engineering & Functional Programming ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.

Scala for Backend Engineering & Functional Programming öğrenmeye başlamak için deneyim gerekli mi?

Önceden deneyim gerekmez. CoddyKit'te Scala for Backend Engineering & Functional Programming, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 3. dersidir.

“Örtük Dönüşümler” dersi ne kadar sürer?

Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.

Bu Scala for Backend Engineering & Functional Programming dersinde kod yazıp çalıştırabilir miyim?

Evet. Her Scala for Backend Engineering & Functional Programming dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.

Bu kursun tüm dersleri

  1. Örtük Parametreler
  2. Scala 3 given/using
  3. Örtük Dönüşümler
  4. Genişletme Yöntemleri
← Scala for Backend Engineering & Functional Programming Sayfasına Dön