aboutsummaryrefslogtreecommitdiff
path: root/src/std/any.gleam
blob: ac902cd2abf46b3d51d2a1c835f25f2c28122a6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import std/list as list_mod
import std/atom
import std/result

// `Any` data is data that we don"t know the type of yet.
// We likely get data like this from interop with Erlang, or from
// IO with the outside world.
//
pub external type Any;

// Convert any Gleam data into `Any` data.
//
pub external fn from(a) -> Any = "gleam__stdlib" "identity";

// Unsafely cast any type into any other type.
//
// This is an escape hatch for the type system that may be useful when wrapping
// native Erlang APIs. It is to be used as a last measure only.
//
pub external fn unsafe_coerce(a) -> b = "gleam__stdlib" "identity";

pub external fn string(Any) -> Result(String, String)
  = "gleam__stdlib" "decode_string"

pub external fn int(Any) -> Result(Int, String)
  = "gleam__stdlib" "decode_int"

pub external fn float(Any) -> Result(Float, String)
  = "gleam__stdlib" "decode_float"

pub external fn atom(Any) -> Result(atom:Atom, String)
  = "gleam__stdlib" "decode_atom"

pub external fn bool(Any) -> Result(Bool, String)
  = "gleam__stdlib" "decode_bool"

pub external fn thunk(Any) -> Result(fn() -> Any, String)
  = "gleam__stdlib" "decode_thunk"

external fn list_any(Any) -> Result(List(Any), String) =
  "gleam__stdlib" "decode_list"

pub fn list(any, decode) {
  any
  |> list_any
  |> result:then(_, list_mod:traverse(_, decode))
}

pub external fn tuple(Any) -> Result({Any, Any}, String)
  = "gleam__stdlib" "decode_tuple"

pub external fn field(Any, a) -> Result(Any, String)
  = "gleam__stdlib" "decode_field"