Re: Why does .new initialize?

2021-07-19 Thread William Michels via perl6-users
Possibly relevant StackOverflow question:

"Why does constraining a Perl 6 named parameter to a definite value make it
a required value?"
https://stackoverflow.com/questions/48166325/why-does-constraining-a-perl-6-named-parameter-to-a-definite-value-make-it-a-req

On Mon, Jul 19, 2021 at 10:00 AM Peter Scott  wrote:

> On 7/19/2021 1:24 AM, Elizabeth Mattijsen wrote:
> > If .new wouldn't initialize a type to its basic instantiation, what
> would be the point of .new then?
> >
> > FWIW, the same goes for:
> >
> >  dd Int.new;  # 0
> >  dd Num.new;  # 0e0
> >  dd Complex.new;  # <0+0i>
> >  dd Str.new;  # ""
> >
> > If you want to leave it undefined, don't call .new on it?
> >
> > *confused*
>
> Only that there's a vocal school of thought in O-O that says new()
> should only allocate memory and never put anything in there.  Now I know
> that Raku doesn't subscribe to that I have no problem.
>
> Cheers,
> Peter
>


Re: REPL / Linenoise question (backslashes)

2021-07-19 Thread William Michels via perl6-users
Hi Rob!

Thanks for the reply. So what you're saying is the "backslash-newline"
combination tells the REPL that it has received incomplete input? Otherwise
I don't see how the Raku REPL knows how to cycle from taking input at its
prompt and moving to the read/evaluate step.

I took a quick look at how three other languages (R, Python2.7, Ruby)
handle backslashes in their REPLs. None of the versions below are "the
latest and greatest", but it should give us an idea what (historically)
other languages have done. R balks at backslashes (but can accept
multi-line input pasted into the terminal, as well as pasted into the
R-GUI). Python2.7 and Ruby appear to accept backslashes as continuation
lines, then balk at syntax errors (Python immediately, Ruby after typing
'exit'):

$ R

R version 3.6.3 (2020-02-29) -- "Holding the Windsock"
Copyright (C) 2020 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin15.4.0 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> \
Error: unexpected input in "\"
> \\
Error: unexpected input in "\"
> \\\
Error: unexpected input in "\"
> q()

$ python
Python 2.7.10 (v2.7.10:15c95b7d81dc, May 23 2015, 09:33:12)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> \
... \
... \\
  File "", line 3
\\
 ^
SyntaxError: unexpected character after line continuation character
>>> exit()

