1

Topic: typedef

As to the person who has been brought up on With ++, me very much does not suffice in Java  typedef. Whether there are any normal methods to receive similar functionality in Java? I want to receive semantic integrity of the data types. At me in the project the set of the data is transferred as String or int. But as a matter of fact there absolutely different data types are transferred. For example, at objects is Id, InstanceId, ResourceId and so forth, transferred as String and by mistake developers can sometimes take Id and transfer it there where it is expected ResourceId. Syntactically problems are not present - both there and there a line, but logically it is an error. And to trace this error occasionally difficult. I would like to receive any  from the compiler in such places. In With ++ I would make it through typedef, but in Java such feature is not present. Those  for types which can be declared through using, it at all that, it works only within the limits of one file. Other decision occurring - to be inherited from standard types, something like it: public class InstanceIdType extends String But it is impossible so to make, for class String is declared as "final", closed for inheritance. While on mind creation of the classes-wrappers having an internal field of type String or int comes, but it leads to creation of a heap of additional small objects in . It would not be desirable to sacrifice productivity and storage. Can eat any other methods? Any magic summaries prompting when String it is possible to transfer in a method expecting String and when it is impossible? The Development environment - Intellij IDEA, but in an ideal the general-purpose decision that everywhere worked since some developers use Eclipse would be necessary.

2

Re: typedef

Hello, Artem Korneev, you wrote: AK> While on mind creation of the classes-wrappers having an internal field of type String or int comes, but it leads to creation of a heap of additional small objects in . It would not be desirable to sacrifice productivity and storage. I use wrappers many years, in all projects had brakes normally from  interactions on a network (a DB, JMS, web services, http, etc.), but it is exact not from small objects

3

Re: typedef

AK> While on mind creation of the classes-wrappers having an internal field of type String or int comes, but it leads to creation of a heap of additional small objects in . It would not be desirable to sacrifice productivity and storage. typedef is not present, but here in  it is easy to overcome a heap of small objects in many cases-> immutable + cache still if time is, it is possible  the plug-in to the compiler which or checks simply up your summaries (easily) or tears a wrapper and replaces with an internal field (difficult, it is possible  experience of Lombok)

4

Re: typedef

B> also replaces with an internal field (difficult, it it is possible  experience of Lombok) Though Lombok is a preprocessor, its experience here hardly helps.

5

Re: typedef

B> still if time is, it is possible  the plug-in to the compiler which or checks simply up your summaries Advised, and then itself thought - really to anybody to a head did not come?  came and implemented already - https://checkerframework.org/manual/#units-checker

6

Re: typedef

Hello, bzig, you wrote: B>> still if time is, it is possible  the plug-in to the compiler which or checks simply up your summaries B> Advised, and then itself thought - really to anybody to a head did not come?  came and implemented already - https://checkerframework.org/manual/#units-checker And here an example for values String "different type" https://checkerframework.org/manual/#subtyping-checker

7

Re: typedef

Hello, Artem Korneev, you wrote: AK> Can eat any other methods? Any magic summaries prompting when String it is possible to transfer in a method expecting String and when it is impossible? The Development environment - Intellij IDEA, but in an ideal the general-purpose decision that everywhere worked since some developers use Eclipse would be necessary. About high-speed performance it is possible especially not , JVM GC it is ground just under the fast assembly of a considerable quantity of short-lived objects. - quite working variant, besides it it is necessary to look at wrappers on: - projectlombok - java compile-time annotation processor (javax.annotation.processing): http://hannesdorfmann.com/annotation-pr … cessing101 - language Scala ^ _ ^

8

Re: typedef

Hello, Artem Korneev, you wrote: AK> While on mind creation of the classes-wrappers having an internal field of type String or int comes, it seems To me, in such cases of attempt to overcome language and to do something strange (from the point of view of language), to add which feature there is not present, make only worse. Normally all live without it and are not soared, only  surprise at those who writes for a long time adding of such wrappers on java. If checks that are absolutely necessary, Preconditions with the designer and in setter help a little, but it is finite runtime. With  and similar  I would not advise to communicate - favor disputable, but a lot of attention demands. AK> but it leads to creation of a heap of additional small objects in . It would not be desirable to sacrifice productivity and storage. Yes it is fine, it java. Here so it is accepted. About storage  only if it really problem i.e. when it really does not suffice, but not before. AK> Can eat any other methods? Any magic summaries prompting when String it is possible to transfer in a method expecting String and when it is impossible? The Development environment - Intellij IDEA, but in an ideal the general-purpose decision that everywhere worked since some developers use Eclipse would be necessary. No. Because in language there is no such problem.

9

Re: typedef

scf> - language Scala ^ _ ^ the Strange council to the person who worries about one additional level of object hierarchy.

10

Re: typedef

Hello, Artem Korneev, you wrote: whether AK> There are any normal methods to receive similar functionality in Java? I want to receive semantic integrity of the data types. At me in the project the set of the data is transferred as String or int. But as a matter of fact there absolutely different data types are transferred. For example, at objects is Id, InstanceId, ResourceId and so forth, transferred as String and by mistake developers can sometimes take Id and transfer it there where it is expected ResourceId. It is pleasant to see the person who understands this problem. I in C# solved it through heap introduction struct which are different incompatible types, but at the same time are types-values, and public struct UserId {public int Id;} will occupy the same of 4 bytes, as normal int. For  the normal decision is not present, except creation of new classes. Well or it is valid - to pass on Scala.

