comp.lang.ada
 help / color / mirror / Atom feed
* If a routine should return two results, how should it be done?
@ 2014-07-26 18:00 Victor Porton
  2014-07-26 18:53 ` Niklas Holsti
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Victor Porton @ 2014-07-26 18:00 UTC (permalink / raw)


If a routine should return two results, how should it be done?

1. procedure with two out arguments;

2. function with one out argument;

3. create specific record type for the result type of the function?

What are advantages and disadvantages of each variant?

-- 
Victor Porton - http://portonvictor.org

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

* Re: If a routine should return two results, how should it be done?
  2014-07-26 18:00 If a routine should return two results, how should it be done? Victor Porton
@ 2014-07-26 18:53 ` Niklas Holsti
  2014-07-26 18:57   ` Victor Porton
  2014-07-26 20:42 ` Victor Porton
  2014-07-27 11:59 ` anon
  2 siblings, 1 reply; 9+ messages in thread
From: Niklas Holsti @ 2014-07-26 18:53 UTC (permalink / raw)


On 14-07-26 21:00 , Victor Porton wrote:
> If a routine should return two results, how should it be done?

Pick any method you like (but not case 2, if you want to be compatible
with pre-2012 Ada).

> 1. procedure with two out arguments;
> 
> 2. function with one out argument;
> 
> 3. create specific record type for the result type of the function?
> 
> What are advantages and disadvantages of each variant?

The same as in any other language which offers these choices, which is
most languages (well, for C you must use pointers for "out" and "in
out", but logically it is the same thing).

Oh, compared to C++ there is a small difference, in that Ada allows
overloading on the result type; in C++ one would perhaps avoid choices 3
(and 2) for that reason.

Personally I would first consider if the "two results" are logically
connected in such a way that they actually represent one composite
result, and then I would have a subprogram with one result -- no
problem. Otherwise, I would use choice 1. Choice 2 would be acceptable
to me only in very special cases, such as a random-number generator.

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


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

* Re: If a routine should return two results, how should it be done?
  2014-07-26 18:53 ` Niklas Holsti
@ 2014-07-26 18:57   ` Victor Porton
  2014-07-26 19:21     ` Peter Chapin
  2014-07-26 20:10     ` Shark8
  0 siblings, 2 replies; 9+ messages in thread
From: Victor Porton @ 2014-07-26 18:57 UTC (permalink / raw)


Niklas Holsti wrote:

> On 14-07-26 21:00 , Victor Porton wrote:
>> If a routine should return two results, how should it be done?
> 
> Pick any method you like (but not case 2, if you want to be compatible
> with pre-2012 Ada).
> 
>> 1. procedure with two out arguments;
>> 
>> 2. function with one out argument;
>> 
>> 3. create specific record type for the result type of the function?
>> 
>> What are advantages and disadvantages of each variant?
> 
> The same as in any other language which offers these choices, which is
> most languages (well, for C you must use pointers for "out" and "in
> out", but logically it is the same thing).
> 
> Oh, compared to C++ there is a small difference, in that Ada allows
> overloading on the result type; in C++ one would perhaps avoid choices 3
> (and 2) for that reason.
> 
> Personally I would first consider if the "two results" are logically
> connected in such a way that they actually represent one composite
> result, and then I would have a subprogram with one result -- no
> problem. Otherwise, I would use choice 1. Choice 2 would be acceptable
> to me only in very special cases, such as a random-number generator.

I want to create a thick Ada binding for the following C function:

http://librdf.org/raptor/api/raptor2-section-uri.html#raptor-uri-uri-string-to-filename-fragment

char *              raptor_uri_uri_string_to_filename_fragment
                                                        (const unsigned char *uri_string,
                                                         unsigned char **fragment_p);

Which variant is better for this specific case: 1 or 3?

-- 
Victor Porton - http://portonvictor.org

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

* Re: If a routine should return two results, how should it be done?
  2014-07-26 18:57   ` Victor Porton
