Hacker Newsnew | past | comments | ask | show | jobs | submit | greyblake's commentslogin

## What is nutype?

*nutype* is a procedural macro that adds sanitization and validation to newtypes, ensuring that values always meet your defined constraints.

## What’s new in v0.6.2

* *\[FEATURE]* Added `derive_unsafe(..)` for deriving arbitrary traits (requires enabling the `derive_unsafe` feature). * *\[FIX]* Upgraded Rust edition: *2021 → 2024*. * *\[FIX]* Improved error messages for `len_char_max` and `len_char_min`—they’re now clearer and easier to interpret.

## About `derive_unsafe`

With `derive_unsafe(..)`, you can now derive *any trait*, including third-party ones that nutype doesn’t recognize.

Unlike the safe `derive(..)`, this skips nutype’s internal checks. That means *you can accidentally break your validations at runtime*—for example, by enabling mutation via `DerefMut`.

To use this feature, add the `derive_unsafe` flag.

*Cautionary example:*

```rust use derive_more::{Deref, DerefMut}; use nutype::nutype;

#[nutype( derive(Debug, AsRef), derive_unsafe(Deref, DerefMut), validate(greater_or_equal = 0.0, less_or_equal = 2.0) )] struct LlmTemperature(f64);

fn main() { let mut temperature = LlmTemperature::try_new(1.5).unwrap();

    // This is exactly what you shouldn't do!
    *temperature = 2.5;

    // Validation rule? Violated.
    assert_eq!(temperature.as_ref(), &2.5);
} ```

*TL;DR:* `derive_unsafe` gives you freedom—but with that comes full responsibility. Use wisely.


Nutype is a proc macro that allows adding extra constraints like sanitization and validation to the regular newtype pattern. The generated code makes it impossible to instantiate a value without passing the checks. It works this way even with serde deserialization.


CoffeeScript is a language without static typing. Despite this, CoffeeScript actually made a real revolution when it was introduced.


You don't have to. You can have write a single component in ReScript and use it with the rest of the project written in JS or TS. See https://rescript-lang.org/docs/gentype/latest/introduction


Whatlang demo webpage is implemented with seed: https://whatlang.org/


+1


There still "weak" points, where incorrect typing can be introduced, e.g. incorrectly written bindings. Regarding data from HTTP call in theory they can be cast to any type abusing `external` keyword which is meant for binings. But common and recommend approach is to write codes. Codec would perform all the necessary checks to ensure that the data are in correct shape to satisfy the type restrictions. You can see some examples here: https://github.com/greyblake/from-typescript-to-rescript/blo...


I agree, ReScript has no big adoption (hopefully yet), and that's the biggest disadvantage in comparison with TypeScript. But somebody has to do the first steps...


There's some project to get TS type annotations to work for ReScript. It'd be great if ReScript could lift on the work done by the TS community to produce types for many common JS libs.


I wonder how would that work. Typescript has pretty advanced types like Template Literal Types[0]. Wouldn't ReScript need to support all those types? Or maybe it just resorts to `any` equivalent if I can't map a TS type to ReScript type.

[0] https://www.typescriptlang.org/docs/handbook/2/template-lite...


> the use of decorators in even the most basic example

Can we agree that JSX is not a valid JS? So decorators are required indeed to simulate "JSX magic"

> no async/await.

As I mentioned, it's not an really an issue (although I though it will be). At least from my experience.

> You can't have two files with the same name in different folders??

This sucks indeed :)

The problem that those 20% corrupt the value of another 80% :/


> Can we agree that JSX is not a valid JS?

Neither is rescript - so why not make jsx a first class citizen?


JSX is a first-class citizen in ReScript. No decorators required for JSX. Only required for React component function signature.


Because a programming language is not a template engine.


Neither is jsx. It’s just syntactic sugar for some recursive function calls.


Thanks for your comment! I am aware of PureScript and even tried it a little bit 3-4 years ago, but was not aware that it has good interops too!


Yeah I also noticed Purescript was missing from your comparisons list. It's the only one of these that I've looked at much, and it seemed like the best of them, though I wasn't aware of the issue with ES6 modules. I don't use JS at all for now, so I haven't been up to date about anything in it.


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: