Hacker News new | ask | show | jobs
by SideburnsOfDoom 1512 days ago
> arrays of alternating functions and regexps

many languages will allow you to succinctly express the concept of "the type of this element is a or b"

e.g. typescript:

https://www.typescriptlang.org/docs/handbook/2/everyday-type...

Rust:

https://doc.rust-lang.org/book/ch06-01-defining-an-enum.html

F# :

https://docs.microsoft.com/en-us/dotnet/fsharp/language-refe...

IDK, allowing that kind of type to be declared seems like a good thing to me? Those links have good examples of where it's useful. You'd need a good reason to use actually "function or regexp" in actual code though.

2 comments

You have misunderstood GP.

Alternating functions and regexps looks like this:

    [\&some_function, qr/some_regex/, \&another_function, qr/another_regex/]
This is distinct from the idea of sum types.
Oh, I get that their example said _alternating_ function and regex. If that's the real requirement then they shouldn't be _alternating_ in the list at all, there should be a list of (function, regex) tuples. We don't need an example of tuples, they're even more widespread than sum types / unions / enums.

So the design as stated is very contrived, to the point where it doesn't make sense, but existing constructs can be used to make sense of it with a change one way or the other.

Sum types are great, but neither perl nor python let you statically check that they're being used properly.