@ 2014-07-26 19:21     ` Peter Chapin
  2014-07-26 19:37       ` Dan'l Miller
  2014-07-26 20:10     ` Shark8
  1 sibling, 1 reply; 9+ messages in thread
From: Peter Chapin @ 2014-07-26 19:21 UTC (permalink / raw)


On 2014-07-26 14:57, Victor Porton wrote:

> I want to create a thick Ada binding for the following C function:
> 
> http://librdf.org/raptor/api/raptor2-section-uri.html#raptor-uri-uri-string-to-filename-fragment
> 
> char *              raptor_uri_uri_string_to_filename_fragment
>                                                         (const unsigned char *uri_string,
>                                                          unsigned char **fragment_p);
> 
> Which variant is better for this specific case: 1 or 3?

If you're creating a thick binding you don't need to limit yourself to
the same collection of functions provided by the underlying library.
You're in a position to define the types abstractly and write
subprograms that expose the needed functionality without regard to the
specific underlying functions.

I would expect a thick binding to merely use the underlying library to
implement a higher level (cleaner, more abstract) interface. It's not
about providing a binding to each underlying function one-to-one. That's
a thin binding.

What I'm talking about is more work because you'll have to do some
designing. It also comes with a larger documentation effort because
users of your binding won't be able to just read the underlying
library's documentation. But that's what thick bindings are all about.
That's what makes them better. :)

Peter



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

* Re: If a routine should return two results, how should it be done?
  2014-07-26 19:21     ` Peter Chapin
@ 2014-07-26 19:37       ` Dan'l Miller
  0 siblings, 0 replies; 9+ messages in thread
From: Dan'l Miller @ 2014-07-26 19:37 UTC (permalink / raw)


On Saturday, July 26, 2014 2:21:03 PM UTC-5, Peter Chapin wrote:
> On 2014-07-26 14:57, Victor Porton wrote:
> > I want to create a thick Ada binding for the following C function:
> > http://librdf.org/raptor/api/raptor2-section-uri.html#raptor-uri-uri-string-to-filename-fragment
> > char *              raptor_uri_uri_string_to_filename_fragment
> >                                                         (const unsigned char *uri_string,
> >                                                          unsigned char **fragment_p);
> > Which variant is better for this specific case: 1 or 3?
> 
> If you're creating a thick binding you don't need to limit yourself to
> the same collection of functions provided by the underlying library.
> You're in a position to define the types abstractly and write
> subprograms that expose the needed functionality without regard to the
> specific underlying functions.
> 
> I would expect a thick binding to merely use the underlying library to
> implement a higher level (cleaner, more abstract) interface. It's not
> about providing a binding to each underlying function one-to-one. That's
> a thin binding.

Yes, Victor, to be clear:  so far, you are writing a relatively thin binding, judging from the postings here at c.l.a that you have made.  As Dmitry & I have encouraged (and now apparently Peter as well), your thick binding would have a URI record or tagged record.  You would go through the entire underlying C library looking for OOish objects to represent as 1st-class citizens in your thick binding, not buried implicitly, scattered hither & yon in the C way of thinking.  You would decide how many URI syntaxes/domains that you want to support:  e.g., only URLs in WWW browsers?, not general URNs of, say, ISBNs?  If strictly URLs, then perhaps you would forego URI superclass entirely, since URNs for far-afield topics would be uninteresting to you.  If strictly URLs, then which protocols, e.g., all of them {http, https, file, ftp, ...}?  If only a subset, then perhaps the thick binding adds much value for treating URLs for http & https as having many http/https-specific behavior beyond mere string processing.  Likewise for files.  Likewise for ftp or any other protocol.  Perhaps protocol is the OO design to emphasize most and URIs or URLs or URNs are secondary or truly buried as mere parameters here & there.  In other words, take the C library and rethink it as if it did not exist, as if you were writing it from scratch.  What would you like to see the main entities be and how would you like them to behave most sanely?  Then, implement that OO API via the Raptor C library with a little or a lot of glue code between your Ada desired-state API and the (clunky?) Raptor C API's vision or lack thereof.

As Peter said:
...
> That's what makes them better. :)

Btw, URN examples far afield from WWW-browsers' URLs: http://en.wikipedia.org/wiki/Uniform_Resource_Name#Examples


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

* Re: If a routine should return two results, how should it be done?
  2014-07-26 18:57   ` Victor Porton
  2014-07-26 19:21     ` Peter Chapin
@ 2014-07-26 20:10     ` Shark8
  1 sibling, 0 replies; 9+ messages in thread
From: Shark8 @ 2014-07-26 20:10 UTC (permalink / raw)


On 26-Jul-14 12:57, Victor Porton wrote:
> I want to create a thick Ada binding for the following C function:
>
>
> char *              raptor_uri_uri_string_to_filename_fragment
>                                                          (const unsigned char *uri_string,
>                                                           unsigned char **fragment_p);

