comp.lang.ada
 help / color / mirror / Atom feed
* overload ":=" ???
@ 1996-07-17  0:00 David Morton
  1996-07-17  0:00 ` Robert Dewar
                   ` (3 more replies)
  0 siblings, 4 replies; 28+ messages in thread
From: David Morton @ 1996-07-17  0:00 UTC (permalink / raw)



Is there a way to overload the assignment operator ":=" ?


ie:

with Text_IO; use Text_IO;
procedure Test is
type Text;
type Text_Ptr is access Text;

type Text is record
   Buf : String(1..50);
   Length : Natural;
end record;

function ":="(Right : String) is
begin

...

end ":=";

This would be a very helpful construct...

Thanks!


-- 
David Morton
 mailto:dmorton@jinx.sckans.edu   http://www.sckans.edu/~dmorton/
 205 College, Winfield, KS 67156    
 This signature will self-destruct in 10 seconds...




^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: overload ":=" ???
  1996-07-17  0:00 overload ":=" ??? David Morton
@ 1996-07-17  0:00 ` Robert Dewar
  1996-07-18  0:00   ` Laurent Guerby
                     ` (2 more replies)
  1996-07-18  0:00 ` John Herro
                   ` (2 subsequent siblings)
  3 siblings, 3 replies; 28+ messages in thread
From: Robert Dewar @ 1996-07-17  0:00 UTC (permalink / raw)



David Morton asks

Is there a way to overload the assignment operator ":=" ?


No, not directly (for many reasons the kind of simple minded approach
you suggested, though it seems reasonable at first, does not work).

(i.e. cannot easily be made to work from a language desigfn point of view)

However, look at non-limited controlled types and the Adjust procedure.
I think you will find you can do what you want!





^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: overload ":=" ???
  1996-07-17  0:00 ` Robert Dewar
@ 1996-07-18  0:00   ` Laurent Guerby
  1996-07-18  0:00   ` David Morton
  1996-07-25  0:00   ` Wolfgang Gellerich
  2 siblings, 0 replies; 28+ messages in thread
From: Laurent Guerby @ 1996-07-18  0:00 UTC (permalink / raw)



Robert> However, look at non-limited controlled types and the Adjust
Robert> procedure.  I think you will find you can do what you want!

David> That looks interesting, but how does it work???

   You can have a look at the Rationale:

   http://lglwww.epfl.ch/Ada/LRM/9X/Rationale/rat95html/rat95-p2-7.html#4

   Or at the Lovelace tutorial:

   http://lglwww.epfl.ch/Ada/Tutorials/Lovelace/s7-f.htm

-- 
Laurent Guerby <guerby@gnat.com>, Team Ada.
   "Use the Source, Luke. The Source will be with you, always (GPL)."




^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: overload ":=" ???
  1996-07-17  0:00 overload ":=" ??? David Morton
  1996-07-17  0:00 ` Robert Dewar
  1996-07-18  0:00 ` John Herro
@ 1996-07-18  0:00 ` Jon S Anthony
  1996-07-19  0:00 ` Joerg Ozimek
  3 siblings, 0 replies; 28+ messages in thread
From: Jon S Anthony @ 1996-07-18  0:00 UTC (permalink / raw)



In article <31ED3F5F.1135B4EA@jinx.sckans.edu> David Morton <dmorton@jinx.sckans.edu> writes:

> Is there a way to overload the assignment operator ":=" ?

Not exactly, but you can typically get close to equivalent
functionality with controlled types (RM 7.6 "User-Defined Assignment
and Finalization")

However, your example is something where a "converter" would be the
more natural approach in Ada.

> with Text_IO; use Text_IO;
> procedure Test is
> type Text;
> type Text_Ptr is access Text;
> 
> type Text is record
>    Buf : String(1..50);
>    Length : Natural;
> end record;
>
> function ":="(Right : String) is

function to_text ( right : string ) return text is
    res : text;
begin
    if right'length > text.buf'length then
        raise ...
    else
        res.buf(right'range) := right;
        res.length := right'length;
    end if;
    return res;
end to_text;

...

    t : text := to_text("hi there!");

A variation that some use is to name the "to_text" function as a unary
operator like "+" or something.  Then you get:

    t : text := +"hi there!";

/Jon
-- 
Jon Anthony
Organon Motives, Inc.
1 Williston Road, Suite 4
Belmont, MA 02178

617.484.3383
jsa@organon.com





^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: overload ":=" ???
  1996-07-17  0:00 overload ":=" ??? David Morton
  1996-07-17  0:00 ` Robert Dewar
@ 1996-07-18  0:00 ` John Herro
  1996-07-18  0:00   ` Robert Dewar
  1996-07-18  0:00 ` Jon S Anthony
  1996-07-19  0:00 ` Joerg Ozimek
  3 siblings, 1 reply; 28+ messages in thread
From: John Herro @ 1996-07-18  0:00 UTC (permalink / raw)



David Morton <dmorton@jinx.sckans.edu> writes:
>Is there a way to overload the assignment operator ":=" ?
     Unfortunately, no.  However, you can write your own procedure Assign
