aboutsummaryrefslogtreecommitdiff
path: root/std/prelude/convert.pk
blob: 6abf543af283a7a122618b3c09f32ebc9559bab3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
## std.convert: Interfaces for type coersion and conversion.
## This module is imported by default.

## The Coerce interface is used for type conversion that will not fail.
## Its associated methods, `from` and `into`, are used internally
## by the compiler for implicit type conversion (coersion).
pub type Coerce[T] = interface
  from(T): Self
  # into(Self): T

## The `into` function is automatically implemented for all types that
## implement `from`: that is, all types U that are convertable to T.
pub func into[T, U: Coerce[T]](self: T): U =
  from(self)

## The Convert interface is used for type conversion that may fail.
# We'll see what this breaks.
pub type Convert[T] = interface
  from(T): Self?!

## A blanket implementation of a corresponding `into` function.
pub func into[T, U: Convert[T]](self: T): U =
  from(self)