The Project Context

Book in Progress AI represents the digital evolution of an established educational project, led by the IISS “E. Majorana” in Brindisi. The platform must handle multilingual content, dynamic personalization of learning styles through the “Genius” mode, and a continuous formative assessment system. These features require a robust, performant, and easily maintainable frontend.


Why ReScript for an Educational Project?

Type Safety and Code Correctness in a Complex Domain

The educational domain involves complex data models: student profiles with learning styles, adaptive content, learning analytics, and multi-dimensional assessment systems. ReScript, with its advanced type system and automatic inference, has allowed me to model these domains precisely and mathematically correctly.

The ReScript compiler ensures correctness at compile-time, completely eliminating entire categories of runtime errors that are common in traditional JavaScript. This guarantee of correctness is particularly critical in an educational context where system reliability is essential for students’ learning experiences.

Pure Functional Programming and State Management

The “Genius” mode personalization requires managing complex states that dynamically update based on user interaction. ReScript, with its functional nature, encouraged the use of pure functions for business logic. This approach has led to several benefits:

  • Predictability: Pure functions always return the same output for the same input, making system behavior fully predictable.
  • Testability: The absence of side effects makes unit testing extremely simple and reliable.
  • Simplified Debugging: The immutable nature of data eliminates bugs related to unexpected state mutations.

The use of ReScript’s native functional libraries, such as Belt for immutable collections and array/list operations, enabled the writing of elegant and performant code for data processing.

Pattern Matching: The Heart of Application Logic

ReScript’s pattern matching has proven to be a fundamental tool for managing the logical complexity of Book in Progress AI. Compared to traditional if/else or switch statements, pattern matching offers unique advantages:

  • Exhaustiveness Checking: The compiler ensures all possible cases are handled. In the “Genius” mode, pattern matching ensures every learning style is always considered, eliminating bugs from missed cases.
  • Powerful Destructuring: Pattern matching allows direct extraction and manipulation of nested data.
  • Safe State Management: Application states (Loading, Success, Error, Empty) are handled exhaustively, making it impossible to forget a particular state.
  • Refactoring Assistance: When new cases are added, the compiler automatically guides you to all code points that need updating, making software evolution safe and systematic.

Variant Types: Precise Domain Modeling

ReScript’s variant types (or tagged unions) have been one of the most powerful features for modeling the complex domains of Book in Progress AI. They allow defining a type that can take on one value from a finite set of alternatives, each potentially containing specific data.

  • Clarity and Semantics: I was able to explicitly represent complex concepts related to content types, assessment states, or API results. This clarity makes the code self-documenting and easy to understand.
  • Intrinsic Type Safety: The compiler ensures only valid values for a given variant type can be assigned, eliminating a common source of errors.
  • Perfect Integration with Pattern Matching: Variant types are designed to be used with pattern matching. This combination ensures every possible case of a type is explicitly handled, providing full control over application logic.

Asynchronous Programming Support: Robust Interaction Handling

In an educational platform that constantly interacts with the backend for user data, content, and analytics, asynchronous handling is essential. ReScript offers excellent support for asynchronous programming, using the same Promise and async/await concepts as JavaScript, but with type safety.

  • Type-Safe Promises: Promises in ReScript are fully typed, ensuring resolved or rejected data is always of the expected type. This eliminates common errors related to undefined or incorrectly typed data in asynchronous operations.
  • Predictable Side Effects: Although pure functions are preferred, asynchronous operations necessarily introduce side effects. ReScript’s type system helps contain and manage these effects in a controlled way, making it easier to reason about data flow and application state.

Compatibility with the React Ecosystem

A crucial aspect of adopting ReScript for the frontend of Book in Progress AI was its excellent compatibility with React. ReScript provides first-class support for developing React components, allowing you to combine ReScript’s type safety and functional approach with React’s popularity and extensive component library.

  • React Bindings: ReScript provides official and optimized bindings for React, enabling you to write functional React components with the same fluency as JavaScript or TypeScript, but with all the guarantees of ReScript’s type system. This means that props, states, and component events benefit from full type safety.
  • Seamless Integration: Integrating existing or new React components was surprisingly easy. The ReScript compiler generates clean and idiomatic JavaScript output that React can consume without issues, making collaboration between ReScript code and existing JavaScript/React code a practical reality.
  • UI Development Benefits: The combination of ReScript’s robustness and React’s flexibility has proven to be a winning one. We can define logic and data models with maximum safety thanks to ReScript, and then use React to build reactive and performant user interfaces, with the certainty that interactions between logic and UI are type-safe and predictable. This has significantly reduced bugs related to incorrect data passing between components and state management in the UI.

Emerging Advantages

Guaranteed Correctness and Exhaustive Pattern Matching

The main advantage of ReScript is the mathematical guarantee of correctness, enhanced by exhaustive pattern matching. The system eliminates:

  • Null/undefined reference errors (completely impossible).
  • Runtime type errors.
  • Uncovered cases: Exhaustive pattern matching ensures every possible value of a type is always handled.
  • Access to non-existent properties.

Exhaustive pattern matching has proven particularly valuable in managing the complex states of the educational platform. When modeling student progress with variant types, the compiler forces you to consider every possible state, making the software more robust and predictable.

This guaranteed correctness translated into production code that is practically free of runtime errors — a notable achievement for a complex educational platform.

Safe Refactoring with Pattern Matching, Variant Types, and Pure Functions

The development of Book in Progress AI required numerous significant refactorings, especially during the integration of AI features. The functional approach using pure functions, combined with exhaustive pattern matching and variant types, made these refactoring efforts incredibly safe:

  • Pure functions are easy to move and reorganize.
  • The absence of hidden side effects makes refactoring predictable.
  • The type system ensures all usage points are updated correctly.
  • Pattern matching actively assists in refactoring: when a new case is added to a variant type, the compiler automatically identifies all pattern matches that need updating, transforming refactoring from an error-prone process into a systematic and safe one.

Optimal Performance through Immutability

The code generated by ReScript, combined with a functional approach and immutable data structures, has shown excellent performance. Immutability allows aggressive optimizations by the compiler and makes garbage collection more efficient — particularly important for a platform that must handle rich content and real-time interactions.

Long-Term Maintainability

ReScript-based code using pure functions has demonstrated superior maintainability compared to comparable JavaScript/TypeScript projects. The combination of:

  • Pure functions that are easy to test and reason about.
  • Full type safety.
  • Guaranteed absence of null/undefined.
  • Exhaustive pattern matching and variant types.

has created a codebase that is a pleasure to maintain and extend.


Conclusion

Adopting ReScript for the frontend development of Book in Progress AI has proven to be a winning choice, especially for mathematical correctness, exhaustive pattern matching, the strategic use of variant types, the functional approach, and React compatibility that the language promotes. Pure functions, combined with powerful pattern matching and precise modeling through variant types, have produced code that is not only error-free but also elegant and maintainable.

Pattern matching, enhanced by variant types, has proven to be a transformative tool, turning complex logic management from an error-prone process into a systematic and type-safe one. The ability to model complex domains using variant types and manage them with exhaustive pattern matching has significantly elevated the quality of the software.