or Set.  The difficulty with that is that you may forget to call it, and
use the predefined := instead.
     The workaround is to make the type of the object you're assigning
limited private.  Then the compiler will force you to call your Set
procedure, and won't allow you accidentally to use := (outside the
package).  For example:

package P is
   type Stack is limited private;
   ...
   procedure Set (Left: out Stack; To: in Stack);
end P;

with P; use P;
procedure Main is
   A, B : Stack;
   ...
begin
   ...
   Set(A, To => B); -- Legal
   A := B; -- Illegal.  Compiler will catch this.
end Main;
     There's more information about when to use private types and when to
use limited private types in my Ada Tutor program.  I hope this helps.
- John Herro
Software Innovations Technology
http://members.aol.com/AdaTutor
ftp://members.aol.com/AdaTutor




^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: overload ":=" ???
  1996-07-18  0:00 ` John Herro
@ 1996-07-18  0:00   ` Robert Dewar
  1996-07-19  0:00     ` John Herro
  0 siblings, 1 reply; 28+ messages in thread
From: Robert Dewar @ 1996-07-18  0:00 UTC (permalink / raw)



John Herro, answering a question from David said

  ">Is there a way to overload the assignment operator ":=" ?
     Unfortunately, no.  However, you can write your own procedure Assign
  or Set.  The difficulty with that is that you may forget to call it, and
  use the predefined := instead.
     The workaround is to make the type of the object you're assigning
  limited private.  Then the compiler will force you to call your Set
  procedure, and won't allow you accidentally to use := (outside the
  package)."

This seems the wrong advice. In Ada 95, nearly all cases where you want
to redefine assignment can be nicely handled by using non-limited controlled
types and redefining Adjust appropriately. I am not clear as to why John
omitted this most obvious response. Perhaps he was answering in the
framework of Ada 83 (where controlled types are not available).





^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: overload ":=" ???
  1996-07-17  0:00 ` Robert Dewar
  1996-07-18  0:00   ` Laurent Guerby
@ 1996-07-18  0:00   ` David Morton
  1996-07-19  0:00     ` Brad Balfour
  1996-07-19  0:00     ` David Weller
  1996-07-25  0:00   ` Wolfgang Gellerich
  2 siblings, 2 replies; 28+ messages in thread
From: David Morton @ 1996-07-18  0:00 UTC (permalink / raw)



Robert Dewar wrote:
> 
> David Morton asks
> 
> Is there a way to overload the assignment operator ":=" ?
> 
> No, not directly (for many reasons the kind of simple minded approach
> you suggested, though it seems reasonable at first, does not work).
> 
> (i.e. cannot easily be made to work from a language desigfn point of view)
> 
> However, look at non-limited controlled types and the Adjust procedure.
> I think you will find you can do what you want!

That looks interesting, but how does it work???

:)



-- 
David Morton
 mailto:dmorton@jinx.sckans.edu   http://www.sckans.edu/~dmorton/
 205 College, Winfield, KS 67156    
 This signature will self-destruct in 10 seconds...




^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: overload ":=" ???
  1996-07-18  0:00   ` David Morton
  1996-07-19  0:00     ` Brad Balfour
@ 1996-07-19  0:00     ` David Weller
  1 sibling, 0 replies; 28+ messages in thread
From: David Weller @ 1996-07-19  0:00 UTC (permalink / raw)



In article <31EE43DA.79E97C27@jinx.sckans.edu>,
David Morton  <dmorton@jinx.sckans.edu> wrote:
>Robert Dewar wrote:
>> However, look at non-limited controlled types and the Adjust procedure.
>> I think you will find you can do what you want!
>
>That looks interesting, but how does it work???
>

Fire up we browser and point to...

	http://www.acm.org/~bbalfour/tips_no_1.html

All shall be answered! :-)

-- 
    Visit the Ada 95 Booch Components Homepage: www.ocsystems.com/booch
           This is not your father's Ada -- lglwww.epfl.ch/Ada




^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: overload ":=" ???
  1996-07-18  0:00   ` Robert Dewar
@ 1996-07-19  0:00     ` John Herro
  1996-07-21  0:00       ` David Morton
                         ` (6 more replies)
  0 siblings, 7 replies; 28+ messages in thread
From: John Herro @ 1996-07-19  0:00 UTC (permalink / raw)



dewar@cs.nyu.edu (Robert Dewar) writes:
>[John Herro seemed to give the wrong advice,
>about using limited private types]... Perhaps
>he was answering in the framework of Ada 83
>(where controlled types are not available).
     Robert, you caught me with my pants down!  Although I have a
Janus/Ada 95 compiler, I do almost all my programming with a Janus/Ada 83
compiler or a Meridian Ada 83 compiler, because the 83 compilers produce
native code, while the 95 produces an .EXP file which requires an
interpreter.  That's why I was "thinking in Ada 83."  Fortunately, I'll be
acquiring Gnat in a few days!
     What made matters worse is that my post took a long time to appear in
