Giter Club home page Giter Club logo

Comments (5)

mobily avatar mobily commented on May 20, 2024 1

sounds reasonable! I already have an initial implementation, so it will be published in v3.10.0 this week 🚀

from ts-belt.

mobily avatar mobily commented on May 20, 2024

@Darkle this is an excellent idea! but here's the thing, I'm wondering what type the tryCatch function should return? I see two options:

  1. the first option is straightforward, we add tryCatch to the F namespace (so it is F.tryCatch), and it always returns the Result data type
export declare function tryCatch<A, B>(
  value: A,
  fn: (value: A) => B,
): Result<B, string>

usage:

type Obj = {
  readonly hello: string,
}

const x = pipe(
  '{"hello": "world"}',
  F.tryCatch<string, Obj>(JSON.parse), // → Ok({ hello: 'world' })
  R.map(obj => obj.hello),
  R.getWithDefault(''),
) // → 'world'

const y = pipe(
  '<>',
  F.tryCatch<string, Obj>(JSON.parse), // → Error('Unexpected token < in JSON at position 0')
  R.map(obj => obj.hello),
  R.getWithDefault('oops'), 
) // → 'oops'

the upside of this option is that having a single tryCatch function might be less confusing for users, and the F namespace is a perfect place for that kind of function, the downside tho might be mapping to the Option data type with R.toOption if someone needs Option instead of Result ¯_(ツ)_/¯

  1. alternatively, we could add tryCatch to both namespaces: Option and Result, so we had O.tryCatch and R.tryCatch eventually
// R namespace
export declare function tryCatch<A, B>(
  value: A,
  fn: (value: A) => B,
): Result<B, string>


// O namespace
export declare function tryCatch<A, B>(
  value: A,
  fn: (value: A) => B,
): Option<B>

what are your thoughts on this topic?

from ts-belt.

Darkle avatar Darkle commented on May 20, 2024

I think 1 is the better choice. But it would probably be good to remind people in the docs for tryCatch that they can convert the Result to an Option with R.toOption.

from ts-belt.

mobily avatar mobily commented on May 20, 2024

@Darkle published in v3.10.0

from ts-belt.

thelinuxlich avatar thelinuxlich commented on May 20, 2024

This won't handle promises/async right?

from ts-belt.

Related Issues (20)

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.