Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8758317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:30:58+00:00 2026-06-13T14:30:58+00:00

I’m trying to overload the equality operator for a generic linked list class. Here’s

  • 0

I’m trying to overload the equality operator for a generic linked list class. Here’s the relevant code:

list.ads:

generic
    type Element_Value_Type is private;

package List is
    type List_Type   is private;
    type Element     is private;
    type Element_Ptr is private;

    function "=" (L, R : List_Type) return Boolean;

    --  Other linked list function declarations  --

private
    type Element is
        record
            Value : Element_Value_Type;
            Next  : Element_Ptr;
            Prev  : Element_Ptr;
        end record;

    type Element_Ptr is access Element;

    type List_Type is
        record
            Length : Integer     := 0;
            Head   : Element_Ptr := null;
            Tail   : Element_Ptr := null;
        end record;
end List;

list.adb:

package body List is
    function "=" (Left, Right : List_Type) return Boolean is
    begin
        --  Code for equality checking  --
    end "=";

    --  Other function implementations  --
end List;

main.adb:

with Text_IO;
with List;
use Ada;

procedure Main is
    package Int_Lists is new List (Integer);

    procedure Print_List (List : Int_Lists.List_Type) is
    begin
        --  code to print the contents of a list  --
    end

    L1, L2 : Int_Lists.List_Type;
begin
    Int_Lists.Append (L1, 1);
    Int_Lists.Append (L2, 1);
    Int_Lists.Append (L1, 2);
    Int_Lists.Append (L2, 2);

    Text_IO.Put_Line (Item => Boolean'Image (L1 = L2));
end Main;

And this is the error that I get in the last line of the body of Main:

operator for private type "List_Type" defined at list.ads:X, instance at line X is not directly visible

Is there any way to get it to see the “=” function? It works if I do Int_Lists."=" (L1, L2), or if I put use Int_Lists before the body of Main, but the first one sort of defeats the purpose of operator overloading and the second one allows unqualified access to all of the List functions from within Main.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T14:31:01+00:00Added an answer on June 13, 2026 at 2:31 pm

    Yes, you can use “=” with a private type generic parameter, however I would advise passing in the “=” function along with the private type but defaulting to the visible one, as the with function … is <> indicates.

    Also, note that when comparing Element you have to compare the value, not the entire record. (See the definition of “=” with Element parameters; it’s in the private section as one of Ada 2012’s expression functions.)

    Test_List.ads

    generic
        type Element_Value_Type is private;
        with function "=" (Left, Right : Element_Value_Type) Return Boolean is <>;
        -- Image only for debugging.
        with function Image( Value : Element_Value_Type ) Return String;
    package Test_List is
        type List_Type   is private;
        type Element     is private;
        type Element_Ptr is private;
    
        function "=" (L, R : List_Type) return Boolean;
        Procedure Append(List : in out List_Type; Item : Element_Value_Type);
        function Image( List : List_Type ) Return String;
    
    private
        type Element is
            record
                Value : Element_Value_Type;
                Next  : Element_Ptr;
                Prev  : Element_Ptr;
            end record;
    
        function "=" (Left, Right : Element ) Return boolean is
        ( Left.Value = Right.Value );
    
    
        type Element_Ptr is access Element;
    
        type List_Type is
            record
                Length : Integer     := 0;
                Head   : Element_Ptr := null;
                Tail   : Element_Ptr := null;
            end record;
    end Test_List;
    

    Test_List.adb

    Package Body Test_List is
    
        function "=" (L, R : List_Type) return Boolean is
        begin
        Return Result : Boolean:= L.Length = R.Length do
            -- We only need to check if lengths are equal.
            if not Result then Return; end if;
    
            declare
            SubType NN_Element_Ptr is Not Null Element_Ptr;
            L_Cursor : NN_Element_Ptr:= L.Head;
            R_Cursor : NN_Element_Ptr:= R.Head;
            begin
            loop
                if L_Cursor.Value /= R_Cursor.Value then
                Result:= False;
                Return;
                end if;
    
                Exit when L_Cursor = L.Tail;
                L_Cursor:= L_Cursor.Next;
                R_Cursor:= R_Cursor.Next;
            end loop;
            end;
        exception
            when Constraint_Error =>
            -- Handle empty lists.
            Result:= L.Tail = R.Tail;
        End return;
        end "=";
    
        Procedure Append(List : in out List_Type; Item : Element_Value_Type) is
        begin
        List.Tail:= new Element'(
                Value => Item,
                Next  => null,
                Prev  => List.Tail
                 );
        -- If this is the inital element we link head to ie, if not we need
        -- to link the previous tail's next-pointer to the current tail.
        if List.Length = 0 then
            List.Head := List.Tail;
        else
            List.Tail.Prev.Next:= List.Tail;
        end if;
        List.Length:= List.Length + 1;
        end Append;
    
        Function Image( List : List_Type ) Return String is
        Separator : Constant String := ", ";
    
        Function Image( Item : Element_Ptr ) Return String is
        begin
            if Item = Null then
            Return "";
            else
            Return Image(Item.Value) & Separator & Image(Item.Next);
            end if;
        end Image;
    
        Temp : String:= Image( List.Head );
        begin
        Return '(' & Temp(temp'First..temp'Last-Separator'Length) & ')';
        end Image;
    
    End Test_List;
    

    test.adb

    With
    Test_List,
    Ada.Text_IO;
    
    Procedure Test is
        Package J is new Test_List( Integer, Image => Integer'Image );
        L1, L2 : J.List_Type;
        Use J;
    
    Begin
        Ada.Text_IO.Put_Line( Image(L1) );
        J.Append (L1, 1);
        J.Append (L2, 1);
        J.Append (L1, 2);
        J.Append (L2, 2);
        J.Append (L1, 3);
        J.Append (L2, 3);
        Ada.Text_IO.Put_Line( Image(L1) );
        Ada.Text_IO.Put_Line( "List equality: " & Boolean'(L1 = L2)'Img );
        J.Append (L2, 1);
        J.Append (L1, 2);    
        Ada.Text_IO.Put_Line( "List equality: " & Boolean'(L1 = L2)'Img );
    End Test;
    

    Output is:

    ()
    ( 1,  2,  3)
    List equality: TRUE
    List equality: FALSE
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.