The person who respects another person because of his wealth, 2/3 of his religion is gone - Imam Ali SWT
وَ مَنْ أَتَى غَنِيّاً فَتَوَاضَعَ لَهُ لِغِنَاهُ ذَهَبَ ثُلُثَا دِينِهِ - امام علی ع
Labeling types enables you to write a more self-documented code, both easy for you to review in the future and for other people to get the idea behind your algorithm.
it is specially good for proc
s that have many lines of codes; as it is not much obvious what they are doing exactly...
assume you're reading someone's code, you come upon something like this:
var cars: Table[string, bool]
wow! what is it? is that a table for (ownerName
-> isWrecked
) or (carName
-> hasWheels
) ??
you wanna develop a telegram bot using telebot for example, you come upon UpdateCallback
type:
proc(bot: Telebot, update: Update): Future[bool]
what is Future[bool]
for? why a bool
? you can't even guess it without reading the docs [RTFM right? :D]
you've stored list of attedences' names by age.
let namesByAge: seq[seq[string]]
Isn't seq[seq[string]]
like a 2D matrix of names ?!?!?! 😵
but if you could somehow just label your types, the code would be a lot more readable!
the labeledtypes
package enables you to do this:
var cars: Table[name !: string, isReady !: bool]
# or
var cars: Table[!(name: string), !(isReady: bool)]
proc(bot: Telebot, update: Update): shouldEndWaiting !: Future[bool]
# or
proc(bot: Telebot, update: Update): !(shouldEndWaiting: Future[bool])
let namesByAge: seq[(age: int) !> (names: seq[string])]
I like the idea of named return in golang:
func join(paths ...string) (joined_path string)
func split(sum int) (x, y int)
nimble install labeledtypes
# or
nimble install https://github.com/hamidb80/labeledtypes
See tests/test.nim
.
see tests/talternative.nim
.
See other comments on this. it was originally a proposal.