Hacker News new | ask | show | jobs
by herrosheep 2756 days ago
I'd like to hear opinions on how the use of sigils and the other syntax oddities like Int:D, Int:U, given, when, etc improves the codabliity/readability of Perl 6 vs other languages. The examples vs Haskell are not helping me understand. Personally I don't think the added verbosity is helping in any way.
8 comments

Can you give an example of a language that is similarly precise with less verbosity, while still being comprehensible?

Perl 6 is alarmingly concise in a lot of cases (hyper-operators come to mind), so it's super weird to see it called verbose. I'd wager that the ideal, or at least shortest, Perl 6 solution to most problems is shorter than most other languages...I don't know if it's necessarily more readable (you have to know a lot of the language to read really concise Perl 6 code, and there are many new concepts for most developers), but it's definitely not verbose.

Int:D, Int:U, etc. provide information about types. They aren't arbitrary extra boilerplate. Every language with types specifies them in somewhat comparable ways. Perl 6 has gradual typing, you can use as much or as little of it as you want, and as suits your problem domain. You don't need those types, but if you use them well, you can do things more concisely...e.g. for multi-method dispatch based on type of arguments.

given/when are keywords. Every language has keywords, and sometimes they'll be new ones that you haven't seen in other languages.

Sigils, again, provide additional information, both to the reader and the compiler. Perl 6 is probably an improvement, or at least simplification, over Perl 5 in that the sigil is constant and doesn't reflect the usage. e.g. in Perl 5, when you want a single element of an array, you use $name[1], but when you want the whole array you use @name. The idea (sort of) being that $ is singular (a "scalar" value), while @ is plural. Some people don't like sigils, but I don't see any point in re-litigating it 30 years on. Perl has sigils. That's just how it do. Some like it, some don't. Enough Perl developers like it that it's never gone away.

> Can you give an example of a language that is similarly precise with less verbosity, while still being comprehensible?

This assumes I think Perl (6) is precise and comprehensible.

You've already made it clear you have no experience with the language...so, what are you trying to accomplish here?
Sigils have been standard in shells for decades. And sigils allow string interpolation to work; again, just like in normal unix shells. The python people decided that sigils are a bad idea, so instead of being able to say "I have $n apples" (like you could in shells we've all been using forever!) I now need to say "I have {} apples".format(n). I know which one I prefer!
Javascript has `I have ${n} apples`, or even `This is chapter ${i+1}.`

In terms of interpolation, that's the variant that I like best.

Perl 6 does it without the sigil outside the bracket :)

"I have {some-complex-calc($i)} frobitzes"

Unlike Python's `"I have {} apples".format(n)` and various shells' `"I have $n apples"`, Javascript's and Apache Groovy's `"This is chapter ${i+1}"` mean the lexer must make a call to the parser when lexing the string contents -- this allows `"This is ${"chapter"} ${i+1}"` to parse. Don't know about Javascript, but this makes Groovy's parser unreadable, and stuck on a very old version of Antlr, i.e. 2 instead of 4 (the latest).
In P6:

grammar one-grammar { rule { <some-pattern> <another-grammar> .... } }

Can't get simpler. Can't be more readable.

AFAIK, Groovy 3 is targeting Antlr 4.
Also in groovy. I too am a fan.
I don't get the given/when part of this comment at all? given/when is exactly like C's switch/case, except the names are better and it's at least an order of magnitude more powerful.

It's 100% not added verbosity in any way. Added complexity, sure, but it's a pretty straightforward application of some of the main ideas of the language, usable in the standard C-language-family way out of the box, and powerful and flexible when you understand it.

> I don't get the given/when part of this comment at all?

