HaxeFoundation / haxe.org-comments

Repository to collect comments of our haxe.org websites
2 stars 2 forks source link

[haxe.org/manual] Typedef #102

Open utterances-bot opened 2 years ago

utterances-bot commented 2 years ago

Typedef - Haxe - The Cross-platform Toolkit

Haxe is an open source toolkit based on a modern, high level, strictly typed programming language.

https://haxe.org/manual/type-system-typedef.html

daverave1212 commented 2 years ago

A very useful feature would be to be able to nest typedefs inside other classes/functions/typedefs. I constantly use typing for dynamic types and I don't like inflating my code with large type definitions, so I create typedefs. The problem is, I would need to switch the file or go to the top/bottom of the current class to see what the type actually contains. It would be neat to have a typedef right near the variable that uses that type.

It's not supported on try.haxe.org, so I assume it's not supported in the actual language either.

Aurel300 commented 2 years ago

I agree, scoped type definitions would be great. I don't know that the compiler team feels this way though ¯_(ツ)_/¯

marcelEuchnerMartinez commented 2 years ago
Gama11 commented 2 years ago

@marcelEuchnerMartinez They're completely separate concepts, just commonly combined. A typedef itself is just an alias for a type. You can use it to give an anonymous structure a name.

marcelEuchnerMartinez commented 2 years ago

When I use a typedef to replace an anonymous structure do I overcome the performance issue of the latter? — I would like to know what a typedef is precisely. While types don't mention typedef explicitly, typedef being in another section (under Type System) and your comment (it's "itself just an alias") speak for typedef being just an alias allowing to structure code (and nothing more then, no impact at runtime)... but then the wiki text above mentions that "Typedefs are not textual replacements, but are actually real types." so that is a little confusing. What are they really?

nanjizal commented 2 years ago

Not technical but my rough understanding.

A typedef is often a documented anonymous structure. It allows re naming of classes or naming Type structures. typedef Listeners = Array< ( Event ) -> Void >;

It is not heavy on targets when used mostly as renaming structures. But if you want to use it like a class of: no methods only parameters, then use a structInit class instead, it seems faster on c++ and js as they can better type and optimise typed classes than objects.

But typedef's are bit strange they come more from functional languages.

typedef implementation varies - but it's like an Object {} or a type alias, if the compiler can optimize it at runtime to largely disappear then it's obviously light, otherwise if it is a type object then it may not be optimal often a proper class is better typed by V8 or by gcc.

The haxe compilers ability to restrustucture and optimise code can muddy what structure to use to get fast and simple results, so often you need to test and look at the output.

marcelEuchnerMartinez commented 2 years ago

@nanjizal Okay, I think this gives me a direction. I probably stick to classes right away for now then. I get the idea that the compiler does unclarify things here (in favor of optimization). That's a good hint with C++ and gcc. It shows one just has to write custom benchmark tests to get the best performance. Thank you!

Sedomanai commented 1 year ago

Is typedef what I think it is? I come from c++ is it the same thing? I'm still reading this thread and it makes me more confused. I'm not intent on using anonymous structures, if I only use typedef as a type alias I shouldn't be worried, correct?

RblSb commented 1 year ago

Yes, this is compile-time alias to any type you want.

fy0 commented 6 months ago

Is this similar to typescript and golang's interface? I'm care about memory structure, like struct in C, or everything is hashmap?