11

Re: typedef

12

Re: typedef

13

Re: typedef

S> I Think that speech was about : Hardly it is applicable to about what speaks a topic a starter - at it objects are transferred there-here and he worries that on the way types mix up, explicitly speech does not go about local objects.

14

Re: typedef

Hello, Artem Korneev, you wrote: AK> In With ++ I would make it through typedef, but in Java such feature is not present. Those  for types which can be declared through using, it at all that, it works only within the limits of one file. Other decision occurring - to be inherited from standard types, something like it: Well in Si, generally, typedef generates a synonym for type, completely with it interchangeable. I.e., if to produce from lines logically different two typedef', the compiler will not mourn if to confuse them among themselves. I.e., it does not give any protection, only decorates the code.

15

Re: typedef

16

Re: typedef

17

Re: typedef

18

Re: typedef

19

Re: typedef

20

Re: typedef

21

Re: typedef

Hello, Artem Korneev, you wrote: AK> As to the person who has been brought up on With ++, me very much does not suffice in Java  typedef. Whether AK> There are any normal methods to receive similar functionality in Java? I want to receive semantic integrity of the data types. At me in the project the set of the data is transferred as String or int. But as a matter of fact there absolutely different data types are transferred. If to speak about productivity invariable wrappers are quite effectively optimized, up to layout on a stack. And if a little to wait, soon appear value types and it will be absolutely good. But wrappers - the right answer is even not so much from the point of view of productivity, how many from the point of view of OOP. Type it is not simple any data structure, it all the same also a dial-up of operations over the data, so a class instead of typedef - more pertinent decision.

22

Re: typedef

Hello, StanislavK, you wrote: AK>> Can eat any other methods? Any magic summaries prompting when String it is possible to transfer in a method expecting String and when it is impossible? The Development environment - Intellij IDEA, but in an ideal the general-purpose decision that everywhere worked since some developers use Eclipse would be necessary. SK> Is not present. Because in language there is no such problem. In language? Problems are at programmers. And problems are everywhere and always - there was no still the normal reliable platform guaranteeing absence of bugs. Each type of bugs has reasons, there are the approaches, allowing to lower risk of appearance of a bug. I here will not remember that I ever alive saw "a classical" bug because of which it is recommended to mold curly brackets in each unit if/else/for/... etc: if (something) action1 (); action2 (); action3 (); But I agree that it is better to mold everywhere brackets, than to have let even small probability to receive such here the code. The majority of real bugs with which I had to work, were much more , but thus I see the typical patterns conducting to these bugs. Here one of these  patterns - usage of the same simple data types for set of different logic data types. In the code recently came across the such: interfaceId = node.getInstanceId (); And should be: nterfaceId = node.getInterfaceId (); Autocompletion offered the first variant, I  did not make out a dirty trick and took that offer. Then caught , but all the same. And the colleague sitting in the neighbourhood, chased day "bug" - the system on Azure behaved not how system on AWS. After some  in the code it was clarified that the code of support AWS transverses in a cycle on network interfaces of system, but copies tags not from interfaces, and from the virtual machine to which these interfaces belong. There just there was something such (strongly simplified code): for (NetworkInterface interface: instance.getInterfaces ()) {cachedInterfaces.add (new NetworkInterface (instance.getTags ());} I.e. besides, instance it was thrust instead of interface and tags copied not therefrom (both types of these objects have tags and everywhere, certainly, String, in both types of tags). We now leaking  finished a branch, we give to testers. There is time for improvings, will try different variants which here advised to me. Variants as I see, two - summaries and types-wrappers. Summaries look bulky enough decision is it is necessary in thousand places of summaries  that it could check them. Types-wrappers, I am afraid, can meet objections of colleagues. Would be where easier if it would be supported typedef. The data at us actually not too it is a lot of - well thousand or ten thousand objects. Even if to double storage spent for them anybody and does not note - well the system on half-mbyte gobbles up more, the problem is not great. It will be necessary to try to enter type-wrapper for one type and to show to colleagues result.

23

Re: typedef

scf>> - language Scala ^ _ ^ B> the Strange council to the person who worries about one additional level of object hierarchy. Well then Kotlin ^ _ ^ There just ` typealias ` corresponding with ++. Truth a little strange Artem Korneev writes that in pluses ` typedef ` helped it  type, since that with pluses that in  it not new type, and only

24

Re: typedef

Hello, std.denis, you wrote: SD> Truth a little strange Artem Korneev writes that with pluses ` typedef ` helped it  type since that in pluses that in  it not new type, and only  When I with pluses the last time worked, I used the compiler clang and any additional static analyzers. Can therefrom  was. Well or I already survived from mind and something I confuse - I on pluses the last time wrote about 6 years ago. But me it was for some reason remembered that in pluses so to check a logical coordination of types it is possible. And I can is stupid from a line std:: string was inherited to receive new logical type. Already I will not recall.