Given and when were listed as separate items, not “given/when”; I agree with you about given (encompassing the given/when construct), but I can understand seeing “when” as an oddity (though I think it's a plus for comprehensibility, because it's general behavior is a consistent generalization of it's behavior in given/when.)

If Perl is too verbose for you, you can always go to APL.
I think APL wins hands down in being the most concise when it comes to mathematical tasks, but P6 wins at more common scripting tasks such as parsing data. Only Rebol & Red in my opinion have similar constructs to P6's native grammars. Rebol wins at being the quickest way to write short GUI applications that do useful things as it uses its GUI dialect (basically a dsl) to make it really easy....almost unbelievably so (http://www.re-bol.com/rebol.html). I'd say powershell is the most concise way to deal with Windows automation.

So really...all depending on what you mean.

> I'd like to hear opinions on how the use of sigils and the other syntax oddities like Int:D, Int:U, given, when, etc improves the codabliity/readability of Perl 6 vs other languages.

Given/when is not really a syntax oddity, it's (in the simplest case), a structurally and semantically common construct in other languages that goes by various names (case/when, switch/case, etc.), the name is unique to Perl but the naming fis all over the map between other languages, and Perl matches English much better than most others (though it would do so more precisely if “when” was “when it is”), so that's a plus for readability except for people who come to Perl more fluent in some other programming language than in English.

Other uses of when might be unique to Perl, but they generalize the use from given/when (and have the much the relation to English), so there is a deep consistency there; learning a concept that is more limited in other languages buys you more in Perl.

FWIW, with a little syntactic trick, you can actually have sigillless variables in Perl 6:

    my \foo = $ = 42;  # bind sigilless name to a container with 42
    say foo;    # 42
    foo = 666;
    say foo;    # 666
See https://opensource.com/article/18/9/using-sigils-perl-6 for more information. </plug>
Perl6's use of sigils seems excessive to me but I do like sigils for variables. It acts like a namespace between similarly named constructs. I know for instance WebClient is a class and $WebClient contains an instance of it. I also like how it makes it easy for syntax highlighters to identify variables from classes and keywords.
> The examples vs Haskell are not helping me understand. Personally I don't think the added verbosity is helping in any way.

It doesn't sound to me like you are the right audience for the document you're reading.

Quoting from that page:

> this should not be mistaken for a beginner tutorial or overview of Perl 6; it is intended as a technical reference for Perl 6 learners with a strong Haskell background.

It is not wise to start with a technical reference unless you are already fairly well versed in the basics of the technology you're learning and are motivated to deepen your knowledge of it.

If you are interested in P6 you should start with overview or beginner material. Then, if you have a strong Haskell background, you can refer to the technical reference if and when the overview or beginner material isn't working for you.

(If you are not genuinely motivated to have fun learning P6 then nothing related to P6 will work for you. Are you genuinely motivated to have fun learning P6?)

----

> I'd like to hear opinions on how the use of sigils and the other syntax oddities

If you view childishly simple and natural syntax as "oddities" then your mind is already made up that they are oddities.

So the rest of this comment is intended primarily for other readers and secondarily for you on the off chance that you are able to transcend your mind's confirmation bias against the following constructs actually being simple and natural.

> use of sigils

Many programmers think in terms of three fundamental primitive data types from which all other datatypes are composed:

* single items

* collections of numbered things

* collections of named things

P6 adopts this view and assigns three sigils to these three abstract notions.

This makes it both easier and quicker to both read and write code -- once you've learned how they work. In P5 sigils were onerously complex. In P6 they are childishly simple. So now it's also easy and quick to learn them as well as to use them.[1]

> Int:D, Int:U

In P6, every type that's created using any of its type constructors is a sum type with two sub-types. This includes types defined in the standard language such as Int. The sub-types are named D and U. For example, to refer to Int's D sub-type, use `Int:D`.

This makes it easy and quick to read and write code once you've learned what they are. Fortunately, it's childishly simple to learn what they are.[2]

> given

The `given` keyword means essentially the same as the English word `given`.

This makes it easy and quick to read and write code once you've learned the English meaning of `given`.[3]

> when

The `when` keyword hides a good deal of complexity. But one can hop on Wittgenstein's Ladder easily. Because for the first rung, it just means the same as the meaning of the English word `when` used in the context of something that's `given`.

The keyword `when` makes it easy and quick to read and write code once you've learned the English meaning of `when` in the context of `given` -- and it can then be used in other contexts.[4]

----

[1] 27 second vide clip covers it all: https://www.youtube.com/watch?v=Nq2HkAYbG5o&t=10m&list=PLRuE...

[2] They're called "type smilies" for a reason. :D is the definite, happy, valuable type. What's `42`? Simple. Definite. Happy. Valuable. Unlike :D, the other subtype, :U, is undefined, unhappy, not a value. What's `Int`? Is it `42`? No, because that would be defined. Every value/object is either definite/happy or it isn't.

[3] From dictionary.com: given: noun: "an established fact"

[4] From dictionary.com: when: conjunction: "in the event that"

> In P6, every type that's created using any of its type constructors is a sum type with two sub-types. > This includes types defined in the standard language such as Int. The sub-types are named D and U. > For example, to refer to Int's D sub-type, use `Int:D`.

Probably your comment hints to this already in which case the following will be redundant but I wanted to mention them more explicitly. This is mainly for people unfamiliar with Perl 6.

Using the type smileys(:U, :D) are also ways to specifically match against a type object (`Int`, `Str`, `Rat`, etc.) and an instance object of a type object (-5, 'Hello', 1/5, etc.):

    Int ~~ Int:U;   # True
    -5  ~~ Int:U;   # False
    -5  ~~ Int:D;   # True
    Int ~~ Int:D;   # False
There is also the :_ smiley which is used by default whenever the other two aren't specified:

    Int ~~ Int;     # True, same as: Int ~~ Int:_;
    -5  ~~ Int;     # True, same as: -5  ~~ Int:_;
> Probably your comment hints to this already

Regardless, I'm glad you've written your comment to clarify. :)

