* Integer-Types @ 2004-10-04 23:54 Rick Santa-Cruz 2004-10-05 0:48 ` Integer-Types Jeffrey Carter 2004-10-05 1:23 ` Integer-Types Stephen Leake 0 siblings, 2 replies; 6+ messages in thread From: Rick Santa-Cruz @ 2004-10-04 23:54 UTC (permalink / raw) Hi, the more I consider the details and play with the language, the more questions come in my mind... sorry... Is the following the same: type New_Int is range 1..1000000; type New_Int1 is new Integer range 1..1000000; ? Thanks a lot, Rick ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Integer-Types 2004-10-04 23:54 Integer-Types Rick Santa-Cruz @ 2004-10-05 0:48 ` Jeffrey Carter 2004-10-05 1:12 ` Integer-Types Rick Santa-Cruz 2004-10-05 1:23 ` Integer-Types Stephen Leake 1 sibling, 1 reply; 6+ messages in thread From: Jeffrey Carter @ 2004-10-05 0:48 UTC (permalink / raw) Rick Santa-Cruz wrote: > Is the following the same: > type New_Int is range 1..1000000; > type New_Int1 is new Integer range 1..1000000; No. In the first place, 2 type declarations always create 2 distinct types, even if everything is identical except the type name. New_Int is an integer type with a range of 1 .. 1_000_000. New_Int'Base will be chosen by the compiler. New_Int1 is an integer type with a range of 1 .. 1_000_000. New_Int1'Base has the same representation as Integer. Thus New_Int1 is similar to subtype New_Int1 is Integer range 1 .. 1_000_000; except that type New_Int1 cannot be mixed with Integer, and subtype New_Int1 can. If you have a compiler for a 64-bit processor, Integer is probably a 64-bit type, and New_Int1 will be, too. New_Int'Base will probably be a 32-bit type. Note how much easier it is to read one million when it's written with underscores. -- Jeff Carter "You tiny-brained wipers of other people's bottoms!" Monty Python & the Holy Grail 18 ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Integer-Types 2004-10-05 0:48 ` Integer-Types Jeffrey Carter @ 2004-10-05 1:12 ` Rick Santa-Cruz 2004-10-05 19:37 ` Integer-Types Jeffrey Carter 0 siblings, 1 reply; 6+ messages in thread From: Rick Santa-Cruz @ 2004-10-05 1:12 UTC (permalink / raw) Hi, thanks for the fast answer! > > New_Int is an integer type with a range of 1 .. 1_000_000. New_Int'Base > will be chosen by the compiler. > > New_Int1 is an integer type with a range of 1 .. 1_000_000. New_Int1'Base > has the same representation as Integer. Thus New_Int1 is similar to So this means the main difference is, that without the "new" the compiler will choose the Base-Type (for example a short-int) and in the second case I explicitly decide which is the base-type. Is this correct? Is there somewhere a summing-up in which contexts I can use the keyword "new"? I know some: 1.) For declaring a new type 2.) For deriving from tagged-types 3.) For using with pointers... that means for creating a pointer-object. 4.) In the context of the instantiation of generics Are there more? Bye, Rick ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Integer-Types 2004-10-05 1:12 ` Integer-Types Rick Santa-Cruz @ 2004-10-05 19:37 ` Jeffrey Carter 0 siblings, 0 replies; 6+ messages in thread From: Jeffrey Carter @ 2004-10-05 19:37 UTC (permalink / raw) Rick Santa-Cruz wrote: > So this means the main difference is, that without the "new" the > compiler will choose the Base-Type (for example a short-int) and in > the second case I explicitly decide which is the base-type. Is this > correct? That's right. You can also leave off the range information, in which case the new type is just like the parent type, but is a different type with a different name. New, in a type declaration, declares a derived type. A derived type has the same representation as the parent (unless that is changed with representation clauses) and inherits all the primitive operations of the parent. Conversions between the two types are also defined. Sometimes this is useful. Suppose you have a memory location that maps to a hardware output register. The register can have only a few valid values. Then you can write: type Sensor_State is (One, Two, Three, Four); Suppose the 4 values are represented by 1, 2, 4, 8, respectively. You can hide this external representation from your application using a derived type. Define an operation to read the sensor and return a Sensor_State. The rest of the application doesn't need to know that the register uses a different encoding: function Get return Sensor_State is type Hardware_Sensor_State is new Sensor_State; for Hardware_Sensor_State'Size use Hardware_Register_Size; for Hardware_Sensor_State use (One => 2#0001#, Two => 2#0010#, Three => 2#0100#, Four => 2#1000#); Register : Hardware_Sensor_State; for Register'Address use Hardware_Sensor_Address; pragma Import (Ada, Register); pragma Volatile (Register); begin -- Get return Sensor_State (Register); end Get; This is called a change of representation. > Is there somewhere a summing-up in which contexts I can use the > keyword "new"? Not that I know of. ARM Annex P contains a syntax summary. > I know some: > 1.) For declaring a new type For declaring a derived type. > 2.) For deriving from tagged-types This is also a derived type. It's called a type extension, because the new type extends the parent. > 3.) For using with pointers... that means for creating a > pointer-object. This is an allocator. It doesn't create a pointer object. It allocates an object of the designated type and returns an access value designating that object. > 4.) In the context of the instantiation of generics Annex P shows derived type definition (including formal derived type definitions) allocator private extension definition generic instantiation (including formal package definitions) -- Jeff Carter "We burst our pimples at you." Monty Python & the Holy Grail 16 ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Integer-Types 2004-10-04 23:54 Integer-Types Rick Santa-Cruz 2004-10-05 0:48 ` Integer-Types Jeffrey Carter @ 2004-10-05 1:23 ` Stephen Leake 2004-10-05 9:58 ` Integer-Types Rick Santa-Cruz 1 sibling, 1 reply; 6+ messages in thread From: Stephen Leake @ 2004-10-05 1:23 UTC (permalink / raw) To: comp.lang.ada "Rick Santa-Cruz" <rick_santa_cruz75@msn.com> writes: > Hi, > > the more I consider the details and play with the language, the more > questions come in my mind... sorry... > > Is the following the same: > type New_Int is range 1..1000000; > type New_Int1 is new Integer range 1..1000000; These are very similar. They will be different if Integer'last < New_Int'last; in that case, the compiler will report a compile time error. They will have different base types if the base of Integer is 64 bits, since New_Int will fit in 32 bits. The only reason to derive an integer type from another integer type is to ensure they have the same underlying representation. -- -- Stephe ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Integer-Types 2004-10-05 1:23 ` Integer-Types Stephen Leake @ 2004-10-05 9:58 ` Rick Santa-Cruz 0 siblings, 0 replies; 6+ messages in thread From: Rick Santa-Cruz @ 2004-10-05 9:58 UTC (permalink / raw) Hi, > They will have different base types if the base of Integer is 64 bits, > since New_Int will fit in 32 bits. > > The only reason to derive an integer type from another integer type is > to ensure they have the same underlying representation. And will it give me any other advantages in using varaibles of the New_Int1 beside the fact that I can choose the correct base manually? Thanks in advance, Rick ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2004-10-05 19:37 UTC | newest] Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2004-10-04 23:54 Integer-Types Rick Santa-Cruz 2004-10-05 0:48 ` Integer-Types Jeffrey Carter 2004-10-05 1:12 ` Integer-Types Rick Santa-Cruz 2004-10-05 19:37 ` Integer-Types Jeffrey Carter 2004-10-05 1:23 ` Integer-Types Stephen Leake 2004-10-05 9:58 ` Integer-Types Rick Santa-Cruz
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox