comp.lang.ada
 help / color / mirror / Atom feed
From: jsa@alexandria (Jon S Anthony)
Subject: Re: overload ":=" ???
Date: 1996/07/18
Date: 1996-07-18T00:00:00+00:00	[thread overview]
Message-ID: <JSA.96Jul17203044@alexandria> (raw)
In-Reply-To: 31ED3F5F.1135B4EA@jinx.sckans.edu


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





  parent reply	other threads:[~1996-07-18  0:00 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-07-17  0:00 overload ":=" ??? David Morton
1996-07-17  0:00 ` Robert Dewar
1996-07-18  0:00   ` David Morton
1996-07-19  0:00     ` David Weller
1996-07-19  0:00     ` Brad Balfour
1996-07-18  0:00   ` Laurent Guerby
1996-07-25  0:00   ` Wolfgang Gellerich
1996-07-25  0:00     ` Robert A Duff
1996-07-18  0:00 ` Jon S Anthony [this message]
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-19  0:00 ` Joerg Ozimek
replies disabled

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