I'd be delighted to hear about any of the several big or small mistakes I am about to make in the following.

Also an honest opinion about whether the following approach I take boils down to one that is complete gibberish, vaguely interesting but terribly complex, OK but meh, good but a bit abstract, enlightening, or something else. If you do let me know, please clarify whether you mean what you say for just you, or for what you think might be true for others that don't know P6 at all. TIA.

----

Like a lot of P6 stuff this basic aspect of its type system is both childishly simple but also so general, abstract, and high level, and comes at things from such a different angle than pretty much all other languages, that it can be difficult to explain.

Another attempt at what I was trying to get at in my second footnote in my GP comment...

----

A P6 type and its two standard sub-types are a simple and fun unification of the universal/existential "quantification" that our inner logician loves and the "article definiteness" distinction recognized by our linguistic side.

From https://en.wikipedia.org/wiki/Article_(grammar):

> The articles in English grammar are "the" and "a/an" ... In languages that employ articles, every common noun, with some exceptions, is expressed with a certain definiteness, definite or indefinite

From https://en.wikipedia.org/wiki/Quantifier_(logic):

> The two most common quantifiers mean "for all" [Universal] and "there exists" [Existential].

----

:D denotes a definite thing.

It is directly analogous to "the".

The value `42` is considered definite.

Definiteness implies that a thing actually exists. Its the linguistic analog of existential quantification.

Many coders talk about happy/sad path processing. :D is associated with the happy path, perhaps because it has no existential crisis. :D

----

:U is related to Universal quantification.

As a type, it is also related to "a". Quoting wikipedia:

> An indefinite article indicates that its noun is not a particular one identifiable to the listener.

:U corresponds to an indefinite thing, something that's only identifiable as a Universal, not a Definite thing.

:U is also associated with Undefined things, even if they are supposed to be undefined.

In the context of talk about happy/sad path processing, :U is associated with the Unhappy path, because it can denote a thing that should be definite but isn't.

----

As a value, a type name without a `:D` or `:U` smiley is the same as the type name with a `:U`.

As a type, a type name without a smiley is the same as the type name with a `:_`, where the `_` denotes either `D` or `U`.

Thus:

* `Int`, as a thing in of itself, is the same as an `Int:U`.

* `Int`, as a type of thing, denotes either an `Int:U` or an `Int:D`.

> ...the several big or small mistakes I am about to make in the following.

Everything you wrote seems fine to me and you even expounded on what I wrote! However, even if it wasn't, I'd be hardly pressed to come up with a response as clever and resourceful as your responses. For this reason, I quite enjoy reading your comments whenever I come across them on /r/perl6 and /r/programminglanguages.

> Like a lot of P6 stuff this... is both childishly simple but also so general, abstract, and high level... that it can be difficult to explain.

You're totally spot on. The most recent example of my struggle (mainly due to my ignorance of Perl 6 and probably CS concepts too) was the concept of containers and how they interface most assignments in Perl 6. I think the fact that many Perl 6 constructs/concepts, which complex in nature, seem so natural can deceive people who haven't tried it yet. This is because it's easy to overlook (or plainly ignore) how much thought went into both integrating them into the language and make them play nicely together.

---

Thanks for your comments!

> Everything you wrote seems fine to me and you even expounded on what I wrote!

Given that your reply was just the right sort of clear answer that would make up for my original footnote I was especially happy to riff off your answer and get super detailed again. :)

> I'd be hardly pressed to come up with a response as clever and resourceful as your responses.

.oO ( Ever too clever by half )

> The most recent example of my struggle (mainly due to my ignorance of Perl 6 and probably CS concepts too) was the concept of containers and how they interface most assignments in Perl 6.

Yeah. Simple on the outside so noobs can just do their thing. Rich on the inside so gurus can develop new candy and gourmet meals.

Anyhoo, thanks for your reassuring feedback. :)