rawhat / glisten

A pure gleam TCP library
Apache License 2.0
48 stars 6 forks source link

Is the example wrong? #17

Closed avgrocks closed 3 months ago

avgrocks commented 4 months ago

Hello,

The example in the README file is maybe wrong.

import gleam/bytes_builder
import gleam/erlang/process
import gleam/option.{None}
import gleam/otp/actor
import glisten.{Packet}

pub fn main() {
  let assert Ok(_) =
    glisten.handler(fn(_conn) { #(Nil, None) }, fn(msg, state, conn) {
      let assert Packet(msg) = msg
      let assert Ok(_) = glisten.send(conn, bytes_builder.from_bit_array(msg))
      actor.continue(state)
    })
    |> glisten.serve(3000)

  process.sleep_forever()
}

This will throw

error: Type mismatch
  ┌─ /home/xxx/dev/gleam/xxx/src/xxx.gleam:9:21
  │
9 │     glisten.handler(fn(_conn) { #(Nil, None) }, fn(msg, state, conn) {
  │                     ^^^^^^^^^^^^^^^^^^^^^^^^^^

Expected type:

    fn() -> #(a, Option(Selector(b)))

Found type:

    fn(c) -> #(Nil, Option(d))

The on_init function takes no argument: https://hexdocs.pm/glisten/glisten.html#handler if I remove the _conn argument everything works.

rawhat commented 3 months ago

Generally, the documentation (including the README) on hexdocs will be the valid reference point for the latest published version. In this case, there were some changes made on the main branch that were not published yet. The README was updated here to reflect that.

I have since published a new version, so this should not be an issue anymore. Just giving context for the future 😃