comp.lang.ada, after David had already received the "correct" advice about
non-limited controlled types.  Sorry!
     Once I acquire the Gnat compiler, I expect to switch from Ada 83 to
Ada 95 for most of my programming.  Then I hope to add more Ada 95
information to my Ada Tutor program.
- John Herro
Software Innovations Technology
http://members.aol.com/AdaTutor
ftp://members.aol.com/AdaTutor




^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: overload ":=" ???
  1996-07-17  0:00 overload ":=" ??? David Morton
                   ` (2 preceding siblings ...)
  1996-07-18  0:00 ` Jon S Anthony
@ 1996-07-19  0:00 ` Joerg Ozimek
  3 siblings, 0 replies; 28+ messages in thread
From: Joerg Ozimek @ 1996-07-19  0:00 UTC (permalink / raw)



David Morton <dmorton@jinx.sckans.edu> wrote:
>> Is there a way to overload the assignment operator ":=" ?


The language (-> LRM 4.5) defines only the following six categories of
operators. The corresponding operator_symbols, and only those, can be
used as designators in declarations of functions for user-defined
operators. 

  logical_operator            ::=  and | or  | xor
  relational_operator         ::=  =   | /=  | <   | <= | > | >=
  binary_adding_operator      ::=  +   | -   | &
  unary_adding_operator       ::=  +   | -
  multiplying_operator        ::=  *   | /   | mod | rem
  highest_precedence_operator ::=  **


Therefore the assignment_statement ":=" is not an operator and 
can't be overloaded! :-((


But you have the ability to control the effects of an assignement by
inheriting your own "controlled type" from the private tagged type
Controlled. You find it in the predefined package Ada.Finalization 
(-> LRM 7.6).

----------------------------------------------------
with Ada.Finalization;
use  Ada.Finalization;
package MyText is
   type Text is new Controlled with private;
   procedure Adjust (Object : in out Text);
private
   type Text is new Controlled with
      record
         Buffer : String(1..50);
         Length : Natural := 0;
      end record;
end MyText;
----------------------------------------------------
with Ada.Text_IO;
use  Ada.Text_IO;
package body MyText is
   procedure Adjust (Object : in out Text) is
   begin
      Put_Line ("Here is the text : " & 
                Object.Buffer(1 .. Object.Length));
      -- here you can place some usefull control code,
      -- which is executed after an assignement ...
   end Adjust;
end MyText;
----------------------------------------------------



Joerg Ozimek





^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: overload ":=" ???
  1996-07-18  0:00   ` David Morton
@ 1996-07-19  0:00     ` Brad Balfour
  1996-07-19  0:00     ` David Weller
  1 sibling, 0 replies; 28+ messages in thread
From: Brad Balfour @ 1996-07-19  0:00 UTC (permalink / raw)



In article <31EE43DA.79E97C27@jinx.sckans.edu>, David Morton
<dmorton@jinx.sckans.edu> wrote:

>Robert Dewar wrote:
>> However, look at non-limited controlled types and the Adjust procedure.
>> I think you will find you can do what you want!
>That looks interesting, but how does it work???

If you are looking for a simple example of controlled types and the Adjust
prodcedure to implement user defined assignment, you may be interested in
an article I wrote for Ada letters. It is "Ada 95 Tips & Tidbits Number 1:
User Defined Assignment", published in Ada Letters, Vol. XIV, No. 5,
September/October, 1994.

You can find an html copy on the www at:
    <http://www.acm.org/~bbalfour/Tips_No_1.html>

Brad

-- 
Brad Balfour                         SIGAda WWW Server
CACI, Inc.                              http://www.acm.org/sigada/
703/277-6767                         and also try:
bbalfour@std.caci.com                   http://lglwww.epfl.ch/Ada/
bbalfour@sw-eng.falls-church.va.us
bbalfour@acm.org
**new address: 3930 Pender Drive * Fairfax, VA 22030 * 703/277-6767**




^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: overload ":=" ???
  1996-07-19  0:00     ` John Herro
@ 1996-07-21  0:00       ` David Morton
  1996-07-21  0:00       ` Laurent Guerby
                         ` (5 subsequent siblings)
  6 siblings, 0 replies; 28+ messages in thread
From: David Morton @ 1996-07-21  0:00 UTC (permalink / raw)



Well, so far, the adjust procedure doesn't
seem to do what I want...  Like someone else pointed
out, I'm really wanting to do a conversion.
But it would be nice to have it done automatically.


like my example:

type Text is record
  Buffer : String(1..80);
  Length : Natural;
end record;

...

a : Text;
begin
a := "test";  -- would like this to set both fields accordingly...

a := To_Text("test"); -- seems too wordy

If that can't be done, then,
IMHO, there is *one* way that C++ is better...
but the only one so far.

why is this so hard to implement??


-- 
David Morton
 mailto:dmorton@jinx.sckans.edu   http://www.sckans.edu/~dmorton/
 205 College, Winfield, KS 67156    
 This signature will self-destruct in 10 seconds...




