I think I have an idea of where your thinking is going wrong.
The trouble is going to be to describe it so that you can understand.
First, I think you may be misunderstanding what we mean by defined and
undefined.
So I will use "instance" and "class"
---
class Foo {};
say Foo; # <- "class" value
say Foo.new(); # <- "instance" value
my $bar = Foo; # <- "class" value
my $baz = Foo.new(); # <- "instance" value
Foo.defined.say; # False # <- "class" value
Foo.new().defined.say; # True # <- "instance" value
---
In Perl 6 types are both types, and values:
You can store it as a value.
my $a = Int;
I am going to call that a "class" value.
In this example the value is of type "Int".
The "Int" ("class") value is also of type "Int".
You can even pass around "class" values with the type "smiley".
my $a = Int:D;
This is useful in the compiler.
---
If you use it as a type:
my Int $b;
You are saying that it must be an "instance" or a "class" value of that type.
The default value is the same as the type.
So that last example is the same as:
my Int $b = Int;
You can bring it back to the default by assigning a "class" value.
$b = 42;
$b = Int;
If you use a type "smiley" in the type, you constrain it further.
my Int:D $c; # error
In this case you are saying only "instance" values are allowed.
In that last case the default value is "Int:D", which is a "class"
value, but only "instance" values are allowed by the type.
Note that again the default value is the same as the type, so it
produces an error.
---
The type/value "Nil" is special, as it changes the variable to it's default.
my Int $d is default(42); # 42
$d = 10; # 10
$d = Nil; # 42
In this example for it to hold a "class" value, you have to do so explicitly.
$d = Int; # Int
---
my Int:U $u;
The "class" values that can be stored in this variable are below
Int
Int:_ # same as previous
Int:U
Int:D
class Other-Int is Int {…}
Other-Int
Other-Int:_ # same as previous
Other-Int:U
Other-Int:D
my \foo = Int but 'Foo'
foo
foo:_ # same as previous
foo:U
foo:D
Note again that I am talking about them as values.
---
When you use a type in a declaration:
my Int $f;
You can think about it as a macro named "my" which takes two arguments.
The first one is a "class" value, and the second one is the name of
the variable.
This makes it so that you can have aliases.
constant Name = Str:D;
my Name $first-name = "Brad";
---
I think where you are going wrong is that other languages also use the
words "defined" and "undefined".
When we use them in Perl 6, we mean something slightly different.
===
You just added a question about :=
:= is the binding operator.
Normally scalar variables store a Scalar that you assign into.
Just think about this:
my $a = 0;
As being short for something like the following fake code:
(my $a := Scalar.new) = 0;
When you use :=, you are basically creating an alias:
my $b;
my $c := $b;
$c = 42;
say $b; # 42
This also means that if you alias a value, the variable becomes readonly
my $d := 0;
$d = 0; # error Cannot assign to an immutable value
On Sun, Sep 16, 2018 at 7:49 PM ToddAndMargo <[email protected]> wrote:
>
> On 09/14/2018 08:07 PM, ToddAndMargo wrote:
> > ":D"
> > means it wants actual data in the string and not a Nil.
> > The jargon for this requirement is that is is constrained
> > to an actual value
> >
> > If it wanted a Nil, it would say ":U" or constrained to
> > a Nil
>
> Iteration 3:
>
>
> ":D"
> means it wants the variable "Defined" (Constrained).
> For example:
> my Str $x;
> my Int $i;
>
> The value of the variable may be empty but the variable
> does not become "defined" until a value is places in it.
> ("Nil" is seen as "Undefined")
>
> ":U"
> means the variable is "Undefined" (defaults to type "Any")
>
> For example:
> my $x;
> my $i;
>
>
>
> $ p6 'my $x; if $x.defined {say "Defined"}else{say "Undefined"};'
> Undefined
>
> $ p6 'my Real $x; if $x.defined {say "Defined"}else{say "Undefined"};'
> Undefined
>
> $ p6 'my Real $x=3; if $x.defined {say "Defined"}else{say "Undefined"};'
> Defined
>
> $ p6 'my $x=3; if $x.defined {say "Defined"}else{say "Undefined"};'
> Defined
>
>
> $ p6 'my $x=3; dd $x'
> Int $x = 3
>
> $ p6 'my $x; dd $x'
> Any $x = Any
>
> $ p6 'my Real $x; dd $x'
> Real $x = Real
>
> $ p6 'my Real $x = 3; dd $x'
> Int $x = 3
>
> Huh ??????