Giter Site home page Giter Site logo

Module naming about gleam_jsone HOT 3 CLOSED

lpil avatar lpil commented on June 12, 2024
Module naming

from gleam_jsone.

Comments (3)

lpil avatar lpil commented on June 12, 2024 1

Given type specific functions are not required for decoding I think the encode_ prefix would be redundant- the second example you give could be this:

import gleam/jsone.{JsonValue}

pub fn encode_person(name: String, age: Int) -> JsonValue {
  jsone.object([
    tuple("name", jsone.string(name)),
    tuple("age", jsone.int(age))
  ])
  |> jsone.encode
}

This is the most concise, has unambiguous module names without renaming, and requires only a single module import.

from gleam_jsone.

rjdellecese avatar rjdellecese commented on June 12, 2024 1

I'm persuaded—I like that better. Reopening to track as a task.

from gleam_jsone.

rjdellecese avatar rjdellecese commented on June 12, 2024

Thank you for the feedback, @lpil!

I hear you, and had also wondered the same thing. I agree that the usage in your example feels a bit awkward.

The main reason I chose this module naming structure was that I thought the encoding functions would feel better to use this way. The usage I envisioned was like so:

import gleam/jsone/encode.{encode_json, JsonValue}

pub fn encode_person(name: String, age: Int) -> JsonValue {
  encode.object([
    tuple("name", encode.string(name)),
    tuple("age", encode.int(age))
  ])
  |> encode_json
}

So I guess that would be in contrast to something like this:

import gleam/jsone.{JsonValue}

pub fn encode_person(name: String, age: Int) -> JsonValue {
  jsone.encode_object([
    tuple("name", jsone.encode_string(name)),
    tuple("age", jsone.encode_int(age))
  ])
  |> jsone.encode
}

I still think I might like the former a little better... if you wanted to make it really clear what kind of data you were encoding, you could always alias the import, e.g.

import gleam/jsone/encode.{encode_json, JsonValue} as jsone_encode

pub fn encode_person(name: String, age: Int) -> JsonValue {
  jsone_encode.object([
    tuple("name", jsone_encode.string(name)),
    tuple("age", jsone_encode.int(age))
  ])
  |> encode_json
}

But I'll keep thinking about it!

Thank you again for the input! :)

from gleam_jsone.

Related Issues (6)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.