^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: overload ":=" ???
  1996-07-19  0:00     ` John Herro
  1996-07-21  0:00       ` David Morton
@ 1996-07-21  0:00       ` Laurent Guerby
  1996-07-22  0:00         ` Robert A Duff
  1996-07-22  0:00       ` David Morton
                         ` (4 subsequent siblings)
  6 siblings, 1 reply; 28+ messages in thread
From: Laurent Guerby @ 1996-07-21  0:00 UTC (permalink / raw)



David> like my example:
[...]

   You don't have to do write anything to use this kind of data
structures in Ada 95, there's a language-defined package called
Ada.Strings.Bounded that does exactly what you want, RM95-A.4.4.

David> a := To_Text("test"); -- seems too wordy

   If you are in a race where the goal is to type the minimal number
of character to write your code, I can tell you: ada is not the right
language (except in the case of parallelism since it's built in in the
language), use C/Fortran/C++/whatever and have a look at the
obfuscated C/C++ contests.

   In this case, you can write in Ada:

   function "+" (X : String) renames To_Text; -- or whatever

   a := +"test"; 

David> If that can't be done, then, IMHO, there is *one* way that C++
David> is better...  but the only one so far.

   If way = "type less characters", then you're right. 

David> why is this so hard to implement??

   If this = "the data structure", it's zero cost to implement it
since it's already in the language. (Oh well, I guess it's very hard
since it requires you learn Ada 95 and not just try to translate C++
in pseudo-Ada).

-- 
Laurent Guerby <guerby@gnat.com>, Team Ada.
   "Use the Source, Luke. The Source will be with you, always (GPL)."




^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: overload ":=" ???
  1996-07-22  0:00       ` David Morton
@ 1996-07-22  0:00         ` Robert Dewar
  1996-07-23  0:00         ` Robert A Duff
  1 sibling, 0 replies; 28+ messages in thread
From: Robert Dewar @ 1996-07-22  0:00 UTC (permalink / raw)



David Morton said, replying to Laurent

>    If you allow programmers to play with assignment, you'll definitly
> damage the readability of all Ada sources.

what about "write-ability" ????


Ah! Interesting you should ask, the answer is we do NOT care at all -- well
that's a little strong, but one of the critical design principles of Ada
is that we always emphasize readability over writability -- why? simple,
we are more interested in maintability than saving a bit of coding time,
since the latter is such a small part of the total life-cycle cost.

I stronly agree with Laurent that allowing general redefinition of assignment
is likely to encourage (mis)uses that would tend to damage readability. Yes,
I know you can argue that allowing redefinition of operators is also a risk,
and indeed I have seen this badly abused on occasion, but I think assignment
is more fundamental. The nice thing about the use of Adjust is that it allows
quite a bit of flexibility without allowing the undesirable introduction of
implicit conversions (or 
something even worse)





^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: overload ":=" ???
  1996-07-21  0:00       ` Laurent Guerby
@ 1996-07-22  0:00         ` Robert A Duff
  1996-07-23  0:00           ` Laurent Guerby
  0 siblings, 1 reply; 28+ messages in thread
From: Robert A Duff @ 1996-07-22  0:00 UTC (permalink / raw)



In article <ws9d91pcshi.fsf@schonberg.cs.nyu.edu>,
Laurent Guerby <guerby@gnat.com> wrote:
>David> a := To_Text("test"); -- seems too wordy
>
>   If you are in a race where the goal is to type the minimal number
>of character to write your code, I can tell you: ada is not the right
>language ...

I agree with David here.  The problem is not how much you have to type.
The problem is extra verbosity for the person READING the code.  Extra
verbosity is good if and only if it adds useful information for the
reader of the program.  Here, we have a character-string data type, and
yet we can't directly use string literals (or indexing, slicing, etc),
just because it doesn't happen to be the built in sort of string.  (The
built-in string types are no good, because they aren't dynamic enough
for this application.)

The To_Text conversion is just extra junk -- there is no "conversion"
going on at the conceptual level.

Note that the suggestion of calling it "+" proves my point -- if there
were really a useful operation going on here (at the conceptual level),
then everybody would agree that it should have a clear name, and calling
it "+" would be intolerable.

- Bob




^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: overload ":=" ???
  1996-07-19  0:00     ` John Herro
                         ` (3 preceding siblings ...)
  1996-07-22  0:00       ` Laurent Guerby
@ 1996-07-22  0:00       ` Laurent Guerby
  1996-07-23  0:00       ` Tarjei Jensen
  1996-07-24  0:00       ` Robert I. Eachus
  6 siblings, 0 replies; 28+ messages in thread
From: Laurent Guerby @ 1996-07-22  0:00 UTC (permalink / raw)



Bob> I agree with David here.  The problem is not how much you have to
Bob> type.  The problem is extra verbosity for the person READING the
Bob> code.  Extra verbosity is good if and only if it adds useful
Bob> information for the reader of the program.  

   Well, I guess it depends on what background you have on Ada (;-),
when I read in an Ada source.

X := "sdfsfg";

   I know that X is a fixed string (or wide_string). When I read:

