comp.lang.ada
 help / color / mirror / Atom feed
* Ada.Strings.Unbounded vs Ada.Containers.Indefinite_Holders
@ 2017-09-21 18:14 Victor Porton
  2017-09-21 21:30 ` AdaMagica
  0 siblings, 1 reply; 8+ messages in thread
From: Victor Porton @ 2017-09-21 18:14 UTC (permalink / raw)


I am writing an interface to a C library. Some C functions accept either a C 
string pointer or NULL.

I need some way to define an Ada object which could hold values equivalent 
to C strings with possible NULL.

I consider two approaches:

1. Ada.Strings.Unbounded (remind that it can be Null_Unbounded_String which 
is equivalent to NULL C string).

2. Ada.Containers.Indefinite_Holders holding a String.

The first way seems more natural, as a more specialized package is used.

The second way may have the advantage that there is Is_Empty function while 
for unbounded strings I would need to compare with Null_Unbounded_String 
using "=" operator. This may be a little slower. Or does it matter with 
modern compilers?

Hm well, do I understand Null_Unbounded_String correctly? Is 
Null_Unbounded_String really "no string"? or is it just empty string ("")? 
RM2012 should be made more clear on this topic!

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


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

* Re: Ada.Strings.Unbounded vs Ada.Containers.Indefinite_Holders
  2017-09-21 18:14 Ada.Strings.Unbounded vs Ada.Containers.Indefinite_Holders Victor Porton
@ 2017-09-21 21:30 ` AdaMagica
  2017-09-22 12:16   ` Victor Porton
  0 siblings, 1 reply; 8+ messages in thread
From: AdaMagica @ 2017-09-21 21:30 UTC (permalink / raw)


Am Donnerstag, 21. September 2017 20:14:29 UTC+2 schrieb Victor Porton:
> This may be a little slower.

Why do you care for performance? Have you ever measured what you are worrying about?

Premature optimization is the source of all evil - you can hear this mantra very often here.


> Hm well, do I understand Null_Unbounded_String correctly? Is 
> Null_Unbounded_String really "no string"? or is it just empty string ("")? 

It's the latter. Is RM A.4.5(73) and 2.5(6) clear enough?

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

* Re: Ada.Strings.Unbounded vs Ada.Containers.Indefinite_Holders
  2017-09-21 21:30 ` AdaMagica
@ 2017-09-22 12:16   ` Victor Porton
  2017-09-22 19:25     ` Simon Wright
  0 siblings, 1 reply; 8+ messages in thread
From: Victor Porton @ 2017-09-22 12:16 UTC (permalink / raw)


AdaMagica wrote:

> Am Donnerstag, 21. September 2017 20:14:29 UTC+2 schrieb Victor Porton:
>> This may be a little slower.
> 
> Why do you care for performance? Have you ever measured what you are
> worrying about?
> 
> Premature optimization is the source of all evil - you can hear this
> mantra very often here.
> 
> 
>> Hm well, do I understand Null_Unbounded_String correctly? Is
>> Null_Unbounded_String really "no string"? or is it just empty string
>> ("")?
> 
> It's the latter. Is RM A.4.5(73) and 2.5(6) clear enough?

The problem I asked about is exactly that A.4.5(73) is not clear enough. 
What is 2.5(6)? There is no (6) in RM 2.5.

Well, you are right (assuming that GCC 7.2.0 does the right thing), as this 
prints "True":

with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Text_IO;

procedure Test is
begin
  Ada.Text_IO.Put_Line(Boolean'Image(To_Unbounded_String("") = 
Null_Unbounded_String));
end;


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

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

* Re: Ada.Strings.Unbounded vs Ada.Containers.Indefinite_Holders
  2017-09-22 12:16   ` Victor Porton
@ 2017-09-22 19:25     ` Simon Wright
  2017-09-22 22:15       ` Victor Porton
  0 siblings, 1 reply; 8+ messages in thread
From: Simon Wright @ 2017-09-22 19:25 UTC (permalink / raw)


Victor Porton <porton@narod.ru> writes:

> What is 2.5(6)? There is no (6) in RM 2.5.

2.6(6):

    A null string literal is a string_literal with no string_elements
    between the quotation marks.

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

* Re: Ada.Strings.Unbounded vs Ada.Containers.Indefinite_Holders
  2017-09-22 19:25     ` Simon Wright
@ 2017-09-22 22:15       ` Victor Porton
  2017-09-23  8:09         ` Dmitry A. Kazakov
  0 siblings, 1 reply; 8+ messages in thread
From: Victor Porton @ 2017-09-22 22:15 UTC (permalink / raw)


Simon Wright wrote:

> Victor Porton <porton@narod.ru> writes:
> 
>> What is 2.5(6)? There is no (6) in RM 2.5.
> 
> 2.6(6):
> 
>     A null string literal is a string_literal with no string_elements
>     between the quotation marks.

In my opinion, it would be better to change RM phrasing from "null string" 
to "empty string", because in some other languages (notably C) NULL means 
something other. It is just confusing.

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

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

