6 ms·
But a colon is also often used to indicate a mapping of names or categories to value. For instance: Breakfast: eggs and bacon Lunch: falafel sandwich Din
by djur 7y ago
But a colon is also often used to indicate a mapping of names or categories to value. For instance:
Breakfast: eggs and bacon
Lunch: falafel sandwich
Dinner: BBQ pork and slaw
The type of the variable is the "explanation" here.
val x: String
"x, which is a String"
val x: String = "hello"
"x, which is a String, is initialized with 'hello'"
- dirtydroog 7y agoWhat's the point of the 'val' keyword? It reminds me of Visual Basic's "Dim x As String".
- RhodesianHunter 7y agoval is immutable, var is mutable
- saagarjha 7y agoImmutable, as opposed to var which declares a mutable variable.
- djur 7y agoHaving a keyword for variable initialization makes parsing easier and less ambiguous. "let", "var", etc. are also used for this purpose, but I was going along with the example.
- hinkley 7y agoYour examples have the things on the right and the type on the left, and then used it to describe why examples on the left and [types] on the right makes sense... If anything you've just argued that String: x, y is a preferable declaration style.
- djur 7y agoThe point of the list isn't to say "eggs and bacon is a kind of breakfast", it's to say "breakfast is eggs and bacon". It's "name: details about name", not "type: instance".
- htfu 7y agoWouldn’t that argue in favor of val breakfast: “eggs and bacon” = String and not val breakfast: String = “eggs and bacon”
- djur 7y agoMy intention was solely to counter the idea that using ":" to mean "is" or "is a" is somehow inconsistent with written English. I don't think it's always necessary or even desirable to match the use of symbols in programming with English orthography, in any case. The use of a declaration with initialization as the example here muddies the water. Whatever syntax you use has to work for uninitialized declaration of variables, function arguments, and structure members: val breakfast: String fun serveBreakfast(breakfast: String) struct MealPlan { breakfast: String } Declaration with initialization just needs to be consistent with these.