X := To_X ("sgsgs");

   I know that a change in representation is on the way (may be X
isn't a fixed string after all), and it's not extra verbosity.

Bob> Here, we have a
Bob> character-string data type, and yet we can't directly use string
Bob> literals (or indexing, slicing, etc), just because it doesn't
Bob> happen to be the built in sort of string.  (The built-in string
Bob> types are no good, because they aren't dynamic enough for this
Bob> application.)

   This "sort of string" is built in Ada 95 under the name
Bounded_String.

Bob> The To_Text conversion is just extra junk -- there is no
Bob> "conversion" going on at the conceptual level.

   "conversion" at the conceptual level is pretty vague. What about

X : Float := 1; -- What, that's illegal? 
-- Why should add ".0"? At the conceptual level there's no need for it.

Y : Integer := "32"; -- grr...

Z : String := 'A'; -- ...

   If you allow programmers to play with assignment, you'll definitly
damage the readability of all Ada sources.

-- 
Laurent Guerby <guerby@gnat.com>, Team Ada.
   "Use the Source, Luke. The Source will be with you, always (GPL)."




^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: overload ":=" ???
  1996-07-19  0:00     ` John Herro
  1996-07-21  0:00       ` David Morton
  1996-07-21  0:00       ` Laurent Guerby
@ 1996-07-22  0:00       ` David Morton
  1996-07-22  0:00         ` Robert Dewar
  1996-07-23  0:00         ` Robert A Duff
  1996-07-22  0:00       ` Laurent Guerby
                         ` (3 subsequent siblings)
  6 siblings, 2 replies; 28+ messages in thread
From: David Morton @ 1996-07-22  0:00 UTC (permalink / raw)



>    Well, I guess it depends on what background you have on Ada (;-),
> when I read in an Ada source.
> 
> X := "sdfsfg";
> 
>    I know that X is a fixed string (or wide_string). When I read:
> 
> X := To_X ("sgsgs");
> 
>    I know that a change in representation is on the way (may be X
> isn't a fixed string after all), and it's not extra verbosity.

A semi-good point, but I still don't care...  Hopefully
X isn't defined so far away that I can't remember what it is...


>    This "sort of string" is built in Ada 95 under the name
> Bounded_String.

I looked at that, and it would be great, but...
RM A.04.04.106
seems to say that this isn't good with dynamic pointers
(or am I misreading this?)


>    If you allow programmers to play with assignment, you'll definitly
> damage the readability of all Ada sources.

what about "write-ability" ????

-- 
David Morton
 mailto:dmorton@jinx.sckans.edu   http://www.sckans.edu/~dmorton/
 205 College, Winfield, KS 67156    
 This signature will self-destruct in 10 seconds...




^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: overload ":=" ???
  1996-07-23  0:00         ` Robert A Duff
@ 1996-07-22  0:00           ` Robert Dewar
  0 siblings, 0 replies; 28+ messages in thread
From: Robert Dewar @ 1996-07-22  0:00 UTC (permalink / raw)



David Morton  <dmorton@jinx.sckans.edu> wrote:
>I looked at that, and it would be great, but...
>RM A.04.04.106
>seems to say that this isn't good with dynamic pointers
>(or am I misreading this?)



something doesn't compute here :-)

In your example, you didnt' use pointers either, but rather a record
structure that is almost EXACTLY what is intended by the above
implementation advice.

Don't get confused here, that paragraph is not saying you can't have
pointers (accesses) to bounded strings, it is just advising that you
should not use pointers in the implementation of the type itself. 

It really does seem like bounded strings are EXACTLY what you want!





^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: overload ":=" ???
  1996-07-19  0:00     ` John Herro
                         ` (2 preceding siblings ...)
  1996-07-22  0:00       ` David Morton
@ 1996-07-22  0:00       ` Laurent Guerby
  1996-07-22  0:00       ` Laurent Guerby
                         ` (2 subsequent siblings)
  6 siblings, 0 replies; 28+ messages in thread
From: Laurent Guerby @ 1996-07-22  0:00 UTC (permalink / raw)



Laurent> This "sort of string" is built in Ada 95 under the name
Laurent> Bounded_String.

David> I looked at that, and it would be great, but...  RM A.04.04.106
David> seems to say that this isn't good with dynamic pointers (or am
David> I misreading this?)

   First, here is the GNAT implementation of Bounded_String:

   private

      type Bounded_String is record
         Length : Length_Range := 0;
         Data   : String (1 .. Max_Length);
      end record;

   So I guess it's very close to what you want, with a big bonus, all
Ada 95 String handling routines are waiting to be used. Note that
you'll need to instanciate a generic to use it.

   Second, A.4.4(106) is an "implementation advice" which states:

RM>			       Implementation Advice
RM>
RM> 106   Bounded string objects should not be implemented by implicit pointers
RM> and dynamic allocation.

   As far as I understand it, it advices (not requires) compiler
writers not to use a pointer and "new" to implement this type (this
kind of stuff is for Unbounded_Strings). This is not at all a concern
to the user, who sees only "type Bounded_String is private".

David> what about "write-ability" ????

   Please define "write-ability" (I don't want to assume it's related
to the number of characters to type ;-).

David> This signature will self-destruct in 10 seconds...

   Boom ;-).

