9
votes

Upon reading certain documents, I have noticed that they use classes, functions, symbols, methods, things that even I, as an electronics engineer, know about. Then, they have concepts which I have never heard of, such as roles, and adverbs. If I don't understand the nomenclature, I can't understand well the documents, and might be getting very unexpected results, and not utilize well the language as well.

I could not find their definition anywhere, including as tags in StackOverflow....Any pointers would be appreciated.

5
Probably you meant adverbs, not adjectives in the title? As for the word 'adverb', it comes from Latin (adverbium). Ad- means 'to', verbum means verb, action. So, in natural languages adverb is something that modifies (e.g. gives some additional info) a verb, whose main role is to denote an action.Eugene Barsky
Yes, my mistake, adverbs.. Thanks. As for rest, I would need to go deeper...user1134991

5 Answers

13
votes

which paradigm do the concepts role and adverb come from?

roles

Roles are an OO concept.

They are Raku's generalization and unification of the notion of OO interface protocols, mixins, and traits.

Quoting the Characteristics section of the Trait page:

For inter-object communication, traits are somewhere between an object-oriented protocol (interface) and a mixin. An interface may define one or more behaviors via method signatures, while a trait defines behaviors via full method definitions: i.e., it includes the body of the methods. In contrast, mixins include full method definitions and may also carry state through member variable, while traits usually don't.

Raku's roles cover all three of the above, er, roles, plus some additional capabilities to extend classes, or other roles, or objects, with:

Roles can also act as classes themselves in the sense that instantiating one will work via type punning.

adverbs

Adverbs in Raku are:

  • Directly analogous to adverbs in natural languages like English.

  • Named parameters/arguments that are used to modify a routine's behaviour rather than serving as input per se.

Adverbs are frequently specified using ordinary key/value pair syntax but, in a manner analogous to adverbs in natural languages, can appear in a diverse range of convenient syntactic and semantic forms, eg:

m:i / foo /

The :i is the "ignoring case" adverb (set to True) being passed to the match (m) routine. Note how this is not input in the same way that the foo is, or $_ which is implicitly used as the string being matched against, but instead just modifies how the matching process does what it does.

Using a pair argument to modify a routine's behaviour like this is what justifies calling :i an "adverb" for the m routine.

8
votes

"Adverbs" specifically are borrowed from human language -- Larry Wall is a linguist by training. Likewise, we tend to talk about terms as nouns, and operators as verbs.

7
votes

maybe the glossary on the perl 6 documentation site helps:

https://docs.perl6.org/language/glossary

Also, there's a documentation section on roles specifically: https://docs.perl6.org/language/objects#Roles

7
votes

In language, adverbs describe how a verb (action) happens. Walking quickly. Carefully writing a function. Debugging patiently. For operators and functions, Perl 6 adverbs act like linguistic adverbs: they describe how the function is to be done. To open a file for reading with the latin1 encoding:

my $fh = $path.open(:r, :enc('latin1'));

Without those adverbs, open would still open the file, but not in read-mode and with a different encoding.

For a function, all adverbs are named parameters. But named parameters don't always influence how the function operates. They may simply be parameters, often optional, which are passed by name. Conceptually, these aren't adverbs. They look like adverbs, but we tend not to call them that.

my AuthToken .= new(username => "fred", :$password);
3
votes

I'm going to try go get you to think about adverbs the way I do.

Imagine an object with a move method

class Foo
  method move ( $direction ) {…}
}

To get it to move to the right you might write

Foo.new.move('right');

Now what if that doesn't move fast enough for you, you may want to write something like this

Foo.new.move('right', :quickly);

So a object is a noun, a method is a verb, and an adverb is an adverb.


Now it would be nice to be able to re-use that syntax for modifying more things like:

  • quoting q :backslash '\n'
  • hash access %h{'b'}:delete
  • other user-defined operators 'a' (foobar) 'b' :normalize

That's useful enough, but what if you want to match the second thing a regex would match.

'Foo Bar' ~~ m :nd(2) / <:Lu> <:Ll>+ /; # 「Bar」

How about allowing :2nd be an alias to :nd(2)

'Foo Bar' ~~ m :2nd / <:Lu> <:Ll>+ /; # 「Bar」

And have :quickly just be short for :quickly(True).
How about we add :!quickly and have it short for :quickly(False).


That gives us really nice syntax, but how does it actually work?
Well there is the idea of named parameters quickly => True from Perl 5, so how about it just be generalization for that.

class Foo
  # there are more ways to handle named parameters than this
  multi method move ( $direction ) {…}
  multi method move ( $direction, :$quickly! ) {…}
}

Foo.new.move( 'right', :quickly )

Foo.new.move( 'right' ) :quickly

Foo.new.move( 'right', :quickly(True) )

Foo.new.move( 'right', ) :quickly(True)

Foo.new.move( 'right', quickly => True )

I've only touched on the subject, but it doesn't really get any harder than this.

It's widespread reuse of features like this that are why we often call Perl 6 strangely consistent.