Skip to content

Instantly share code, notes, and snippets.

import scala.annotation.implicitNotFound
import scala.language.{dynamics, implicitConversions}
object HMap:
opaque type M[T <: Tuple] = Map[Any, Any]
extension [T <: Tuple](m: M[T])
def add[K <: Singleton, V](k: K, v: V)(using
NoMatch[T, K] =:= true
): M[(K, V) *: T] = m + (k -> v)

Exploring Type-Safe Dynamic Maps in Scala 3

In this blog post, we'll dive into a powerful implementation of a type-safe heterogeneous map in Scala 3. We'll explore how to create a TypedHMap (a slight twist on the original HMap name) that allows keys and values of different types while maintaining type safety, and pair it with a dynamic interface for an intuitive, case-class-like experience. Along the way, we'll unpack the Scala 3 concepts that make this possible and showcase its power with practical examples.

What is a Heterogeneous Map?

In Scala, a standard Map requires all keys to share one type (e.g., String) and all values to share another (e.g., Int), as in Map[String, Int]. But what if you need a map where each key-value pair can have its own types—like "name" -&gt; String, "age" -&gt; Int, and "active" -&gt; Boolean—all within the same map? This is where a heterogeneous map comes in. Unlike a Map[Any, Any], which sacrifices type safety, our goal is a map that enforces type correc

import pytest
from moto import mock_dynamodb2, mock_sqs
import boto3
import os
import json
from lambdas.scanner.scanner_lambda import lambda_handler
@pytest.fixture
def aws_credentials():
os.environ['AWS_ACCESS_KEY_ID'] = 'testing'
#!/bin/bash
# Path to your Hercules.properties file
PROPERTIES_FILE="Hercules.properties"
# Temporary files
SBT_OUTPUT="sbt_update_output.txt"
PROPERTIES_DEPS="properties_deps.txt"
SBT_DEPS="sbt_deps.txt"
# Function to list and switch Git branches using fzf
git_checkout_fzf() {
# Ensure we're inside a Git repository
if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
echo "Not a git repository!"
return 1
fi
# Fetch the latest remote branches
git fetch --prune
import scala.meta._
class ExtendWith(additionalFields: (String, Type)*) extends scala.annotation.StaticAnnotation {
inline def apply(defn: Any): Any = meta {
// Extract the original case class fields
val q"..$mods case class $tname[..$tparams] $ctorMods(...$paramss) extends $template" = defn
// Create new fields using the additionalFields provided
val newFields = additionalFields.map {
case (name, tpe) => param"$name: $tpe"
package com.ncl.vacation.analytics.utils
import scala.language.experimental.macros
import scala.reflect.macros.blackbox
import scala.reflect.runtime.universe.typeOf
object ExtendCaseClass {
def extendCaseClassImpl[T: c.WeakTypeTag](c: blackbox.Context)(additionalFields: c.Expr[Any]*): c.Expr[Any] = {
import c.universe._
object Graduation extends DefaultRunnableSpec {
def spec =
suite("Graduation") {
test("bulkhead") {
type Task = Has[Console] with Has[Live]
val task = Live.live(ZIO.sleep(100.millis)) *> Console.print(".")
final case class Bulkhead(ref: TRef[Int], maxInProcess: Int) {

Advanced Functional Programming with Scala - Notes

Copyright © 2017 Fantasyland Institute of Learning. All rights reserved.

1. Mastering Functions

A function is a mapping from one set, called a domain, to another set, called the codomain. A function associates every element in the domain with exactly one element in the codomain. In Scala, both domain and codomain are types.

val square : Int => Int = x => x * x
package com.github.etacassiopeia.jna;
import com.sun.jna.LastErrorException;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;
import sun.nio.ch.DatagramSocketAdaptor;
import java.io.FileDescriptor;
import java.io.IOException;