-- 
Laurent Guerby <guerby@gnat.com>, Team Ada.
   "Use the Source, Luke. The Source will be with you, always (GPL)."




^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: overload ":=" ???
  1996-07-19  0:00     ` John Herro
                         ` (4 preceding siblings ...)
  1996-07-22  0:00       ` Laurent Guerby
@ 1996-07-23  0:00       ` Tarjei Jensen
  1996-07-23  0:00         ` Robert A Duff
  1996-07-24  0:00       ` Robert I. Eachus
  6 siblings, 1 reply; 28+ messages in thread
From: Tarjei Jensen @ 1996-07-23  0:00 UTC (permalink / raw)



The problem with bounded string is that it assigns a global maximum string size
instead of letting each string have its own maximum size. The former is not 
particularly flexible while the latter really require a user defined ":=".

Not the least because the maximum length of the strings might be different.

e.g:

  a : counted_string(4) := "1234";
  b : counted_string(5) := "1234";  -- four characters and space for five

  a := b;  -- Will not work with current version of Ada

In this case an user defined assignment operator would be required to make
things work or the very least a pragma to tell the compiler what the actual
size of the string is. e.g.:

type counted_string ( maximum : positive ) is record
    size : natural;
    str  : string( maximum );
end record;

pragma size_controller( counted_string.str, counted_string.size);
  -- will check  size <= maximum
  -- will check a.maximum >= b.size in example above


While I am in wishing mode I would very much like a third alternative to the
values of S'Bit_Order. I would very much like to have Most_Significant_Byte_First
as a choice (Network order for those who are curious).

 


Greetings,

 
 
--
// Tarjei T. Jensen 
//    tarjeij@ulrik.uio.no || fax +47 51664292  || voice +47 51 85 87 39
//   Support you local rescue centre: GET LOST!
// Working, but not speaking for the Norwegian Hydrographic Service.




^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: overload ":=" ???
  1996-07-22  0:00       ` David Morton
  1996-07-22  0:00         ` Robert Dewar
@ 1996-07-23  0:00         ` Robert A Duff
  1996-07-22  0:00           ` Robert Dewar
  1 sibling, 1 reply; 28+ messages in thread
From: Robert A Duff @ 1996-07-23  0:00 UTC (permalink / raw)



In article <31F40F0C.3FDACA19@jinx.sckans.edu>,
David Morton  <dmorton@jinx.sckans.edu> wrote:
>I looked at that, and it would be great, but...
>RM A.04.04.106
>seems to say that this isn't good with dynamic pointers
>(or am I misreading this?)

Maybe.  It's just saying (to the Ada implementer) that Bounded_String
should be a record containing a character count, plus an array of
characters containing the max size (as opposed to some pointer-ish
implementation).  You can make pointers to these things if you want to
-- A.4.4(106) is not restricting the *user* in any way.

If you want unbounded strings (which *are* implemented using pointers),
then look at A.4.5.  These are more flexible but less efficient.

>>    If you allow programmers to play with assignment, you'll definitly
>> damage the readability of all Ada sources.

If you allow programmers to play with "+" and "=", you'll ... ;-)

>what about "write-ability" ????

Irrelevant.  If you want that, use perl (I'm echoing Laurent's earlier
remark).  ;-)

- Bob




^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: overload ":=" ???
  1996-07-22  0:00         ` Robert A Duff
@ 1996-07-23  0:00           ` Laurent Guerby
  1996-07-23  0:00             ` Robert A Duff
  1996-07-23  0:00             ` John Herro
  0 siblings, 2 replies; 28+ messages in thread
From: Laurent Guerby @ 1996-07-23  0:00 UTC (permalink / raw)



Laurent> If you allow programmers to play with assignment, you'll definitly
Laurent> damage the readability of all Ada sources.

Bob> If you allow programmers to play with "+" and "=", you'll ... ;-)

   Yes ;-). But in the case of "=", there are some strict rules
(returns boolean, "/=" implictly defined) that restrain your
creativity. 

   What about the evil:

   function "/" (Source : String; Max_Length : Natural) return Record_Type;
   --  Implementation left to the reader.

   X := "Hello" / 80; -- Better document somewhere the notation ;-).

-- 
Laurent Guerby <guerby@gnat.com>, Team Ada.
   "Use the Source, Luke. The Source will be with you, always (GPL)."




