Hacker News new | ask | show | jobs
by nvme0n1p1 63 days ago
Yes but why? Why should I import a giant dependency to do this:

  a = [1, 2, 3];
  foo = effect.Array.append(a, 4);
instead of using what's built in to the language?

  a = [1, 2, 3];
  foo = [...a, 4];
2 comments

You wouldn't. Effect isn't an array library.
did you read the docs? it says

"Use when you need to guarantee a non-empty result after adding a required trailing value."

...whatever that means lol

The types allow you to very narrowly define functions so that you can, for example, require that a NonEmptyArray be passed. This helps to fulfill that requirement without casts.
NonEmpty is a type that allows you to distinguish between:

    a = [undefined];
    res = a.pop() // undefined

    a = [];
    res = a.pop() // undefined
As a Python person, that 2nd one looks like a bug. Expecting an exception there. Although also Python doesn't have an `undefined`; `None` would have to suffice.
That 2nd one being a bug is exactly what the type system and NonEmpty types is designed to help prevent.
Not really. If you try to take something from an empty container (language or real life) then you should get an error/issue. The container type doesn't change when it's empty, and the operation is just senseless; all that's happening here is added mental gymnastics that makes code unintuitive to read. Checking if/asserting a container isn't empty is a straight forward `if cont: ...` or `assert cont` in Python, which anyone can just get.
The type soft-enforces₁ it immediately for everyone though.

Without the type there are a few possibilities: 1. I could forget the assertion and have buggy code without realizing. 2. I could ensure that all uses do the assertion. Even if we can statically know that it is empty. This costs us the check for every call of the function, and ends up being more code.

Alternatively I could statically know in advance. The code won't let me `Array.pop` because that is invalid, so:

  1. I *cannot* make that mistake
  2. It cost no runtime performance
  3. It cost no extra code in a function
1: I say soft-enforce because if someone were to pass a value incorrectly cast as NonEmpty, it would still have a runtime error, but that is a bug elsewhere, not here.
Right, you can attempt to take and potentially error, but if you already know that taking is possible you can skip the noise and simply return a result. Checking if your container is empty every time you remove an item is not free.