Code Monkey home page Code Monkey logo

Comments (6)

minamorl avatar minamorl commented on June 28, 2024 1

Using .isOK() method is not common in JS either. And you have to look at the prototype to see the method exists.

As you mentioned, it requires looking up to the prototype and the cost developers pay will be mostly same as using plain object (or higher because of console logging issue). Thank you for sharing your thoughts on this. You can close this issue. 👍

from bread-n-butter.

wavebeem avatar wavebeem commented on June 28, 2024

Hi @minamorl!

Thanks for reading 😄

I used to have classes for everything, but I changed the design.

New style

if (result.type === "ParseOK") {
  // ...
}

Old style

if (result.isOK()) {
  // ...
}

Alternative Old Style

if (result instanceof bnb.ParseOK) {
  // ...
}

I thought that using a type with | and no methods was easier to understand. If
it is a class instead, people will want various methods on it, I think.

I decided to only use classes for things that needed methods for readability
(Parser and Context).

You can see the old design with classes here: https://github.com/wavebeem/bread-n-butter/tree/a2f1fd9d143fa3a9541bc83c627b09e8b213ad3d/src

from bread-n-butter.

minamorl avatar minamorl commented on June 28, 2024

Thanks for quick and detailed reply. I read those old code and understand why you choose current design. In TypeScript world, that works perfectly I guess.

My concern is that people who use the ES as it is are not given the benefit of types. It might be a niche for a use case but from that point of view I prefer the old style. How do you think? :)

from bread-n-butter.

wavebeem avatar wavebeem commented on June 28, 2024

I think the new style is easier for plain JS users for a few reasons:

  • Using instanceof is not common in JS.
  • Using .isOK() method is not common in JS either. And you have to look at the prototype to see the method exists.
  • Using .type === should be easy to discover by looking at console.log(result) output.

I like both styles, but I think plain Object style will be easier to learn than custom classes.

from bread-n-butter.

sveyret avatar sveyret commented on June 28, 2024

Hi @wavebeem,
Why not defining a type guard, which could be useful for both Typescript and Javascript users?

function isParseOK<T>(result: ParseOK<T> | ParseFail): result is ParseOK<T> {
  return result.type === "ParseOK"
}

EDIT: This is actually totally useless because both types contain type which is discriminent…

As a side note, I wanted to tell you that I just successfully ported a small Parsimmon-based project to BnB… 😄

from bread-n-butter.

wavebeem avatar wavebeem commented on June 28, 2024

@sveyret

EDIT: This is actually totally useless because both types contain type which is discriminent…

Yeah I initially thought about making one of these, but you don't need it as you found out 😄

As a side note, I wanted to tell you that I just successfully ported a small Parsimmon-based project to BnB… 😄

Oh cool! If you have any thoughts or feedback, please open an Issue and tell me 😄

from bread-n-butter.

Related Issues (10)

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.