^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: overload ":=" ???
  1996-07-23  0:00           ` Laurent Guerby
@ 1996-07-23  0:00             ` Robert A Duff
  1996-07-23  0:00             ` John Herro
  1 sibling, 0 replies; 28+ messages in thread
From: Robert A Duff @ 1996-07-23  0:00 UTC (permalink / raw)



In article <ws9u3uzy5cv.fsf@schonberg.cs.nyu.edu>,
Laurent Guerby <guerby@gnat.com> wrote:
>   Yes ;-). But in the case of "=", there are some strict rules
>(returns boolean, "/=" implictly defined) that restrain your
>creativity. 

Actually, the rule in Ada 95 has changed -- you can define "=" to return
other than Boolean.  If it returns Boolean, then you get the magical
"/=" (which is a pain in the neck for RM authors and compiler writers).
There are at least two bugs in the RM caused by these magical "/=" ops.

- Bob




^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: overload ":=" ???
  1996-07-23  0:00       ` Tarjei Jensen
@ 1996-07-23  0:00         ` Robert A Duff
  0 siblings, 0 replies; 28+ messages in thread
From: Robert A Duff @ 1996-07-23  0:00 UTC (permalink / raw)



In article <TARJEIJ.96Jul23153224@ulrik.uio.no>,
Tarjei Jensen <tarjeij@ulrik.uio.no> wrote: The problem with bounded
>string is that it assigns a global maximum string size instead of
>letting each string have its own maximum size.

True.  But it's not particularly a problem with the bounded strings
package.  The problem is that discriminant constraints are checked for
equal-discrims on assignment.

>... The former is not
>particularly flexible while the latter really require a user defined
>":=".

Or a user-define "implicit conversion" operation.  An earlier version of
Ada 9X solved this problem.

>Not the least because the maximum length of the strings might be different.
>
>e.g:
>
>  a : counted_string(4) := "1234";
>  b : counted_string(5) := "1234";  -- four characters and space for five
>
>  a := b;  -- Will not work with current version of Ada

True.  The expectation is that you'll instantiate the generic
approximately once per program (or subsystem) with a "reasonable" max
such as 80, or 200, or whatever.

Note that typical implementations of Pascal that support a similar thing
usually choose the max for you (often 255).

Note also that bounded strings are for applications that want to avoid
the overhead and unpredictability of heap-allocated strings -- e.g.
many real-time/embedded applications.  If you want flexibility, use
unbounded strings, and pay for heap allocation.

Anyway, I agree that what you're asking for would be nice.  Not quite
nice enough to warrant changing rules about discriminant constraints,
apparently, though.

>While I am in wishing mode I would very much like a third alternative
>to the values of S'Bit_Order. I would very much like to have
>Most_Significant_Byte_First as a choice (Network order for those who
>are curious).

Shouldn't that be S'Byte_Order?  Ask you favorite compiler vendor for an
implementation-defined attribute.

- Bob




^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: overload ":=" ???
  1996-07-23  0:00           ` Laurent Guerby
  1996-07-23  0:00             ` Robert A Duff
@ 1996-07-23  0:00             ` John Herro
  1 sibling, 0 replies; 28+ messages in thread
From: John Herro @ 1996-07-23  0:00 UTC (permalink / raw)



guerby@gnat.com (Laurent Guerby) writes:
> in the case of "=", there are some strict rules
> (returns boolean, "/=" implictly defined) that
> restrain your creativity.
     For user-defined "=" in Ada 83, the result had to be Boolean, the two
parameters had to be of the same type, that type had to be a limited
private type, and "/=" was implicitly defined.
     For user-defined "=" in Ada 95, if I'm not mistaken, the result and
the parameters can be of any type.  *IF* the result is Boolean, then "/="
is implicitly defined, and if not, then we may optionally redefine "/=".
- John Herro
Software Innovations Technology
http://members.aol.com/AdaTutor
ftp://members.aol.com/AdaTutor
MUSIC is easier to read when written in C.
SOFTWARE is easier to read when written in Ada!





^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: overload ":=" ???
  1996-07-19  0:00     ` John Herro
                         ` (5 preceding siblings ...)
  1996-07-23  0:00       ` Tarjei Jensen
@ 1996-07-24  0:00       ` Robert I. Eachus
  6 siblings, 0 replies; 28+ messages in thread
From: Robert I. Eachus @ 1996-07-24  0:00 UTC (permalink / raw)



In article <TARJEIJ.96Jul23153224@ulrik.uio.no> tarjeij@ulrik.uio.no (Tarjei Jensen) writes:

  > The problem with bounded string is that it assigns a global
  > maximum string size instead of letting each string have its own
  > maximum size. The former is not particularly flexible while the
  > latter really require a user defined ":=".

   The generic package Ada.Strings.Bounded can be instantiated as
often as you want.  I have written program which had six
instantiations...  Well, not really.  It had five instantiations which
occured once per program invocation, and a sixth whose size was
determined every time the enclosing procedure was called.

  > Not the least because the maximum length of the strings might be different.

  > e.g:

  >   a : counted_string(4) := "1234";
  >   b : counted_string(5) := "1234";  -- four characters and space for five

  >   a := b;  -- Will not work with current version of Ada

   Try:

   ...
   package Counted is new Ada.Strings.Bounded.Generic_Bounded_Length(10);
   A : Counted.Bounded_String := To_Bounded_String("1234");
   B : Counted.Bounded_String := To_Bounded_String("Some value");
   ...
   A := B; --works fine.
   
   Of couse if instead you say:
   ...
   package Counted4 is new Ada.Strings.Bounded.Generic_Bounded_Length(4);
   package Counted5 is new Ada.Strings.Bounded.Generic_Bounded_Length(5);
   A : Counted4.Bounded_String := To_Bounded_String("1234");
   B : Counted5.Bounded_String := To_Bounded_String("1234");

   Now you have to write:
   ...
   A := Counted4.To_Bounded_String(Counted5.To_String(B));
   
   Of course, if you want, you can make all this less verbose by using
