Quantcast
Viewing latest article 20
Browse Latest Browse All 25

An Exquisite Amble through the Realm of Desugaring

Image may be NSFW.
Clik here to view.

Bonjour Confrères and Consoeurs,

Ai, Yay, Yay,

wish to invite you on a captivating sojourn into the sophisticated sphere of Desugaring. In the lexicon of programming, Desugaring denotes the elegant procedure of translating more ornate linguistic constructs—dubbed as Syntactic Sugar — into their simpler, fundamental equivalents.

Language processors, including compilers and static analyzers, often expand sugared constructs into their more verbose equivalents before processing, a process sometimes called “desugaring”.
Image may be NSFW.
Clik here to view.
“it could be exposed to societal prejudices and harmful stereotypes and integrate these into its images” (Craiyon)

Let’s delve into this with a trifecta of illuminating illustrations:


Java 8: Lambda expressions—an epitome of chic in Java 8. They are nothing short of syntactic sugar elegantly draped over the structure of functional interfaces.

Runnable r = () -> System.out.println("Executing with élan in Java!");
r.run();

Manifests as:

Runnable r = new Runnable(){
    public void run(){
        System.out.println("Executing with élan in Java!");
    }
};
r.run();
Image may be NSFW.
Clik here to view.
PHP Syntactic Sugar Illustration (Craiyon)

Python: Drawing our gaze to Python, we find the list comprehension—a charming vignette of syntactic sugar.

elegantlist = [x**2 for x in range(5)]

Transforms to:

elegantlist = []
for x in range(5):
  elegantlist.append(x**2)
Image may be NSFW.
Clik here to view.

Go: In the realm of Go, short variable declarations are a sublime manifestation of syntactic sugar.

func main() {
  g := 42
  fmt.Println(g)
}

Becomes:

func main() {
  var g int
  g = 42
  fmt.Println(g)
}
Image may be NSFW.
Clik here to view.
The eyes, its always the eyes. Affirmative, Dave. I read you. (Craiyon)

PHP Constructor Property Promotion

Meandering onto our home turf, PHP 8.0, we encounter Constructor Property Promotions—an exhibition of syntactic sugar par excellence:

class Vertex {
  public function __construct(
   public float $x = 0.0,
   public float $y = 0.0,
   public float $z = 0.0,
  ) {}
}

Unveils as:

class Vertex {
  public float $x;
  public float $y;
  public float $z;

  public function __construct(
     float $x = 0.0,
     float $y = 0.0,
     float $z = 0.0,
  ) {
    $this->x = $x;
    $this->y = $y;
    $this->z = $z;
  }
}
Image may be NSFW.
Clik here to view.

Venting through the protocol:

( new class (Vertex::class) { public function __construct(string $_) { var_dump ([
    // :: show instance without a call to contructor :: //
    ($_ = new ReflectionClass($_))->newInstanceWithoutConstructor(),
    // :: show default values :: ................... :: //
    ...array_map(fn ($_) => [$_->getName() => $_->getDefaultValue()], $_->getProperties())
]) ; } } );

Emits:

array(4) {
  [0] =>
  class Vertex#3 (3) {
    public float $x =>    *uninitialized*
    public float $y =>    *uninitialized*
    public float $z =>    *uninitialized*
  }
  [1] =>  array(1) {
    'x' =>    NULL
  }
  [2] =>  array(1) {
    'y' =>    NULL
  }
  [3] =>  array(1) {
    'z' =>    NULL
  }
}
Image may be NSFW.
Clik here to view.

Our programming languages—true art maestros—deftly handle the canvas of syntactic sugar, providing us, fortunate souls, with a more expressive, palatable syntax.

For those in pursuit of deeper enlightenment:


Viewing latest article 20
Browse Latest Browse All 25

Trending Articles