Replace as keyword with @as builtin function for type conversion #35

Merged
noClaps merged 3 commits from as-builtin-fn into main 2026-08-22 09:13:18 -04:00
Owner
No description provided.
Replace as keyword with @as builtin function for type conversion
All checks were successful
/ deploy (push) Successful in 56s
4727bcbe2c
insidiousfiddler requested changes 2026-08-21 13:19:11 -04:00
Dismissed
@ -24,0 +63,4 @@
| `uint` | `byte[]`, `int`, `float`, `str` |
| `float` | `byte[]`, `int`, `uint`, `str` |
| `str` | `char[]`, `byte[]` |
| struct | `str` |

I think we should show examples/declare how some of the more complex types are converted to str. For example, struct, map, enum, and array. Though bool could probably be thrown in as well (is it true|false or 0|1?). I'd say true|false, but that should be explicit.

A similar issue with enums:

enum Node {
  Root(Node[]),
  Doctype,
  Element(Element),
  Comment(str),
  Text(str)
}

println(Node.Doctype("<!doctype html>"))
// Node

What shape are we printing here as a str? Do we include the value inside it, the signature only, how do we handle nested Enums, etc.

I think we should show examples/declare how some of the more complex types are converted to str. For example, `struct`, `map`, `enum`, and `array`. Though `bool` could probably be thrown in as well (is it `true|false` or `0|1`?). I'd say `true|false`, but that should be explicit. A similar issue with enums: ```nc enum Node { Root(Node[]), Doctype, Element(Element), Comment(str), Text(str) } println(Node.Doctype("<!doctype html>")) // Node ``` What shape are we printing here as a str? Do we include the value inside it, the signature only, how do we handle nested Enums, etc.
Author
Owner

I've added examples for how each type will be printed, though all of them are subject to change. I think in actual implementation, we should have a std/fmt module that declares an interface Format that something like fmt.println could use, kind of a mix between Rust's std::fmt::Display and Go's fmt.Println.

import { "std/fmt" as fmt }

struct Data {
  str name
  uint age
}
impl fmt.Format<Data> {
  fn format(Data self) -> str {
      return "\{name: {self.name}, age: {self.age}}"
  }
}

Data data = Data{.name = "Nathan", .age = 24}
fmt.println(data) // output: {name: Nathan, age: 24}

where

// std/fmt.nc

pub interface Format<type T> {
  fn (T self) -> str
}

pub fn println<type T; impl Format<T>>(T[] vals...) {
  for i in vals {
    if i {
      0 -> { @print(Format.format(vals[i])) }
      _ -> { @print(" {Format.format(vals[i])") }
    }
  }
  @println()
}

This could be the recommended way to do formatted printing, and @println is available as the default.

I've added examples for how each type will be printed, though all of them are subject to change. I think in actual implementation, we should have a `std/fmt` module that declares an `interface Format` that something like `fmt.println` could use, kind of a mix between Rust's `std::fmt::Display` and Go's `fmt.Println`. ``` import { "std/fmt" as fmt } struct Data { str name uint age } impl fmt.Format<Data> { fn format(Data self) -> str { return "\{name: {self.name}, age: {self.age}}" } } Data data = Data{.name = "Nathan", .age = 24} fmt.println(data) // output: {name: Nathan, age: 24} ``` where ``` // std/fmt.nc pub interface Format<type T> { fn (T self) -> str } pub fn println<type T; impl Format<T>>(T[] vals...) { for i in vals { if i { 0 -> { @print(Format.format(vals[i])) } _ -> { @print(" {Format.format(vals[i])") } } } @println() } ``` This could be the recommended way to do formatted printing, and `@println` is available as the default.
noClaps deleted branch as-builtin-fn 2026-08-22 09:13:18 -04:00
Sign in to join this conversation.
No reviewers
No labels
stdlib
syntax
No milestone
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
nclang/design!35
No description provided.