9

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.

raiph
  • 31,607
  • 3
  • 62
  • 111
user1134991
  • 3,003
  • 2
  • 25
  • 35
  • 3
    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 Jan 19 '18 at 20:18
  • 1
    Yes, my mistake, adverbs.. Thanks. As for rest, I would need to go deeper... – user1134991 Jan 22 '18 at 09:49

5 Answers5

13

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.

raiph
  • 31,607
  • 3
  • 62
  • 111
8

"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.

moritz
  • 12,710
  • 1
  • 41
  • 63
7

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

timotimo
  • 4,299
  • 19
  • 23
  • 4
    To save folk a search, here's [the glossary entry for adverb](https://docs.perl6.org/language/glossary#index-entry-Adverb). – raiph Jan 19 '18 at 19:46
7

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);
piojo
  • 6,351
  • 1
  • 26
  • 36
  • @raiph Thanks for the suggestion about "optional". Edited. As far as parameters versus arguments, that may be too "ivory tower" to be in this answer, since I'm not aware of the distinction! That's my personal benchmark of when a fact is obscure. :) – piojo Jan 21 '18 at 11:47
  • 3
    Hi piojo. That's a pretty good benchmark. :) The difference is the one between the *declaration* of a routine and a *call* of that routine. *Parameters* are part of a signature which is part of the *declaration* of a routine at *compile-time*. *Arguments* are part of an "argument list" which is part of a *call* of a routine *at run-time*. A successful call results in the called routine's parameters being *bound* to the *values* that are "passed" at *run-time* as *arguments*. Now I've written that out, I request that you leave your answer as is in this regard. Now folk can read this comment. :) – raiph Jan 21 '18 at 23:14
3

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.

Brad Gilbert
  • 33,846
  • 11
  • 78
  • 129
  • about your additional re-uses - delete should be a verb.. Right? As for Normalize, that could be a verb, but it could be an adverb, for example if a string function might return null, it returns an empty string, and a number function would return 0. – Gerard ONeill Aug 27 '21 at 06:24