comp.lang.ada
 help / color / mirror / Atom feed
* unconstrained subtype in component declaration
@ 2014-07-27 15:13 Victor Porton
  2014-07-27 15:21 ` Peter Chapin
  2014-07-27 15:27 ` Victor Porton
  0 siblings, 2 replies; 3+ messages in thread
From: Victor Porton @ 2014-07-27 15:13 UTC (permalink / raw)


As I've told in my previous thread in c.l.a, I want a function to return two 
values.

A fragment of my real code:

   type Filename_And_Fragment is
      record
         Filename: String;
         Fragment: String;
      end record;

   function URI_String_To_Filename_And_Fragment(Str: String) return 
Filename_And_Fragment;

rdf-raptor-uri.ads:63:20: unconstrained subtype in component declaration

The compiler does not accept this. What to do? (I do not want to create a 
full fledged object with access-to-String fields and Get_*() accessors and 
Unchecked_Deallocation in finalization.)

Should I step back to "out" argument of a procedure instead of returning a  
record like this as a return value of a function? (there were reasons why 
this would be better).

Or should I just add two discriminants to the record?

After some practice with Ada, I feel that it is probably not that good as I 
thought earlier.

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


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

* Re: unconstrained subtype in component declaration
  2014-07-27 15:13 unconstrained subtype in component declaration Victor Porton
@ 2014-07-27 15:21 ` Peter Chapin
  2014-07-27 15:27 ` Victor Porton
  1 sibling, 0 replies; 3+ messages in thread
From: Peter Chapin @ 2014-07-27 15:21 UTC (permalink / raw)


On 2014-07-27 11:13, Victor Porton wrote:
> As I've told in my previous thread in c.l.a, I want a function to return two 
> values.
> 
> A fragment of my real code:
> 
>    type Filename_And_Fragment is
>       record
>          Filename: String;
>          Fragment: String;
>       end record;
> 
>    function URI_String_To_Filename_And_Fragment(Str: String) return 
> Filename_And_Fragment;
> 
> rdf-raptor-uri.ads:63:20: unconstrained subtype in component declaration
> 
> The compiler does not accept this. What to do? (I do not want to create a 
> full fledged object with access-to-String fields and Get_*() accessors and 
> Unchecked_Deallocation in finalization.)

What about using type Unbounded_String in package Ada.Strings.Unbounded?

Peter


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

* Re: unconstrained subtype in component declaration
  2014-07-27 15:13 unconstrained subtype in component declaration Victor Porton
  2014-07-27 15:21 ` Peter Chapin
@ 2014-07-27 15:27 ` Victor Porton
  1 sibling, 0 replies; 3+ messages in thread
From: Victor Porton @ 2014-07-27 15:27 UTC (permalink / raw)


Victor Porton wrote:

> As I've told in my previous thread in c.l.a, I want a function to return
> two values.
> 
> A fragment of my real code:
> 
>    type Filename_And_Fragment is
>       record
>          Filename: String;
>          Fragment: String;
>       end record;
> 
>    function URI_String_To_Filename_And_Fragment(Str: String) return
> Filename_And_Fragment;
> 
> rdf-raptor-uri.ads:63:20: unconstrained subtype in component declaration
> 
> The compiler does not accept this. What to do? (I do not want to create a
> full fledged object with access-to-String fields and Get_*() accessors and
> Unchecked_Deallocation in finalization.)
> 
> Should I step back to "out" argument of a procedure instead of returning a
> record like this as a return value of a function? (there were reasons why
> this would be better).
> 
> Or should I just add two discriminants to the record?
> 
> After some practice with Ada, I feel that it is probably not that good as
> I thought earlier.

I've solved this problem. Here is my real code:

   -- In package:
   type Filename_And_Fragment (Filename_Length, Fragment_Length: Natural) is
      record
         Filename: String(1..Filename_Length);
         Fragment: String(1..Fragment_Length);
      end record;

   function URI_String_To_Filename_And_Fragment(Str: String) return Filename_And_Fragment;

   -- In package body:
   function URI_String_To_Filename_And_Fragment(Str: String) return Filename_And_Fragment is
      C_Filename : chars_ptr;
      C_Fragment : aliased chars_ptr;
   begin
      C_Filename := C_Raptor_Uri_Uri_String_To_Filename_Fragment (To_C (Str), C_Fragment'Unchecked_Access);
      declare
         Filename: constant String := Value (C_Filename);
         Fragment: constant String := Value (C_Fragment);
         Result  : constant Filename_And_Fragment :=
            (Filename_Length => Filename'Length, Fragment_Length => Fragment'Length, Filename => Filename, Fragment => Fragment);
      begin
         RDF.Raptor.Memory.raptor_free_memory (C_Filename);
         if C_Fragment /= Null_Ptr then
            RDF.Raptor.Memory.raptor_free_memory (C_Fragment);
         end if;
         return Result;
      end;
   end;

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


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

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

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-07-27 15:13 unconstrained subtype in component declaration Victor Porton
2014-07-27 15:21 ` Peter Chapin
2014-07-27 15:27 ` Victor Porton

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