home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / ada / 2480 < prev    next >
Encoding:
Internet Message Format  |  1992-08-29  |  3.7 KB

  1. Path: sparky!uunet!munnari.oz.au!yoyo.aarnet.edu.au!sirius.ucs.adelaide.edu.au!cs.adelaide.edu.au!andrewd
  2. From: andrewd@cs.adelaide.edu.au (Andrew Dunstan)
  3. Newsgroups: comp.lang.ada
  4. Subject: Re: ADA question
  5. Message-ID: <8363@sirius.ucs.adelaide.edu.au>
  6. Date: 29 Aug 92 00:37:01 GMT
  7. References: <1992Aug27.004228.19551@evb.com> <1992Aug27.102951.8681@news.uni-stuttgart.de>
  8. Sender: news@ucs.adelaide.edu.au
  9. Lines: 74
  10. Nntp-Posting-Host: achilles.cs.adelaide.edu.au
  11.  
  12. In article <1992Aug27.102951.8681@news.uni-stuttgart.de>, ucaa2385@ruscvx.rus.uni-stuttgart.de (Peter Hermann) writes:
  13. |> In article <1992Aug27.004228.19551@evb.com> jjh@evb.com (John Halper) writes:
  14. |> >Daljeet writes:
  15. |> >>I have a function in a package A which returns string. I am using
  16. |> >>that function in another package B and try to give the value returned
  17. |> >>by that function to a variable (which has been declared in package B).
  18. |> >>The variable is of type string (1 .. max) initialised to empty string.
  19. |> >
  20. |> >I don't like making the call twice, and would rather use one of first two 
  21. |> >solutions, but ...
  22. |> 
  23. |> It is advisable to call any function only once
  24. |> for the same purpose, because a function result
  25. |>  1. may change from call to call
  26. |>  2. may be expensive
  27. |>
  28.  
  29. Absolutely! Avoid this like the plague.
  30.  
  31. |> If and only if you have enough space or your strings
  32. |> to be processed are small enough, respectively,
  33. |> then you may code the following:
  34. |> 
  35. |> DECLARE
  36. |>    String_Const : CONSTANT String := My_String_Function;
  37. |>    String_Var : String (String_Const'RANGE) := String_Const ;
  38. |> BEGIN
  39. |>    Process (String_Var) ; -- parameter's mode: IN OUT
  40. |> END ;
  41. |> 
  42. |> An advanced compiler may possibly sweat out the extra space
  43. |> for the constant. Perhaps all compilers of the next generation
  44. |> are apt enough?
  45. |>
  46.  
  47. This is based on my original suggestion, but copies the string into a
  48. variable, which has to be done since "process" is now specified to
  49. take an in out parameter. Unfortunately, this involves the cost of copying
  50. the string, and if the code is called many times this cost can be substantial.
  51. Remember that we really wanted a string variable in the first place, and
  52. only used a constant because it meant we could decalre it without explicit
  53. constraints. To avoid the cost of copying, you could try this, although it
  54. won't necessarily work on all compilers:
  55.  
  56.  DECLARE
  57.     String_Const : CONSTANT String := My_String_Function;
  58.     String_Var : String (String_Const'RANGE) ;
  59.     for string_var use at string_const'address ;
  60.  BEGIN
  61.     Process (String_Var) ; -- parameter's mode: IN OUT
  62.  END ;
  63.  
  64. Now there is no copying, and no extra storage allocation. It's a bit gross,
  65. but it should work.
  66.  
  67. |> After all, I hope that Ada9x relaxes the situation anyway,
  68. |> in that a declaration like
  69. |>    String_Var : String := My_String_Function;
  70. |> will become valid. This would create a constrained array
  71. |> by inspection of the initialization value.
  72.  
  73. It will. Both arrays and discriminated records will be able to be declared
  74. of an unconstrained type and take on constraints from the initialising
  75. expression. In fact, while 9X is far from perfect it promises so many
  76. good things like this that I am getting really impatient to see it.
  77.  
  78. -- 
  79. #######################################################################
  80. #  Andrew Dunstan                   #   There's nothing good or bad   #
  81. #  Department of Computer Science   #   but thinking makes it so.     #
  82. #  University of Adelaide           #                                 #
  83. #  South Australia                  #          - Shakespeare          #
  84. #  net: andrewd@cs.adelaide.edu.au  #                                 #
  85. #######################################################################
  86.