use clauses and perhaps even defining a conversion operation:

   function Convert(B: in Counted5.Bounded_String) 
           return Counted4.Bounded_String is
   begin return Counted4.To_Bounded_String(Counted5.To_String(B)); end;
   pragma Inline(Convert);

--

					Robert I. Eachus

with Standard_Disclaimer;
use  Standard_Disclaimer;
function Message (Text: in Clever_Ideas) return Better_Ideas is...




^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: overload ":=" ???
  1996-07-25  0:00   ` Wolfgang Gellerich
@ 1996-07-25  0:00     ` Robert A Duff
  0 siblings, 0 replies; 28+ messages in thread
From: Robert A Duff @ 1996-07-25  0:00 UTC (permalink / raw)



In article <4t7l2p$n41@zdi.informatik.uni-stuttgart.de>,
Wolfgang Gellerich <gelleric@kafka.informatik.uni-stuttgart.de> wrote:
>What is the problem with overloading := ?

Unconstrained discriminated records.  This problem is explained in
AARM-7.6(17.a - 17.h).

>... In the case discussed here, assigning
>a string literal to an unbounded strng variable, there should be no 
>difficulties ?

This case discussed here, actually, would be better addressed by having
user-defined literals, rather than user-defined assignment.  Or, it
could be addressed by having user-defined implicit conversion, but that
seems like overkill.  Consider that you might want:

    procedure P(X: Fancy_Dynamic_String);
    ...
    P("Hello, world.");

whereas Ada requires:

    P(To_Fancy_Dynamic_String("Hello, world."));

or:

    P(+("Hello, world."));

Assuming, that is, that you buy the premise of the original poster, who
said that `X := To_Fancy_Dynamic_String("Hello, world.");' is too
verbose.

- Bob




^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: overload ":=" ???
  1996-07-17  0:00 ` Robert Dewar
  1996-07-18  0:00   ` Laurent Guerby
  1996-07-18  0:00   ` David Morton
@ 1996-07-25  0:00   ` Wolfgang Gellerich
  1996-07-25  0:00     ` Robert A Duff
  2 siblings, 1 reply; 28+ messages in thread
From: Wolfgang Gellerich @ 1996-07-25  0:00 UTC (permalink / raw)



In article <dewar.837646128@schonberg>, dewar@cs.nyu.edu (Robert Dewar) writes:
|> David Morton asks
|> 
|> Is there a way to overload the assignment operator ":=" ?
|> 
|> 
|> No, not directly (for many reasons the kind of simple minded approach
|> you suggested, though it seems reasonable at first, does not work).
|> 
|> (i.e. cannot easily be made to work from a language desigfn point of view)

What is the problem with overloading := ? In the case discussed here, assigning
a string literal to an unbounded strng variable, there should be no 
difficulties ?

Regards,

  Wolfgang Gellerich




^ permalink raw reply	[flat|nested] 28+ messages in thread

end of thread, other threads:[~1996-07-25  0:00 UTC | newest]

Thread overview: 28+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-07-17  0:00 overload ":=" ??? David Morton
1996-07-17  0:00 ` Robert Dewar
1996-07-18  0:00   ` Laurent Guerby
1996-07-18  0:00   ` David Morton
1996-07-19  0:00     ` Brad Balfour
1996-07-19  0:00     ` David Weller
1996-07-25  0:00   ` Wolfgang Gellerich
1996-07-25  0:00     ` Robert A Duff
1996-07-18  0:00 ` John Herro
1996-07-18  0:00   ` Robert Dewar
1996-07-19  0:00     ` John Herro
1996-07-21  0:00       ` David Morton
1996-07-21  0:00       ` Laurent Guerby
1996-07-22  0:00         ` Robert A Duff
1996-07-23  0:00           ` Laurent Guerby
1996-07-23  0:00             ` Robert A Duff
1996-07-23  0:00             ` John Herro
1996-07-22  0:00       ` David Morton
1996-07-22  0:00         ` Robert Dewar
1996-07-23  0:00         ` Robert A Duff
1996-07-22  0:00           ` Robert Dewar
1996-07-22  0:00       ` Laurent Guerby
1996-07-22  0:00       ` Laurent Guerby
1996-07-23  0:00       ` Tarjei Jensen
1996-07-23  0:00         ` Robert A Duff
1996-07-24  0:00       ` Robert I. Eachus
1996-07-18  0:00 ` Jon S Anthony
1996-07-19  0:00 ` Joerg Ozimek

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