$ ruby --version
ruby 2.0.0p481 (2014-05-08 revision 45883) [universal.x86_64-darwin14]
$ irb --version
irb 0.9.6(09/06/30)
$ irb
irb(main):001:0>
irb(main):002:0* \
irb(main):003:0* \
irb(main):004:0* \\
irb(main):005:0* \\\
irb(main):006:0*
irb(main):007:0* exit
SyntaxError: (irb):4: syntax error, unexpected $undefined
from /usr/bin/irb:12:in `'
irb(main):008:0> exit


HTH, Bill.



On Sat, Jul 17, 2021 at 6:46 PM rir  wrote:

> On Wed, Jul 14, 2021 at 08:40:07AM -0700, William Michels via perl6-users
> wrote:
>
> > > \
> > {}
> > > \\
> > {}
> > > \\\
> > {}
>
> > Curiously, I seem to create an object in my REPL environment when I enter
> > either a single-, double-, or triple-backslash.   ...
>
> Your backslash destroys the newline so the input is non-existent.  The
> empty hash seems an implementation artifact; a place to capture
> the type and value of the code.  Perhaps issuing a prompt as if a bare
> newline
> was typed would be better.
>
> Witness:
>
> > my $x = 1 + 3 \
> {}
> > my $x = ( 1 + 3 \ )
> 4
> > \ # comment
> Nil
> > ;
> Nil
>
> Rob
>


Re: Why does .new initialize?

2021-07-19 Thread Vadim Belman

Matthew has provided some concrete examples of default initializations. I'd 
like to scratch the surface of more general problem: encapsulation. In many 
cases only a class knows it's real internal structure and can use this 
information to protect the data from misuse by 3rd party code which may not 
have the sufficient level of knowledge. If we deprive a class the right of 
self-initializing according what it knows will be a correct state then what 
remains of the encapsulation principle?

Let me make a guess. Perhaps the meaning of "new() should only allocate memory 
and never put anything in there" statement is a bit different. Perhaps the 
initial discussion was about the method new() in particular. It probably stems 
from the early days of Perl OO when more or less typical code was something like

sub new {
my $self = bless {}, shift;
$self->{foo} = 13;
$self->{bar} = 42;
return $self;
}

Then moving the initialization lines for attributes foo and bar out of new() 
makes full sense. One of the approaches taken before the Moose epoch was:

sub new {
my $self = bless {}, shift;
$self->init(@_);
return $self;
}

This was solving some pretty much common errors in class design and allowed the 
child classes to implement their own init() methods without actually worrying 
about object's origins.

Getting back to Raku, the principle is carefully followed because it's new() 
doesn't do any actual object initialization. Instead it re-delegates it down to 
submethods BUILDALL, BUILD, TWEAK. Apparently, it is possible to literally 
follow the principle and split pure new and initialization code. But how much 
happy would you be writing something like Class.new.INITIALIZE(:foo(13), 
:bar(42)) every time? And how much sense would it make?

Best regards,
Vadim Belman

> On Jul 19, 2021, at 1:09 PM, Peter Scott  wrote:
> 
> Yes.  I'm agnostic on this point, but there was a time when some prominent 
> Perl contributors were dogmatic about it and I didn't know how widespread it 
> was.
> 
> Peter
> 
> On 7/19/2021 10:06 AM, Vadim Belman wrote:
>> 
>> Let me guess. The school prohibits object self-initialization? It has to be 
>> done by external code?
>> 
>> Best regards,
>> Vadim Belman
>> 
>>> On Jul 19, 2021, at 1:00 PM, Peter Scott >> > wrote:
>>> 
>>> On 7/19/2021 1:24 AM, Elizabeth Mattijsen wrote:
 If .new wouldn't initialize a type to its basic instantiation, what would 
 be the point of .new then?
 
 FWIW, the same goes for:
 
 dd Int.new;  # 0
 dd Num.new;  # 0e0
 dd Complex.new;  # <0+0i>
 dd Str.new;  # ""
 
 If you want to leave it undefined, don't call .new on it?
 
 *confused*
>>> 
>>> Only that there's a vocal school of thought in O-O that says new() should 
>>> only allocate memory and never put anything in there.  Now I know that Raku 
>>> doesn't subscribe to that I have no problem.
>>> 
>>> Cheers,
>>> Peter
>> 
> 



Re: pod questions

2021-07-19 Thread Tom Browder
On Mon, Jul 19, 2021 at 09:20 Marcel Timmerman  wrote:

> On 7/19/21 2:29 PM, Tom Browder wrote:
>
> On Mon, Jul 19, 2021 at 06:57 Marcel Timmerman  wrote:
>
> Reading a bit, I came across old documents (with a warning that these are
>> out of date) https://design.raku.org/S02.html#Multiline_Comments . It
>> states that any unrecognized format name should be treated as a comment
>> block, which the above output shows, the renderers do not. Should I file an
>> issue?
>>
>
> Yes, please.
>
> -Tom
>
> Will do.
>
> What about the other questions?
>

Regarding the other questions:

1. I believe there are no reserved words for the '=begin/=end comment'.
However, nested comments I believe were an issue at one time but they
aren't now.

3. That depends on what you're doing. For example, currently the Raku
parser recognizes the nested pod code block, but it cannot handle pod codes
inside that pod code block.

If you just want to preserve whitespace using one of the two blocks alone
should be good enough.

-Tom


Re: Why does .new initialize?

2021-07-19 Thread Matthew Stuckwisch
In general, the idea of initialized doesn't mean a lot in Raku, at least
not at the language level.

At any given time, any variable has a value. By default, if you've typed a
variable, it's initially set to the type itself (Any is the default type,
so the default default value). The only exceptions are native types which
are either 0 or the empty string, depending, or if you've added a trait to
override the default value. Note the following, which may be a bit
surprising to you:

my Int $a;
my $b = Int;
my $c;
say $a === $b === Int; # True
say $c === $a; # False

$c's value here is (Any), but both $a and $b are (Int).

.new calls self.bless, which handles all of the allocation stuff behind the
scenes, but —importantly— each attribute, like any variable, will always
have a value (it just might not be defined), such that

class Foo {
has Rat $.bar;
}
say Foo.new.bar === Rat; # True
say Foo.new.bar.defined; # False

In some classes, undefined values for those attributes may not make sense,
and each can make its own decisions how to handle when lacking defined
values.

Hope that makes sense,

Matéu

On Mon, Jul 19, 2021, 19:10 Peter Scott  wrote:

> Yes.  I'm agnostic on this point, but there was a time when some prominent
> Perl contributors were dogmatic about it and I didn't know how widespread
> it was.
>
> Peter
>
> On 7/19/2021 10:06 AM, Vadim Belman wrote:
>
>
> Let me guess. The school prohibits object self-initialization? It has to
> be done by external code?
>
> Best regards,
> Vadim Belman
>
> On Jul 19, 2021, at 1:00 PM, Peter Scott  wrote:
>
> On 7/19/2021 1:24 AM, Elizabeth Mattijsen wrote:
>
> If .new wouldn't initialize a type to its basic instantiation, what would
> be the point of .new then?
>
> FWIW, the same goes for:
>
> dd Int.new;  # 0
> dd Num.new;  # 0e0
> dd Complex.new;  # <0+0i>
> dd Str.new;  # ""
>
> If you want to leave it undefined, don't call .new on it?
>
> *confused*
>
>
> Only that there's a vocal school of thought in O-O that says new() should
> only allocate memory and never put anything in there.  Now I know that Raku
> doesn't subscribe to that I have no problem.
>
> Cheers,
> Peter
>
>
>
>


Re: Why does .new initialize?

2021-07-19 Thread Peter Scott
Yes.  I'm agnostic on this point, but there was a time when some 
prominent Perl contributors were dogmatic about it and I didn't know how 
widespread it was.


Peter

On 7/19/2021 10:06 AM, Vadim Belman wrote:


Let me guess. The school prohibits object self-initialization? It has 
to be done by external code?


Best regards,
Vadim Belman

On Jul 19, 2021, at 1:00 PM, Peter Scott > wrote:


On 7/19/2021 1:24 AM, Elizabeth Mattijsen wrote:
If .new wouldn't initialize a type to its basic instantiation, what 
would be the point of .new then?


FWIW, the same goes for:

dd Int.new;  # 0
dd Num.new;  # 0e0
dd Complex.new;  # <0+0i>
dd Str.new;  # ""

If you want to leave it undefined, don't call .new on it?

*confused*


Only that there's a vocal school of thought in O-O that says new() 
should only allocate memory and never put anything in there.  Now I 
know that Raku doesn't subscribe to that I have no problem.


Cheers,
Peter






Re: Why does .new initialize?

2021-07-19 Thread Vadim Belman

Let me guess. The school prohibits object self-initialization? It has to be 
done by external code?

Best regards,
Vadim Belman

> On Jul 19, 2021, at 1:00 PM, Peter Scott  wrote:
> 
> On 7/19/2021 1:24 AM, Elizabeth Mattijsen wrote:
>> If .new wouldn't initialize a type to its basic instantiation, what would be 
>> the point of .new then?
>> 
>> FWIW, the same goes for:
>> 
>> dd Int.new;  # 0
>> dd Num.new;  # 0e0
>> dd Complex.new;  # <0+0i>
>> dd Str.new;  # ""
>> 
>> If you want to leave it undefined, don't call .new on it?
>> 
>> *confused*
> 
> Only that there's a vocal school of thought in O-O that says new() should 
> only allocate memory and never put anything in there.  Now I know that Raku 
> doesn't subscribe to that I have no problem.
> 
> Cheers,
> Peter



Re: Why does .new initialize?

2021-07-19 Thread Peter Scott

On 7/19/2021 1:24 AM, Elizabeth Mattijsen wrote:

If .new wouldn't initialize a type to its basic instantiation, what would be 
the point of .new then?

FWIW, the same goes for:

 dd Int.new;  # 0
 dd Num.new;  # 0e0
 dd Complex.new;  # <0+0i>
 dd Str.new;  # ""

If you want to leave it undefined, don't call .new on it?

*confused*


Only that there's a vocal school of thought in O-O that says new() 
should only allocate memory and never put anything in there.  Now I know 
that Raku doesn't subscribe to that I have no problem.


Cheers,
Peter


Re: pod questions

2021-07-19 Thread Tom Browder
On Mon, Jul 19, 2021 at 06:57 Marcel Timmerman  wrote:

Reading a bit, I came across old documents (with a warning that these are
> out of date) https://design.raku.org/S02.html#Multiline_Comments . It
> states that any unrecognized format name should be treated as a comment
> block, which the above output shows, the renderers do not. Should I file an
> issue?
>

Yes, please.

-Tom


pod questions

2021-07-19 Thread Marcel Timmerman

Hi,

Still trying to find a way to have test code in my programs. Normally 
not executed but an imported class could make some sense of it. Others 
have tried already but I wanted to do the following which looks promising;


=begin Gnome-T
=begin code

my Int $i = 10;

=end code
=end Gnome-T


I can find the test code when I process the pod data. The tag added to 
begin will not pose a problem. I need the code section to keep the 
newlines in. The problem now is when one needs to render other pod data 
to html or markdown, this part comes with it like shown the next 
markdown output



Gnome-T
===

    my Int $i = 10;



Three questions;

 * Are there any reserved tags for the '=begin' format name?
 * Reading a bit, I came across old documents (with a warning that
   these are out of date)
   https://design.raku.org/S02.html#Multiline_Comments . It states that
   any unrecognized format name should be treated as a comment block,
   which the above output shows, the renderers do not. Should I file an
   issue?
 * If the block is really treated as comments, would the code block be
   necessary?

Regards,

Marcel



Re: Why does .new initialize?

2021-07-19 Thread Elizabeth Mattijsen
> On 19 Jul 2021, at 05:49, Peter Scott  wrote:
> 
> I'm curious as to why Rat.new initializes instead of leaving as undefined:
> 
> > $*RAKU
> Raku (6.d)
> > my Rat $p
> (Rat)
> > put $p
> Use of uninitialized value $p of type Rat in string context.
> Methods .^name, .raku, .gist, or .say can be used to stringify it to 
> something meaningful.
>   in block  at  line 1
> > my $q = Rat.new
> 0
> > put $q
> 0

If .new wouldn't initialize a type to its basic instantiation, what would be 
the point of .new then?

FWIW, the same goes for:

dd Int.new;  # 0
dd Num.new;  # 0e0
dd Complex.new;  # <0+0i>
dd Str.new;  # ""

If you want to leave it undefined, don't call .new on it?

*confused*