Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

flutter - Dart datatype naming convention.. Why String instead of string

In dart/Flutter all basic data types name are defined in small case

  • int
  • bool
  • double etc..

Then why string datatype is became String instead of string ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

From the github issue mentioned by @Ahmed I. Elsayed, the following is relevant answer provided by @LRN Reference

The real question, if you are going for consistency, is why int is not Int. Dart generally capitalize types, so the exception here is not String, it is int, double and bool (and void, but that wasn't originally a real type).

So if you want consistency, we should make int be Int. Or maybe it should even be Integer, because we also discourage abbreviations.

In Java, int is lower case and Integer is capitalized because the former is a primitive type and the latter is an object type. Dart does not have that distinction, our int is an object type, so we don't actually have any consistency-based reason for making int short and lower-case.

Or maybe there is one reason: int, double and bool instances are automatically canonicalized. You cannot have two int instances with the same value without them being identical. That's the one property that Dart has taken from Java/C#/JavaScript primitive types, and it does not apply to strings (like it doesn't in Java and C# either).

The real reason Dart has those exceptions (int, double and bool) is because of trade-offs between usability, user expectation and consistency. Dart was designed as a pragmatic language. It values consistency, but not at any price. The familiarity/user-expectation goal was generally influenced by Java, JavaScript and C#, and it was considered better usability to make those types short, recognizable and easy to write.

Making String be string was not a trade-off that seemed worth it. It would probably have worked perfectly well if we had used string instead, but we didn't. We are not going to change that now.

(If we get generalized type aliases, you can probably define your own typedef string = String;. I implore you not to, because it would not improve the readability of your code. Historically, the reason Java did not have a C-like #define functionality was explicitly because they did not want people writing in a myriad of private dialects that other people couldn't read).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...