I think you're going about this backwards -- Try designing the API how 
you want, in "natural Ada" and /then/ use functionality from the linked 
C/C++/other-lang functions in some hidden part (private package, package 
body, etc).

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

* Re: If a routine should return two results, how should it be done?
  2014-07-26 18:00 If a routine should return two results, how should it be done? Victor Porton
  2014-07-26 18:53 ` Niklas Holsti
@ 2014-07-26 20:42 ` Victor Porton
  2014-07-26 20:44   ` Victor Porton
  2014-07-27 11:59 ` anon
  2 siblings, 1 reply; 9+ messages in thread
From: Victor Porton @ 2014-07-26 20:42 UTC (permalink / raw)


Victor Porton wrote:

> If a routine should return two results, how should it be done?
> 
> 1. procedure with two out arguments;
> 
> 2. function with one out argument;
> 
> 3. create specific record type for the result type of the function?
> 
> What are advantages and disadvantages of each variant?

After reading around in Internet, I am declined to the variant 3.

With variant 3 I need to create only one variable (or rather a constant), 
thus making the program a little shorter.

Hm, well, maybe implement BOTH 1 and 3?

-- 
Victor Porton - http://portonvictor.org


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

* Re: If a routine should return two results, how should it be done?
  2014-07-26 20:42 ` Victor Porton
@ 2014-07-26 20:44   ` Victor Porton
  0 siblings, 0 replies; 9+ messages in thread
From: Victor Porton @ 2014-07-26 20:44 UTC (permalink / raw)


Victor Porton wrote:

> Victor Porton wrote:
> 
>> If a routine should return two results, how should it be done?
>> 
>> 1. procedure with two out arguments;
>> 
>> 2. function with one out argument;
>> 
>> 3. create specific record type for the result type of the function?
>> 
>> What are advantages and disadvantages of each variant?
> 
> After reading around in Internet, I am declined to the variant 3.
> 
> With variant 3 I need to create only one variable (or rather a constant),
> thus making the program a little shorter.
> 
> Hm, well, maybe implement BOTH 1 and 3?

This (variant 3) also reduces the likehood to confuse two variables of the 
same type.

And also with 3 I may need a constant instead of two variables, using 
constants is nice.

-- 
Victor Porton - http://portonvictor.org

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

* Re: If a routine should return two results, how should it be done?
  2014-07-26 18:00 If a routine should return two results, how should it be done? Victor Porton
  2014-07-26 18:53 ` Niklas Holsti
  2014-07-26 20:42 ` Victor Porton
@ 2014-07-27 11:59 ` anon
  2 siblings, 0 replies; 9+ messages in thread
From: anon @ 2014-07-27 11:59 UTC (permalink / raw)


You forgot one:
2.A function with one or more "in" argument(s) that are pointers.

1.   Procedure is the normally way to handle "out" arguments in all  
     Ada. And as of Ada 2012 it just one way.

2.   As for the function with or or more "out" arguments. The only 
     advantage is for interfacing to C routines and libraries. 
     Else, it not very useful. One reason is in some cases the 
     compiler can generate more code to handle the new function 
     design then a procedure.

2.A. As for function with one or more "in" pointer type of argument(s),
     it just bad programming and the compiler should check and report 
     is design as an error.

3.   As for result being a record type.  It just a standard result of 
     some function.


In <lr0qb7$549$1@speranza.aioe.org>, Victor Porton <porton@narod.ru> writes:
>If a routine should return two results, how should it be done?
>
>1. procedure with two out arguments;
>
>2. function with one out argument;
>
>3. create specific record type for the result type of the function?
>
>What are advantages and disadvantages of each variant?
>
>-- 
>Victor Porton - http://portonvictor.org

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

end of thread, other threads:[~2014-07-27 11:59 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-07-26 18:00 If a routine should return two results, how should it be done? Victor Porton
2014-07-26 18:53 ` Niklas Holsti
2014-07-26 18:57   ` Victor Porton
2014-07-26 19:21     ` Peter Chapin
2014-07-26 19:37       ` Dan'l Miller
2014-07-26 20:10     ` Shark8
2014-07-26 20:42 ` Victor Porton
2014-07-26 20:44   ` Victor Porton
2014-07-27 11:59 ` anon

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