Похожие чаты

It seems that letting Go infer (what to us are)

obvious variable types [1] instead of stating them explicitly [2] all the time is a good practice.

[1] a := 5
[2] var a int = 5

However, won't it slow down the compiler in the long run?

Because to my understanding, type inference is one of the reasons why interpreted languages are relatively slow 🤔

7 ответов

4 просмотра

Short answer: not really. Long answer: actually, why not run a benchmark? 😀 generate code with lots and lots of variable declarations and measure the compilation time & cpu/memory usage

Explicitly stating the type does not skip type inference. Some type info still has to be inferred from the value you're assigning to check if it is compatible with the type. Perhaps stating the type explicitly is slower as it forces a compatibility check (which is skipped with inference), but that's just speculation.

I really don't wanna get into a fight but interpreted languages are slow not because of just lack of types, they are slow because they don't have machine code as a binary to run, a runtime will parse them while they are being executed generally... as for type inference in Go there are costs but explicit types don't matter they don't add much to perf because they almost always generate the same machine code in Go... because types on assignment are always inferred before compilation happens. Worst case, your compilation speed is slightly slower(milliseconds if at all), Note: I haven't looked at how the Go compiler handles types(my complaints for it's practices aside), I don't see a reason for it's inference to be much slower. But the code generated is the same, so you should not worry... If you are using pointers and loose types and maybe a lot of casts then yes you are very likely hurting performance, otherwise don't care that much.

swarnim
I really don't wanna get into a fight but interpre...

btw from what I know, Go doesn't really have a heavy type system at all, it just takes the type of right hand side and uses that on the left hand side at compile time. Finally generated code is exactly the same hence. Some one with more experience with Go internals or something can feel free to correct...

swarnim
btw from what I know, Go doesn't really have a hea...

this means, [1] a := 5 [2] var a int = 5 1 and 2 are the exact same just look different

it's not at all because of type inference that interpreted languages are slow and having go compiler figure out the type of variable by type of value is pretty much zero cost compared to the rest of type checking

Похожие вопросы

Обсуждают сегодня

Добрый день. Хочу сделать отрисовку по команде на панели. Почему-то рисуется только при втором вызове. С чем может быть связано, не подскажете? procedure TForm1.FormDblClick(...
Kirill Filippenok
20
а зачем этот вопрос для удаления из чата?
Mёdkinson Medvezhkin
63
А почему в си некоторые вещи работают с двойными кавычками некоторые с одинарными? Нельзя было все сделать с одними или чтоб работало с разными? например чтоб выводить строки ...
.
15
using next image component with s3 image: "url" parameter is valid but upstream response is invalid code: <Image fill src={s...
Fasil
3
Всем привет! Нужен совет от опытных. Переношу свой проект с Делфи 10.2 Токио на Лазарус 3.2 установленный через инсталлятор fpcupdeluxe-x86_64-win64. При импортировании проект...
Дмитрий Завгородний
7
Всем привет! procedure TForm1.FormCreate(Sender: TObject); type TStartEnd = record S: Byte; E: Byte; end; var a, b: TStartEnd; begin {1} a.S := 1; {2} a.E := 2; ...
Руслан Михайлович
10
Всем привет!) я тут новенький и пытаюсь освоить evolution методом тыка. У меня при переходе между папками файлов выскакивают вот такие уведомления Можете подсказать как их от...
Диман Samoed
10
Эх кто-то пришел и весь праздник испортил :( You need complex FBX scene importing setup to change things on import? good luck with that. You need navigation and pathfinding? g...
Serg Gini
5
Friends, how can I find my Wazirx wallet address?
Silm Silm
31
Всем привет! Подскажите. Я написал приложение на Delphi 10.2 Tokyo под Windows 10. И передо мной стал вопрос о том чтобы сделать это приложение кроссплатформенным (под Linux и...
Дмитрий Завгородний
24
Карта сайта