* a beginner question
@ 2004-10-20 23:01 mr1yh1
2004-10-20 23:53 ` Björn Persson
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: mr1yh1 @ 2004-10-20 23:01 UTC (permalink / raw)
i am very new in ada , i try to write codes from book and see result
i tried something but i couldnt get a good result
(i use gnat)
******************
with Ada.Text_Io;use Ada.Text_Io;
procedure test is
type Sex_T is
(Male,
Female);
type Person ( S:Sex_T:=Male )is
record
Age:Positive ;
Sex:Sex_T:=S;------please look at here first
case S is
when Male =>
Has_Wife:Boolean ;
when Female=>
Has_Husband:Boolean;
end case;
end record;--person end
Me : Person;
she : Person (Female); ---- and this one ,
begin
Me:=(
S => Male,
Sex => Male,
Age => 33,
Has_Wife => True);
she:=(
S => Female,
Sex => Male, ---please look at here now!!!!!
Age => 23,
Has_Husband => True);
Put(Sex_T'Image(she.Sex));
end test;
****
there is not any alert
and result is
" male " ??
if i dont put "sex=>male" , or "sex=>female"
it dont let me to use a named association...
even is i dont put " s=> female "
it dont let me too ,
but i said about discriminant
when i declare the variable ( she : person(female) )
why its not possible ?
to put
she:=(age=>23,has_husband=>true);
regards
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: a beginner question
2004-10-20 23:01 a beginner question mr1yh1
@ 2004-10-20 23:53 ` Björn Persson
2004-10-21 11:47 ` Marius Amado Alves
2004-10-21 18:16 ` mr1yh1
2 siblings, 0 replies; 4+ messages in thread
From: Björn Persson @ 2004-10-20 23:53 UTC (permalink / raw)
mr1yh1 wrote:
> type Person ( S:Sex_T:=Male )is
> record
> Age:Positive ;
> Sex:Sex_T:=S;------please look at here first
> case S is
> when Male =>
> Has_Wife:Boolean ;
> when Female=>
> Has_Husband:Boolean;
> end case;
> end record;--person end
You have defined a person with two independent sexes. By default, Sex is
initialized to the same value as S, but it's perfectly possible to
change it.
Just remove Sex and let S be the person's sex.
> Put(Sex_T'Image(she.Sex));
This line then becomes:
Put(Sex_T'Image(she.S));
> why its not possible ?
> to put
> she:=(age=>23,has_husband=>true);
The parenthesis here is called an aggregate. An aggregate must provide
values for all the components. But you can do:
She.Age := 23;
She.Has_Husband := True;
--
Björn Persson PGP key A88682FD
omb jor ers @sv ge.
r o.b n.p son eri nu
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: a beginner question
2004-10-20 23:01 a beginner question mr1yh1
2004-10-20 23:53 ` Björn Persson
@ 2004-10-21 11:47 ` Marius Amado Alves
2004-10-21 18:16 ` mr1yh1
2 siblings, 0 replies; 4+ messages in thread
From: Marius Amado Alves @ 2004-10-21 11:47 UTC (permalink / raw)
To: comp.lang.ada
Discriminated types don't let you do indiscriminated things on them :-)
Proficiency with discriminated types is a big part of being an Ada
expert. Read the chapter on discriminated types of an Ada textbook.
Ben-Ari's or Barnes'es.
In the meanwhile and for the things you seem to be trying to do you can
safely use a normal type and constructor functions.
mr1yh1 wrote:
> i am very new in ada , i try to write codes from book and see result
> i tried something but i couldnt get a good result
> (i use gnat)
>
> ******************
> with Ada.Text_Io;use Ada.Text_Io;
> procedure test is
> type Sex_T is
> (Male,
> Female);
>
> type Person ( S:Sex_T:=Male )is
> record
> Age:Positive ;
> Sex:Sex_T:=S;------please look at here first
> case S is
> when Male =>
> Has_Wife:Boolean ;
> when Female=>
> Has_Husband:Boolean;
> end case;
> end record;--person end
> Me : Person;
> she : Person (Female); ---- and this one ,
> begin
> Me:=(
> S => Male,
> Sex => Male,
> Age => 33,
> Has_Wife => True);
> she:=(
> S => Female,
> Sex => Male, ---please look at here now!!!!!
>
> Age => 23,
> Has_Husband => True);
> Put(Sex_T'Image(she.Sex));
> end test;
>
> ****
> there is not any alert
> and result is
> " male " ??
>
> if i dont put "sex=>male" , or "sex=>female"
> it dont let me to use a named association...
>
> even is i dont put " s=> female "
> it dont let me too ,
> but i said about discriminant
> when i declare the variable ( she : person(female) )
>
> why its not possible ?
> to put
> she:=(age=>23,has_husband=>true);
>
>
> regards
>
> _______________________________________________
> comp.lang.ada mailing list
> comp.lang.ada@ada-france.org
> http://www.ada-france.org/mailman/listinfo/comp.lang.ada
>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: a beginner question
2004-10-20 23:01 a beginner question mr1yh1
2004-10-20 23:53 ` Björn Persson
2004-10-21 11:47 ` Marius Amado Alves
@ 2004-10-21 18:16 ` mr1yh1
2 siblings, 0 replies; 4+ messages in thread
From: mr1yh1 @ 2004-10-21 18:16 UTC (permalink / raw)
thank you for all comments
i read more about discriminants
and i realised
they are different things that i used to use till now :)...
i realise my mistake too ,
i made it possible to access a variable in two ways
( first discriminant give a value ,but its a record and its possible to
change it, in normal ways )
and i see now
the right way to use discriminants give a chance to do something which is
not possible in another way ...
i saw in the examples:
-- they make possible to initialize the types of variables
( arrays,strings ...)
-- and they make possible to use different spesifications in same record
for different discriminant values
regards
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2004-10-21 18:16 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-10-20 23:01 a beginner question mr1yh1
2004-10-20 23:53 ` Björn Persson
2004-10-21 11:47 ` Marius Amado Alves
2004-10-21 18:16 ` mr1yh1
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox