Crystal

From G1Joshi/Agent-Skills · View interactive version

Crystal is an AI agent skill from the GitHub repository G1Joshi/Agent-Skills. Expert Crystal programming assistance covering static typing, LLVM compilation, C bindings, and fibers. Use when writing ultra-fast compiled programs with elegant Ruby-like syntax.

How to use this skill

Skill file

Crystal

Crystal compiles to native code (using LLVM) but looks exactly like Ruby. v1.12 continues improving Windows support and parallelism.

When to Use

Quick Start

# Define typed struct with automatic getters
record User, id : Int32, name : String, email : String

users = [
  User.new(1, "Alice", "alice@example.com"),
  User.new(2, "Bob", "bob@example.com")
]

users.each do |user|
  puts "#{user.name} (#{user.email})"
end

Core Concepts

Compile-Time Static Type Inference

Provides the syntax of Ruby with the type-safety of a compiled language without redundant type annotations:

# Crystal infers types at compile time
def compute_discount(price, coupon = nil)
  if coupon == "VIP"
    price * 0.8
  else
    price
  end
end

puts compute_discount(100)       # 100
puts compute_discount(100, "VIP") # 80.0

Concurrency via Fibers & Channels (CSP)

Lightweight cooperative green threads scheduled cooperatively on an event loop:

channel = Channel(String).new

# Spawn lightweight fiber
spawn do
  sleep 0.5.seconds
  channel.send("Processing complete")
end

puts "Waiting for background fiber..."
message = channel.receive
puts "Received: #{message}"

C-Binding Interoperability

Calls external C dynamic libraries natively with zero wrapper boilerplate:

@[Link("m")]
lib LibM
  fun sqrt(x : Float64) : Float64
end

puts LibM.sqrt(144.0) # 12.0

Common Patterns

Fast C Library Interoperability

Problem: Calling native C libraries without writing complex wrapper boilerplate.

Solution:

Declare lib binding directly in Crystal:

lib LibC
  fun puts(str : UInt8*) : Int32
  fun getpid : Int32
end

pid = LibC.getpid
puts "Current process ID: #{pid}"

Best Practices

Do:

Don't:

Troubleshooting

| Error | Cause | Solution |

| :--------------------------------------- | :------------------------------------------------------------------ | :------------------------------------------------------------- |

| type must be (Type), not (Type \| Nil) | Nil-checking missing before accessing methods on nullable variable. | Use var.not_nil! or guard with if var. |

| Can't use Type as type in union | Incompatible types combined without common ancestor. | Explicitly annotate union type or cast with .as(TargetType). |

| undefined constant in shard | Missing dependency or shards.yml not resolved. | Run shards install to install project dependencies. |

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.