Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions docs/docs/noir/concepts/data_types/coercions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
title: Type Coercions
description:
Noir's various type coercions
keywords:
[
noir,
types,
coercions,
casts,
]
sidebar_position: 11
---

When one type is required in Noir code but a different type is given, the compiler will typically issue
a type error. There are a few cases however where the compiler will instead automatically perform a
type coercion. These are typically limited to a few type pairs where converting from one to the other
will not sacrifice performance or correctness. Currently, Noir will will try to perform the following
type coercions:

| Actual Type | Expected Type |
| ------------- | --------------------------- |
| `[T; N]` | `[T]` |
Comment thread
jfecher marked this conversation as resolved.
| `fn(..) -> R` | `unconstrained fn(..) -> R` |
| `str<N>` | `CtString` |
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think fmtstr will also be coerced to CtString. I added this because, like str, it also implements AsCtString but to be honest I'm not sure it's that useful to have so we could either remove it from the compiler or document it here.

| `&mut T` | `&T` |

Note that:
- Conversions are only from the actual type to the expected type, never the other way around.
- Conversions are only performed on the outermost type, they're never performed within a nested type.
- `CtString` is a compile-time only type, so this conversion is only valid in [comptime code](../../concepts/comptime.md).
- `&T` requires the experimental `-Zownership` flag to be enabled.

Examples:
```noir
fn requires_slice(_slice: [Field]) {}
comptime fn requires_ct_string(_s: CtString) {}

fn main() {
let array: [Field; 4] = [1, 2, 3, 4];

// Ok - array is converted to a slice
requires_slice(array);
// equivalent to:
requires_slice(array.as_slice());

// coerce a constrained function to an unconstrained one:
let f: unconstrained fn([Field]) = requires_slice;

comptime {
// Passing a str<6> where a CtString is expected
requires_ct_string("hello!")
}
}
```
Loading