* Re: Ada.Strings.Unbounded vs Ada.Containers.Indefinite_Holders
  2017-09-22 22:15       ` Victor Porton
@ 2017-09-23  8:09         ` Dmitry A. Kazakov
  2017-09-23  9:16           ` Jeffrey R. Carter
  0 siblings, 1 reply; 8+ messages in thread
From: Dmitry A. Kazakov @ 2017-09-23  8:09 UTC (permalink / raw)


On 2017-09-23 00:15, Victor Porton wrote:
> Simon Wright wrote:
> 
>> Victor Porton <porton@narod.ru> writes:
>>
>>> What is 2.5(6)? There is no (6) in RM 2.5.
>>
>> 2.6(6):
>>
>>      A null string literal is a string_literal with no string_elements
>>      between the quotation marks.
> 
> In my opinion, it would be better to change RM phrasing from "null string"
> to "empty string", because in some other languages (notably C) NULL means
> something other. It is just confusing.

The adjective null and the noun null are distinct parts of speech. C's 
noun null is an abbreviation of null pointer. If pointers can be null so 
strings can.

Wouldn't it be silly to change, say, mathematical group theory with its 
null group elements because C (and Ada too) have null pointers called nulls?

BTW, empty is string is indeed a null element for the concatenation 
operation &:

    forall X in String   X & "" = X

So, using the null adjective is absolutely justified.

A null string is a bit more than just empty string. (:-))

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

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

* Re: Ada.Strings.Unbounded vs Ada.Containers.Indefinite_Holders
  2017-09-23  8:09         ` Dmitry A. Kazakov
@ 2017-09-23  9:16           ` Jeffrey R. Carter
  2017-10-03  0:15             ` Randy Brukardt
  0 siblings, 1 reply; 8+ messages in thread
From: Jeffrey R. Carter @ 2017-09-23  9:16 UTC (permalink / raw)


On 09/23/2017 10:09 AM, Dmitry A. Kazakov wrote:
> On 2017-09-23 00:15, Victor Porton wrote:
>>
>> In my opinion, it would be better to change RM phrasing from "null string"
>> to "empty string", because in some other languages (notably C) NULL means
>> something other. It is just confusing.
> 
> The adjective null and the noun null are distinct parts of speech. C's noun null 
> is an abbreviation of null pointer. If pointers can be null so strings can.

Another way to look at it: Ada has the formal concepts of:

* null access value ARM 4.2(9)
* null array 3.6.1(7)
* null constraint 3.2(7/2)
* null_exclusion 3.10(5.1/2)
* null extension 3.9.1(4.1/2)
* null procedure 6.7(3/3)
* null range 3.5(4)
* null record 3.8(15)
* null slice 4.1.2(7)
* null string literal 2.6(6)
* null value (of an access type) 3.10(13/2)
* null_statement 5.1(6)

not to mention the language-defined identifiers

Null_Address
    in System   13.7(12)
Null_Bounded_String
    in Ada.Strings.Bounded   A.4.4(7)
Null_Id
    in Ada.Exceptions   11.4.1(2/2)
Null_Occurrence
    in Ada.Exceptions   11.4.1(3/2)
Null_Ptr
    in Interfaces.C.Strings   B.3.1(7)
Null_Set
    in Ada.Strings.Maps   A.4.2(5)
    in Ada.Strings.Wide_Maps   A.4.7(5)
    in Ada.Strings.Wide_Wide_Maps   A.4.8(5/2)
Null_Task_Id
    in Ada.Task_Identification   C.7.1(2/2)
Null_Unbounded_String
    in Ada.Strings.Unbounded   A.4.5(5)

(Just look under N in the index.)

It's called overloading. Many of these cases refer to things that can have 
components and mean one with zero components: a null record has no components, a 
null array has no components ('Length = 0), a null string literal has no 
characters, a null set has no members, ... It should not be confusing.

-- 
Jeff Carter
"You cheesy lot of second-hand electric donkey-bottom biters."
Monty Python & the Holy Grail
14

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

* Re: Ada.Strings.Unbounded vs Ada.Containers.Indefinite_Holders
  2017-09-23  9:16           ` Jeffrey R. Carter
@ 2017-10-03  0:15             ` Randy Brukardt
  0 siblings, 0 replies; 8+ messages in thread
From: Randy Brukardt @ 2017-10-03  0:15 UTC (permalink / raw)


"Jeffrey R. Carter" <spam.jrcarter.not@spam.not.acm.org> wrote in message 
news:oq58qb$o3s$1@dont-email.me...
> On 09/23/2017 10:09 AM, Dmitry A. Kazakov wrote:
>> On 2017-09-23 00:15, Victor Porton wrote:
>>>
>>> In my opinion, it would be better to change RM phrasing from "null 
>>> string"
>>> to "empty string", because in some other languages (notably C) NULL 
>>> means
>>> something other. It is just confusing.
....
> It's called overloading. Many of these cases refer to things that can have 
> components and mean one with zero components: a null record has no 
> components, a null array has no components ('Length = 0), a null string 
> literal has no characters, a null set has no members, ... It should not be 
> confusing.

And it also shows the amount of change needed to actually make such a change 
to the RM. It would be a lot of change for (at most) not much value, so it 
wouldn't be a very good use of scarce resources.

                              Randy.


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

end of thread, other threads:[~2017-10-03  0:15 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-09-21 18:14 Ada.Strings.Unbounded vs Ada.Containers.Indefinite_Holders Victor Porton
2017-09-21 21:30 ` AdaMagica
2017-09-22 12:16   ` Victor Porton
2017-09-22 19:25     ` Simon Wright
2017-09-22 22:15       ` Victor Porton
2017-09-23  8:09         ` Dmitry A. Kazakov
2017-09-23  9:16           ` Jeffrey R. Carter
2017-10-03  0:15             ` Randy Brukardt

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