comp.lang.ada
 help / color / mirror / Atom feed
From: Niklas Holsti <niklas.holsti@tidorum.invalid>
Subject: Re: Help on record to a Newbie
Date: Fri, 28 Dec 2012 10:00:06 +0200
Date: 2012-12-28T10:00:06+02:00	[thread overview]
Message-ID: <ak51s6Fna2qU1@mid.individual.net> (raw)
In-Reply-To: <8b12ba84-4753-4b57-b026-ab2bfb148a4a@googlegroups.com>

On 12-12-28 01:53 , Cedric wrote:
> Am Donnerstag, 27. Dezember 2012 20:31:19 UTC+1 schrieb Cedric:
> 
> Hi All,
> 
> many thanks for all your help.
> 
> I think you got the point: I missed to define the specification and
> the body of the package.

That is not the whole point. The point is that executable statements,
like your assignment statements, must be written in a different place
than declarations. Most executable statements go in subprograms
(procedures or functions). Your package has no subprograms; don't you
think that it needs some? At least a main procedure? Or do you plan to
put subprograms in other packages?

> The things is: I have learnt Pascal sometime and liked it a lot. I
> have still a book about Pascal containing a set of units that helped

Aha, "unit": you have used Turbo Pascal, not standard Pascal. Good,
because the "unit" concept has some similarity with the Ada package
concept. But be warned that they are not identical.

> me learn programming in Pascal and write my own programs. I thought
> it would be a good training to use the little knowledge of Ada I have
> so far and implement the Pascal library in Ada.

It seems that you are starting from the Pascal style and trying to
implement bits of Pascal code in Ada, one bit at a time. This can teach
you how details of Pascal map to details in Ada, but you also (and
mainly, IMO) need to understand how an Ada program is put together
overall, including the differences between subprograms and packages.
That understanding seems to be missing still.

> I think I will come across a lot of difficulties.

I think you will avoid many difficulties if you start by learning some
Ada from its basics, and later come back to your Pascal-translation
approach.

> @Jeffrey: What is so bad about my programming style? I thought it
> would be good to have a letter indicating the library I write.

I'm not Jeffrey, but I don't like your naming style either. Of course,
this is a personal and subjective choice; today I don't like the naming
style that I used 15 years ago, and now I prefer the "standard" styles
recommended for Ada programming.

You have to decide if you are going to use the Ada "use" clause a lot,
or not.

The names of things (types, variables, subprograms) declared in an Ada
package declaration are not, by default, usable as such from another
package. In your case, if you want to use your "t_decl" package from
some other package, you write at the start of the other package file:

   with t_decl;  -- Warning! Not the same as "with" in Pascal!

and when you then refer to things in t_decl, you use qualified names of
the form

   t_decl.t_str2_type

So you see that the "t_" prefix is redundant on the things inside the
package. It would be simpler to omit the "t_" prefix within the package,
and then you would write

   t_decl.str2_type

(I don't know if you intend the package name "t_decl" to be the final
name, but to me it seems a poor one. Is the prefix "t_" intended to
identify a library? Then it is too short. Is the "_decl" suffix intended
to explain that the package contains declarations? Well, most packages
contain declarations, so this part of the name is too general. In my
opinion, of course.)

Some Ada programmers like to avoid the need for qualified naming, at
least in some cases, and they use the Ada "use" clause to avoid it. If
you want to do this, you start the other package with this:

   with t_decl;
   use  t_decl;

and then you can refer to items from t_decl directly, for example as

   t_string2_type

You can still also use the qualified name, t_decl.t_string2_type. The
"use" clause can also be written more locally, for example among the
local declarations of a subprogram, and then applies only within that
subprogram.

I prefer to avoid "use" clauses generally, and only use them in special
cases. I find it easier to read and understand programs where items from
other packages are named in the qualified form, including the package
name. This means that I don't need to put any special prefixes or
suffixes on the names I declare within packages, because the package
name itself will be a used as a prefix for the names, when the names are
used in other packages.

> Another question: How do you distinguish between types and variables.
> When reading my programs I find it a lot easier to see directly if it
> is a type or a variable. What does your programming style look like?

I use the "_T" suffix for type names, and no suffix for variables.

   type Tree_T is (Birch, Fir, Aspen, Oak);

   Tree : Tree_T;

> @Niklas: Please find below the complete file:

Some comments on it:

>   subtype t_str2_type is string (2);

Not a valid subtype declaration. When you constrain an array index
range, you must give both lower and upper bounds, for example like this:

   subtype t_str2_type is string (1..2);

(GNAT did not complain about this to you, because it first found the
other errors (statements where declarations were expected) later in the
file, and abandonded the compilation. This is an irritating feature in
GNAT, but nothing is perfect...)

>   t_yes_no : t_yes_no_type;

GNAT says t_decl.ads:125:14: "t_yes_no_type" is undefined

>  -- A type register has to be defined. This type shall be
>  -- used to access the processor registers of a standard
>  -- Intel processor.

Why? What are you trying to do, that requires access to processor registers?

As I said, the following are statements and cannot be written after the
declarations, but must be placed in a subprogram or in the executable
statement block of the package body:

>   t_config.terminal   := black_n_white;
>   t_config.background := black;
>   t_config.foreground := white;

If you only want to give t_config an initial value, you can do it in the
declaration of t_config, like this:

  t_config : t_configure_type := (
    terminal   => black_n_white,
    background => black,
    foreground => white);

>   -- Set the terminal background color
>   -- Set the terminal foreground color
>   -- to be done, but do not know how to do it
>
>   -- Clear the screen
>   -- to be done, but do not know how to do it

For alphanumeric screen control, you have the option of directly using
standard (ANSI) control codes, or some OS-specific system such as
"curses" on Unix-like systems. I think others on comp.lang.ada can help
you find Ada bindings to such tools.

-- 
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
      .      @       .



  reply	other threads:[~2012-12-28  8:00 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-12-27 19:31 Help on record to a Newbie Cedric
2012-12-27 19:50 ` Jeffrey Carter
2012-12-27 19:51 ` Niklas Holsti
2012-12-27 20:25 ` Shark8
2012-12-27 23:53 ` Cedric
2012-12-28  8:00   ` Niklas Holsti [this message]
2012-12-28  8:30     ` Cedric
2012-12-28  9:29       ` Simon Wright
2012-12-30 17:29       ` Niklas Holsti
2012-12-28 11:52 ` Cedric
2012-12-28 15:36   ` Shark8
2012-12-30 16:03   ` Niklas Holsti
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox