Re: numbers as 'generics' parameters



On Sun, 18 May 2008 13:37:47 -0700 (PDT), Maciej Sobczak wrote:

On 18 Maj, 16:52, "Dmitry A. Kazakov" <mail...@xxxxxxxxxxxxxxxxx>
wrote:

Note also that the constraint to have equal strings is only an example
and probably does not show the full problem.

(The problem; I don't understand, BTW)

I might have written my code like this instead:

template <size_t N>
void foo(const fixed_size_string<N> & s1,
const fixed_size_string<2 * N> & s2);

Easily:

generic
N : Natural;
package Silly_String is
type Pascal_String is new String (1..N);
end Silly_String;

with Silly_String;
generic
with package One_Silly is new Silly_String (<>);
with package Twice_As_Silly is
new Silly_String (One_Silly.Pascal_String'Length * 2);
procedure Foo
( S1 : One_Silly.Pascal_String;
S2 : Twice_As_Silly.Pascal_String
);

If you replaced the type declaration in Strings_Nobody_Ever_Needed to

subtype Pascal_String is String (1..N);

you would start to see what's wrong with all this... (:-))

No, I don't follow. Could you please explain?

This Pascal_String would clash when multiple instantiated. The constraint
in your examples is a part of the type. This is wrong. The constraint of
the subtype is a proper constraint. To violate it is *legal*, so in order
to check it statically you need other (finer) means.

Talking about strings, they are allowed to have statically unknown length

Yes - the string was only a vehicle to show the problem. It might be a
matrix of size N or just about anything else.

Still, for a vast majority of them, it is wrong to have the constraint a
part of the type. This is a fundamental issue, why generics are not a
solution.

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



Relevant Pages

  • Re: can constraint generics using string or not ?
    ... A constraint is just that, it says that T can ONLY derive from the types ... Even if you said just string, it would make no sense because string is ... if you do agree that the example below would apply to struct and string ... public class CWHERE T: ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Whats Wrong With This Alter Table Statement?
    ... DROP {COLUMN field I CONSTRAINT indexname}} ... > String which VBA won't understands the way you wrote it in VBA code. ... > need to write VBA code to "wrap around" the SQL String so that VBA knows ...
    (microsoft.public.access.queries)
  • Re: How to implement nested dictionaries?
    ... string does not have a zero parameter ... dictionaries and I guess string does not match the new constraint! ... public class SerializableDictionary<TKey, TValue> ... XmlSerializer valueSerializer ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: can constraint generics using string or not ?
    ... a non-sealed class or a type parameter ... Why can't I constraint using string but I can constraint like bellow and ... Since this is not a valid use of generics, possibly you are trying to solve a problem for which there is a better way than generics. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Question about default discriminants and mutable objects.
    ... Here the constraint is the array bounds. ... via assignment without leaning on the mechanism of default discriminants. ... Ada is a bit sloppy in the object's subtype ... When you declare S as String you write a type, ...
    (comp.lang.ada)