comp.lang.ada
 help / color / mirror / Atom feed
* Need some help in Ada
@ 2016-03-23 19:29 danifreecs
  2016-03-23 19:51 ` Anh Vo
  2016-03-23 21:18 ` danifreecs
  0 siblings, 2 replies; 9+ messages in thread
From: danifreecs @ 2016-03-23 19:29 UTC (permalink / raw)


Hello there!

I'm a student, and i've got my 1st home project in Ada. I need to implement a J_String package with a lot of functions etc, but i have problems at the start. The specification asks me to "implement J_String type as an opaque discriminant record type. For the inner representation of the string use the Standard String type. The discriminant determines the size of the string, which is contained in the J_String type."

My .ads at the moment:

package J_String_Pkg is
  type J_String(Size: Natural) is private;
  private
    type J_String(Size: Natural) is record
      Components: String(Natural Range 0..Natural'Last);
      end record;
end J_String_Pkg;

I don't know if the above is good, but the main reason i'm asking help is the following methods:
function Create(Str : String) return J_String;
--Creates a J_String from a String
function Value_Of(S : J_String) return String;
--Returns the content of S in String type
function Is_Empty(S : J_String) return Boolean; 
-- Returns true if, and only if, length() is 0.

I think i can do the other 20+ methods, but i can't figure out how to make my J_String type to be able to create empty strings. Thanks for every help!


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

* Re: Need some help in Ada
  2016-03-23 19:29 Need some help in Ada danifreecs
@ 2016-03-23 19:51 ` Anh Vo
  2016-03-23 20:24   ` danifreecs
  2016-03-23 21:18 ` danifreecs
  1 sibling, 1 reply; 9+ messages in thread
From: Anh Vo @ 2016-03-23 19:51 UTC (permalink / raw)


On Wednesday, March 23, 2016 at 12:29:05 PM UTC-7, danif...@gmail.com wrote:
> Hello there!
> 
> I'm a student, and i've got my 1st home project in Ada. I need to implement a J_String package with a lot of functions etc, but i have problems at the start. The specification asks me to "implement J_String type as an opaque discriminant record type. For the inner representation of the string use the Standard String type. The discriminant determines the size of the string, which is contained in the J_String type."
> 
> My .ads at the moment:
> 
> package J_String_Pkg is
>   type J_String(Size: Natural) is private;
>   private
>     type J_String(Size: Natural) is record
>       Components: String(Natural Range 0..Natural'Last);
>       end record;
> end J_String_Pkg;
> 
> I don't know if the above is good, but the main reason i'm asking help is the following methods:
> function Create(Str : String) return J_String;
> --Creates a J_String from a String
> function Value_Of(S : J_String) return String;
> --Returns the content of S in String type
> function Is_Empty(S : J_String) return Boolean; 
> -- Returns true if, and only if, length() is 0.
> 
> I think i can do the other 20+ methods, but i can't figure out how to make my J_String type to be able to create empty strings. Thanks for every help!

Empty string means string length is 0. That means upper string index is less than lower string index. For example, an empty predefined string looks like this.

Empty_String : String (1 .. 0) := "";

By the way, now you can see why the record field Components is not correct. 

Anh Vo


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

* Re: Need some help in Ada
  2016-03-23 19:51 ` Anh Vo
@ 2016-03-23 20:24   ` danifreecs
  2016-03-23 20:52     ` Anh Vo
                       ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: danifreecs @ 2016-03-23 20:24 UTC (permalink / raw)


2016. március 23., szerda 20:51:40 UTC+1 időpontban Anh Vo a következőt írta:
> On Wednesday, March 23, 2016 at 12:29:05 PM UTC-7, danif...@gmail.com wrote:
> > Hello there!
> > 
> > I'm a student, and i've got my 1st home project in Ada. I need to implement a J_String package with a lot of functions etc, but i have problems at the start. The specification asks me to "implement J_String type as an opaque discriminant record type. For the inner representation of the string use the Standard String type. The discriminant determines the size of the string, which is contained in the J_String type."
> > 
> > My .ads at the moment:
> > 
> > package J_String_Pkg is
> >   type J_String(Size: Natural) is private;
> >   private
> >     type J_String(Size: Natural) is record
> >       Components: String(Natural Range 0..Natural'Last);
> >       end record;
> > end J_String_Pkg;
> > 
> > I don't know if the above is good, but the main reason i'm asking help is the following methods:
> > function Create(Str : String) return J_String;
> > --Creates a J_String from a String
> > function Value_Of(S : J_String) return String;
> > --Returns the content of S in String type
> > function Is_Empty(S : J_String) return Boolean; 
> > -- Returns true if, and only if, length() is 0.
> > 
> > I think i can do the other 20+ methods, but i can't figure out how to make my J_String type to be able to create empty strings. Thanks for every help!
> 
> Empty string means string length is 0. That means upper string index is less than lower string index. For example, an empty predefined string looks like this.
> 
> Empty_String : String (1 .. 0) := "";
> 
> By the way, now you can see why the record field Components is not correct. 
> 
> Anh Vo

Thank you for the reply! So it should be Components: String(1..Size)?
then i wrote this:
function Create(Str : String) return J_String is
	Jstr: J_String(Str'Length);
	begin	
		if (Str'Length = 0) then
			Jstr.Components := "";
		else
			Jstr.Components := Str;
		end if;
	return Jstr;
	end Create;
when i try to use it like: jstr1: J_String(4) := Create("doge");
i get this error "expected private type ada.text_io.file_type" which i can't really understand.


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

* Re: Need some help in Ada
  2016-03-23 20:24   ` danifreecs
@ 2016-03-23 20:52     ` Anh Vo
  2016-03-23 21:11       ` danifreecs
  2016-03-23 21:06     ` Robert A Duff
  2016-03-23 21:10     ` Niklas Holsti
  2 siblings, 1 reply; 9+ messages in thread
From: Anh Vo @ 2016-03-23 20:52 UTC (permalink / raw)


On Wednesday, March 23, 2016 at 1:24:05 PM UTC-7, danif...@gmail.com wrote:
> 2016. március 23., szerda 20:51:40 UTC+1 időpontban Anh Vo a következőt írta:
> > On Wednesday, March 23, 2016 at 12:29:05 PM UTC-7, danif...@gmail.com wrote:
> 
> Thank you for the reply! So it should be Components: String(1..Size)?
> then i wrote this:
> function Create(Str : String) return J_String is
> 	Jstr: J_String(Str'Length);
> 	begin	
> 		if (Str'Length = 0) then
> 			Jstr.Components := "";
> 		else
> 			Jstr.Components := Str;
> 		end if;
> 	return Jstr;
> 	end Create;
> when i try to use it like: jstr1: J_String(4) := Create("doge");
> i get this error "expected private type ada.text_io.file_type" which i can't really understand.

There is not enough information to tell. I suggest to post more of your codes.


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

* Re: Need some help in Ada
  2016-03-23 20:24   ` danifreecs
  2016-03-23 20:52     ` Anh Vo
@ 2016-03-23 21:06     ` Robert A Duff
  2016-03-23 21:10     ` Niklas Holsti
  2 siblings, 0 replies; 9+ messages in thread
From: Robert A Duff @ 2016-03-23 21:06 UTC (permalink / raw)


danifreecs@gmail.com writes:

> Thank you for the reply! So it should be Components: String(1..Size)?

Yes.

> then i wrote this:
> function Create(Str : String) return J_String is
> 	Jstr: J_String(Str'Length);
> 	begin	
> 		if (Str'Length = 0) then
> 			Jstr.Components := "";
> 		else
> 			Jstr.Components := Str;
> 		end if;
> 	return Jstr;
> 	end Create;

That looks like it will work, although you should try to format it in a
more standard way.  But it's more complicated than it needs to be.  You
don't need to special case the empty string.  A single return statement,
returning an aggregate, will work:

    return (Size => ..., Components => ...);

I think you can figure out what the "..." parts should be.

> when i try to use it like: jstr1: J_String(4) := Create("doge");
> i get this error "expected private type ada.text_io.file_type" which
> i can't really understand.

That's a visibility problem.  Your Create is not directly visible,
but the Create in Ada.Text_IO is, so the compiler thinks you want
the latter.  You need a "use" clause, or you need to say
J_String_Pkg.Create(...).

By the way, you don't want that "4":

    jstr1: J_String := Create("doge");

or maybe:

    jstr1: constant J_String := Create("doge");

Let the program figure out the length.  You shouldn't have to count the
number of characters in "doge".

- Bob


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

* Re: Need some help in Ada
  2016-03-23 20:24   ` danifreecs
  2016-03-23 20:52     ` Anh Vo
  2016-03-23 21:06     ` Robert A Duff
@ 2016-03-23 21:10     ` Niklas Holsti
  2 siblings, 0 replies; 9+ messages in thread
From: Niklas Holsti @ 2016-03-23 21:10 UTC (permalink / raw)


On 16-03-23 22:24 , danifreecs@gmail.com wrote:
> 2016. március 23., szerda 20:51:40 UTC+1 időpontban Anh Vo a következőt írta:
>> On Wednesday, March 23, 2016 at 12:29:05 PM UTC-7, danif...@gmail.com wrote:
>>> Hello there!
>>>
>>> I'm a student, and i've got my 1st home project in Ada. I need to implement a J_String package with a lot of functions etc, but i have problems at the start. The specification asks me to "implement J_String type as an opaque discriminant record type. For the inner representation of the string use the Standard String type. The discriminant determines the size of the string, which is contained in the J_String type."
>>>
>>> My .ads at the moment:
>>>
>>> package J_String_Pkg is
>>>    type J_String(Size: Natural) is private;
>>>    private
>>>      type J_String(Size: Natural) is record
>>>        Components: String(Natural Range 0..Natural'Last);
>>>        end record;
>>> end J_String_Pkg;
>>>
>>> I don't know if the above is good, but the main reason i'm asking help is the following methods:
>>> function Create(Str : String) return J_String;
>>> --Creates a J_String from a String
>>> function Value_Of(S : J_String) return String;
>>> --Returns the content of S in String type
>>> function Is_Empty(S : J_String) return Boolean;
>>> -- Returns true if, and only if, length() is 0.
>>>
>>> I think i can do the other 20+ methods, but i can't figure out how to make my J_String type to be able to create empty strings. Thanks for every help!
>>
>> Empty string means string length is 0. That means upper string index is less than lower string index. For example, an empty predefined string looks like this.
>>
>> Empty_String : String (1 .. 0) := "";
>>
>> By the way, now you can see why the record field Components is not correct.
>>
>> Anh Vo
>
> Thank you for the reply! So it should be Components: String(1..Size)?
> then i wrote this:
> function Create(Str : String) return J_String is
> 	Jstr: J_String(Str'Length);
> 	begin	
> 		if (Str'Length = 0) then
> 			Jstr.Components := "";
> 		else
> 			Jstr.Components := Str;
> 		end if;
> 	return Jstr;
> 	end Create;
> when i try to use it like: jstr1: J_String(4) := Create("doge");
> i get this error "expected private type ada.text_io.file_type" which i can't really understand.
>

Perhaps -- as a guess -- you have, in your program, a "use Ada.Text_IO", 
but do not have a "use J_String_Pkg". If so, then the subpogram 
Ada.Text_IO.Create is directly visible, but J_String_Pkg.Create is not, 
and perhaps the compiler believes you are calling the subprogram from 
Text_IO, which of course works with File_Type objects, not objects of 
type J_Strings.

Try to write "J_String_Pkg.Create" instead of "Create".

HTH,

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


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

* Re: Need some help in Ada
  2016-03-23 20:52     ` Anh Vo
@ 2016-03-23 21:11       ` danifreecs
  2016-03-23 21:34         ` Anh Vo
  0 siblings, 1 reply; 9+ messages in thread
From: danifreecs @ 2016-03-23 21:11 UTC (permalink / raw)


2016. március 23., szerda 21:52:02 UTC+1 időpontban Anh Vo a következőt írta:
> On Wednesday, March 23, 2016 at 1:24:05 PM UTC-7, danif...@gmail.com wrote:
> > 2016. március 23., szerda 20:51:40 UTC+1 időpontban Anh Vo a következőt írta:
> > > On Wednesday, March 23, 2016 at 12:29:05 PM UTC-7, danif...@gmail.com wrote:
> > 
> > Thank you for the reply! So it should be Components: String(1..Size)?
> > then i wrote this:
> > function Create(Str : String) return J_String is
> > 	Jstr: J_String(Str'Length);
> > 	begin	
> > 		if (Str'Length = 0) then
> > 			Jstr.Components := "";
> > 		else
> > 			Jstr.Components := Str;
> > 		end if;
> > 	return Jstr;
> > 	end Create;
> > when i try to use it like: jstr1: J_String(4) := Create("doge");
> > i get this error "expected private type ada.text_io.file_type" which i can't really understand.
> 
> There is not enough information to tell. I suggest to post more of your codes.

It works in my project now which i'm writing since 2 days, i implemented all the methods my teacher wanted except that Is_Empty(), because first my Size was Positive not a Natural so i couldn't create empty strings, then i didn't know that my discriminant was bad, but now it works. The Error i wrote before was in a test project which i created only to examine the above problems separately, but in my main project it works fine at last. If you're curious i can copy the codes, but everything's okay now, thanks to you! Have a nice evening mate! :)

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

* Re: Need some help in Ada
  2016-03-23 19:29 Need some help in Ada danifreecs
  2016-03-23 19:51 ` Anh Vo
@ 2016-03-23 21:18 ` danifreecs
  1 sibling, 0 replies; 9+ messages in thread
From: danifreecs @ 2016-03-23 21:18 UTC (permalink / raw)


Thanks for the tips guys! I have the with and use J_String_pkg in my code but i forgot to declare it in the .ads! :D

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

* Re: Need some help in Ada
  2016-03-23 21:11       ` danifreecs
@ 2016-03-23 21:34         ` Anh Vo
  0 siblings, 0 replies; 9+ messages in thread
From: Anh Vo @ 2016-03-23 21:34 UTC (permalink / raw)


On Wednesday, March 23, 2016 at 2:11:55 PM UTC-7, danif...@gmail.com wrote:
> 2016. március 23., szerda 21:52:02 UTC+1 időpontban Anh Vo a következőt írta:
> > On Wednesday, March 23, 2016 at 1:24:05 PM UTC-7, danif...@gmail.com wrote:
> > > 2016. március 23., szerda 20:51:40 UTC+1 időpontban Anh Vo a következőt írta:
> > > > On Wednesday, March 23, 2016 at 12:29:05 PM UTC-7, danif...@gmail.com wrote:
> > > 
> > > Thank you for the reply! So it should be Components: String(1..Size)?
> > > then i wrote this:
> > > function Create(Str : String) return J_String is
> > > 	Jstr: J_String(Str'Length);
> > > 	begin	
> > > 		if (Str'Length = 0) then
> > > 			Jstr.Components := "";
> > > 		else
> > > 			Jstr.Components := Str;
> > > 		end if;
> > > 	return Jstr;
> > > 	end Create;
> > > when i try to use it like: jstr1: J_String(4) := Create("doge");
> > > i get this error "expected private type ada.text_io.file_type" which i can't really understand.
> > 
> > There is not enough information to tell. I suggest to post more of your codes.
> 
> It works in my project now which i'm writing since 2 days, i implemented all the methods my teacher wanted except that Is_Empty(), because first my Size was Positive not a Natural so i couldn't create empty strings, then i didn't know that my discriminant was bad, but now it works. The Error i wrote before was in a test project which i created only to examine the above problems separately, but in my main project it works fine at last. If you're curious i can copy the codes, but everything's okay now, thanks to you! Have a nice evening mate! :)

I am glad it worked out for you. Thus, you do not need to post any.


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

end of thread, other threads:[~2016-03-23 21:34 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-03-23 19:29 Need some help in Ada danifreecs
2016-03-23 19:51 ` Anh Vo
2016-03-23 20:24   ` danifreecs
2016-03-23 20:52     ` Anh Vo
2016-03-23 21:11       ` danifreecs
2016-03-23 21:34         ` Anh Vo
2016-03-23 21:06     ` Robert A Duff
2016-03-23 21:10     ` Niklas Holsti
2016-03-23 21:18 ` danifreecs

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