* redefinition of the operator
@ 2005-11-20 8:49 Francesco Bochicchio
2005-11-20 10:50 ` Dmitry A. Kazakov
2005-11-20 11:12 ` Martin Krischik
0 siblings, 2 replies; 10+ messages in thread
From: Francesco Bochicchio @ 2005-11-20 8:49 UTC (permalink / raw)
Hi all,
I need to redefine the standard mathematic operators, to replace them with
my own version.
So, if I do something like this, it works:
-- ******************************
procedure Test_MyOperators is
A,B : Long_Float;
function "+"(X, Y : Long_Float) return Long_Float
is
begin
return X;
end;
begin
A := 1.0;
B := 2.0;
if A = A+B then
Put_Line("Yes.");
else
Put_Line("No");
end if;
end;
-- -------------------------------------
But since I have many packages, I want to put the redefined operators
in a separate package, say MY_OPERATORS, and doing instead:
-- *************************************
with MY_OPERATORS; use MY_OPERATORS;
procedure Test_MyOperators is
A,B : Long_Float;
begin
A := 1.0;
B := 2.0;
if A = A+B then
Put_Line("Yes.");
else
Put_Line("No");
end if;
end;
-- -----------------------------------------------
But this does not work.The function Test_MyOperators
keep using the standard "+" instead of the redefined one
exported by MY_OPERATORS.
Anybody knows how to do this?
Thanks in advance.
Ciao
-------
FB
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: redefinition of the operator
2005-11-20 8:49 redefinition of the operator Francesco Bochicchio
@ 2005-11-20 10:50 ` Dmitry A. Kazakov
2005-11-20 12:16 ` Francesco Bochicchio
2005-11-20 11:12 ` Martin Krischik
1 sibling, 1 reply; 10+ messages in thread
From: Dmitry A. Kazakov @ 2005-11-20 10:50 UTC (permalink / raw)
On Sun, 20 Nov 2005 09:49:55 +0100, Francesco Bochicchio wrote:
> I need to redefine the standard mathematic operators, to replace them with
> my own version.
>
> So, if I do something like this, it works:
>
> -- ******************************
> procedure Test_MyOperators is
> A,B : Long_Float;
> function "+"(X, Y : Long_Float) return Long_Float
> is
> begin
> return X;
> end;
>
> begin
> A := 1.0;
> B := 2.0;
>
> if A = A+B then
> Put_Line("Yes.");
> else
> Put_Line("No");
> end if;
> end;
> -- -------------------------------------
>
> But since I have many packages, I want to put the redefined operators
> in a separate package, say MY_OPERATORS, and doing instead:
Define a new type and its operations in one package:
package My_Operators is
type My_Real is new Long_Float;
function "+" (Left, Right : My_Real) return My_Real;
end My_Operators;
> -- *************************************
>
> with MY_OPERATORS; use MY_OPERATORS;
>
> procedure Test_MyOperators is
> A,B : Long_Float;
Use My_Real instead.
> begin
> A := 1.0;
> B := 2.0;
>
> if A = A+B then
> Put_Line("Yes.");
> else
> Put_Line("No");
> end if;
> end;
--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: redefinition of the operator
2005-11-20 8:49 redefinition of the operator Francesco Bochicchio
2005-11-20 10:50 ` Dmitry A. Kazakov
@ 2005-11-20 11:12 ` Martin Krischik
2005-11-20 12:23 ` Francesco Bochicchio
` (2 more replies)
1 sibling, 3 replies; 10+ messages in thread
From: Martin Krischik @ 2005-11-20 11:12 UTC (permalink / raw)
Francesco Bochicchio wrote:
> But this does not work.The function Test_MyOperators
> keep using the standard "+" instead of the redefined one
> exported by MY_OPERATORS.
>
> Anybody knows how to do this?
Well, interesting. And No, I don't know the reason - the language lawyers
will tell you soon.
However from a practical point of view: You should not define your own
operators unless you also define your own type as well. So consider :
type My_Float is Long_Float;
function "+"(X, Y : My_Float) return My_Float;
and then a
use type MY_OPERATORS.My_Type;
And writing package name all upper case is out of fashion for at least 10
years. Actually: Writing anything all upper case in Ada is out of fashion
since the introduction of Ada 95. You can do it if you like but it is realy
un-cool ;-) .
Martin
--
mailto://krischik@users.sourceforge.net
Ada programming at: http://ada.krischik.com
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: redefinition of the operator
2005-11-20 10:50 ` Dmitry A. Kazakov
@ 2005-11-20 12:16 ` Francesco Bochicchio
2005-11-20 13:27 ` Dmitry A. Kazakov
` (2 more replies)
0 siblings, 3 replies; 10+ messages in thread
From: Francesco Bochicchio @ 2005-11-20 12:16 UTC (permalink / raw)
Il Sun, 20 Nov 2005 11:50:47 +0100, Dmitry A. Kazakov ha scritto:
> On Sun, 20 Nov 2005 09:49:55 +0100, Francesco Bochicchio wrote:
>
>> I need to redefine the standard mathematic operators, to replace them with
>> my own version.
>>
>> So, if I do something like this, it works:
>>
>> -- ******************************
>> procedure Test_MyOperators is
>> A,B : Long_Float;
>> function "+"(X, Y : Long_Float) return Long_Float
>> is
>> begin
>> return X;
>> end;
>>
>> begin
>> A := 1.0;
>> B := 2.0;
>>
>> if A = A+B then
>> Put_Line("Yes.");
>> else
>> Put_Line("No");
>> end if;
>> end;
>> -- -------------------------------------
>>
>> But since I have many packages, I want to put the redefined operators
>> in a separate package, say MY_OPERATORS, and doing instead:
>
> Define a new type and its operations in one package:
>
> package My_Operators is
> type My_Real is new Long_Float;
> function "+" (Left, Right : My_Real) return My_Real;
> end My_Operators;
>
>> -- *************************************
>>
>> with MY_OPERATORS; use MY_OPERATORS;
>>
>> procedure Test_MyOperators is
>> A,B : Long_Float;
>
> Use My_Real instead.
>
Thanks. I will try.
It is not exactly a cheap solution, since I need to replace the standard
operators in existing software (about 20000 DSI) and this mean to
change long_float with My_Real in most of it, but I guess with some
automated search/replace I could do it.
Ciao
------
FB
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: redefinition of the operator
2005-11-20 11:12 ` Martin Krischik
@ 2005-11-20 12:23 ` Francesco Bochicchio
2005-11-21 14:26 ` Jean-Pierre Rosen
2005-11-21 14:40 ` Robert A Duff
2 siblings, 0 replies; 10+ messages in thread
From: Francesco Bochicchio @ 2005-11-20 12:23 UTC (permalink / raw)
Il Sun, 20 Nov 2005 12:12:00 +0100, Martin Krischik ha scritto:
>
> However from a practical point of view: You should not define your own
> operators unless you also define your own type as well. So consider :
>
> type My_Float is Long_Float;
>
> function "+"(X, Y : My_Float) return My_Float;
>
> and then a
>
> use type MY_OPERATORS.My_Type;
And this is also going to do the trick, according to the other answer I
received (did not try jet).
>
> And writing package name all upper case is out of fashion for at least
> 10 years. Actually: Writing anything all upper case in Ada is out of
> fashion since the introduction of Ada 95. You can do it if you like but
> it is realy un-cool ;-) .
>
I know, but I learned ADA during maintenance of over-ten-years-old code
(predating ADA 95) and I had to keep the existing style... and now my
fingers go search the shift and capslok all by themselves :-(
Ciao
------
FB
> Martin
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: redefinition of the operator
2005-11-20 12:16 ` Francesco Bochicchio
@ 2005-11-20 13:27 ` Dmitry A. Kazakov
2005-11-20 21:29 ` Gautier Write-only
2005-11-21 14:23 ` Jean-Pierre Rosen
2 siblings, 0 replies; 10+ messages in thread
From: Dmitry A. Kazakov @ 2005-11-20 13:27 UTC (permalink / raw)
On Sun, 20 Nov 2005 13:16:20 +0100, Francesco Bochicchio wrote:
> It is not exactly a cheap solution, since I need to replace the standard
> operators in existing software (about 20000 DSI) and this mean to
> change long_float with My_Real in most of it, but I guess with some
> automated search/replace I could do it.
You should do it anyway from software design / maintenance perspective,
because Long_Float implies operations of Long_Float. Replacing operations
means another type and so other type name.
BTW, it is a good idea to never use standard floating point types directly.
But always:
type Defined_By_My_Requirements is digits ... range ...;
or at least:
type Should_Be_OK is new Float;
--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: redefinition of the operator
2005-11-20 12:16 ` Francesco Bochicchio
2005-11-20 13:27 ` Dmitry A. Kazakov
@ 2005-11-20 21:29 ` Gautier Write-only
2005-11-21 14:23 ` Jean-Pierre Rosen
2 siblings, 0 replies; 10+ messages in thread
From: Gautier Write-only @ 2005-11-20 21:29 UTC (permalink / raw)
Francesco Bochicchio:
> It is not exactly a cheap solution, since I need to replace the standard
> operators in existing software (about 20000 DSI) and this mean to
> change long_float with My_Real in most of it, but I guess with some
> automated search/replace I could do it.
While you are at this, I suggest to make a generic package around your My_Real.
No run-time penalty with that approach on good compilers, and so you
can obtain your redifined operators with several precisions without effort.
______________________________________________________________
Gautier -- http://www.mysunrise.ch/users/gdm/index.htm
Ada programming -- http://www.mysunrise.ch/users/gdm/gsoft.htm
TeXCAD -- http://www.mysunrise.ch/users/gdm/texcad.htm
NB: For a direct answer, e-mail address on the Web site!
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: redefinition of the operator
2005-11-20 12:16 ` Francesco Bochicchio
2005-11-20 13:27 ` Dmitry A. Kazakov
2005-11-20 21:29 ` Gautier Write-only
@ 2005-11-21 14:23 ` Jean-Pierre Rosen
2 siblings, 0 replies; 10+ messages in thread
From: Jean-Pierre Rosen @ 2005-11-21 14:23 UTC (permalink / raw)
Francesco Bochicchio a ï¿œcrit :
> Thanks. I will try.
> It is not exactly a cheap solution, since I need to replace the standard
> operators in existing software (about 20000 DSI) and this mean to
> change long_float with My_Real in most of it, but I guess with some
> automated search/replace I could do it.
>
<PLUG>
Have a look at Adasubst, a free tool available from
http://www.adalog.fr/compo2.htm
It can do that (and much more) in seconds.
</PLUG>
--
---------------------------------------------------------
J-P. Rosen (rosen@adalog.fr)
Visit Adalog's web site at http://www.adalog.fr
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: redefinition of the operator
2005-11-20 11:12 ` Martin Krischik
2005-11-20 12:23 ` Francesco Bochicchio
@ 2005-11-21 14:26 ` Jean-Pierre Rosen
2005-11-21 14:40 ` Robert A Duff
2 siblings, 0 replies; 10+ messages in thread
From: Jean-Pierre Rosen @ 2005-11-21 14:26 UTC (permalink / raw)
Martin Krischik a �crit :
> Francesco Bochicchio wrote:
>
>
>>But this does not work.The function Test_MyOperators
>>keep using the standard "+" instead of the redefined one
>>exported by MY_OPERATORS.
>>
>>Anybody knows how to do this?
>
>
> Well, interesting. And No, I don't know the reason - the language lawyers
> will tell you soon.
>
Because the redefined operators are use-visible, while the predefined
ones are directly visible (declared in Standard).
A use clause never hides something that is directly visible.
--
---------------------------------------------------------
J-P. Rosen (rosen@adalog.fr)
Visit Adalog's web site at http://www.adalog.fr
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: redefinition of the operator
2005-11-20 11:12 ` Martin Krischik
2005-11-20 12:23 ` Francesco Bochicchio
2005-11-21 14:26 ` Jean-Pierre Rosen
@ 2005-11-21 14:40 ` Robert A Duff
2 siblings, 0 replies; 10+ messages in thread
From: Robert A Duff @ 2005-11-21 14:40 UTC (permalink / raw)
Martin Krischik <krischik@users.sourceforge.net> writes:
> Francesco Bochicchio wrote:
>
> > But this does not work.The function Test_MyOperators
> > keep using the standard "+" instead of the redefined one
> > exported by MY_OPERATORS.
> >
> > Anybody knows how to do this?
>
> Well, interesting. And No, I don't know the reason - the language lawyers
> will tell you soon.
The language-lawyerly reason is that things in Standard hide things
coming in via use_clauses. That's because you're always inside
Standard, and things visible because you're inside hide things from
use_clauses -- Standard is just one special case of this.
Probably not the best language design -- Hiding Considered Harmful,
IMHO. It causes something similar to a Beaujolais effect.
To avoid the problem, avoid using non-standard types declared in
Standard, such as Float and Long_Float. Yes, I realize the OP has
code that already uses Long_Float, so has to either fix
it or live with it. :-(
Another (somewhat painful) solution would be to make all your
library packages be children of some single root package.
Then put:
type Long_Float is ...
function "+" ...
in that root package. Your_Package.Long_Float will then hide
Standard.Long_Float (and perhaps confuse anybody who doesn't
realize that!).
> However from a practical point of view: You should not define your own
> operators unless you also define your own type as well. So consider :
>
> type My_Float is Long_Float;
>
> function "+"(X, Y : My_Float) return My_Float;
Yes, that's a good idea. Defining the operators with the type ensures
that you don't accidentally use the predefined ones sometimes,
and the user-defined ones other times, depending on whether you
forgot a use [type] in some places.
- Bob
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2005-11-21 14:40 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-11-20 8:49 redefinition of the operator Francesco Bochicchio
2005-11-20 10:50 ` Dmitry A. Kazakov
2005-11-20 12:16 ` Francesco Bochicchio
2005-11-20 13:27 ` Dmitry A. Kazakov
2005-11-20 21:29 ` Gautier Write-only
2005-11-21 14:23 ` Jean-Pierre Rosen
2005-11-20 11:12 ` Martin Krischik
2005-11-20 12:23 ` Francesco Bochicchio
2005-11-21 14:26 ` Jean-Pierre Rosen
2005-11-21 14:40 ` Robert A Duff
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox