Fact checking for my talk

ZombineDev via Digitalmars-d digitalmars-d at puremagic.com
Tue Aug 16 08:08:46 PDT 2016


On Tuesday, 16 August 2016 at 06:13:18 UTC, Ethan Watson wrote:
>
> [snip]
>
> At this point, the only thing I still haven't found concrete 
> information on is function inspection in Swift and Rust, which 
> should be a mark against the languages if it's not easily 
> Googlable.

 From what I could find, definitely no other language from the 
list than C++ and D provides any sort of compile-time reflection. 
Between C#, Swift and Rust, C# has the most mature and 
fully-featured runtime reflection support. Almost every major 
.NET framework makes heavy use of it. I have even seen it 
casually used in combination with System.Reflection.Emit [1] for 
runtime codegen (for efficient DataBinding or FFI to native 
libraries, when P/Invoke is not enough). Because of the nature of 
CLR's JIT the difference between CT and RT is quite fuzzy. This 
even makes for some twisted form of `static if` [2.1] (look for 
the use of `if (typeof(T) == typeof(Int32))` in the code [2.2]). 
Though the actual code gen and optimization is treated like an 
implementation detail - I don't think it is guaranteed in any way.

The only thing that I could find about Rust is that they didn't 
want to support any reflection beyond `typeid` [3] , because 
apparently that bloated binaries too much and such cost was too 
much for every project to pay. `typeid` is used by their `Any` 
trait [4] which basically is used for dynamic casting. From what 
I understand, you can't even tell at runtime if an object 
implements a trait. All you can do is use object.is::<T>() to 
check if the object is of some concrete type.

Swift developers, on the other hand, explicitly state that they 
don't want to support any form compile-time metaprogramming: [5]. 
Ironically they make heavy use of it in their standard-library. 
However instead of writing the meta code in Swift, they use 
Python [6] for some weird variant of .NET's T4 preprocessor 
templates [7]. As for runtime reflection, as Jacob said, you can 
leverage the Objective-C heritage if your classes derive from 
`NSObject` [8]. Otherwise, due to the needs of things like 
XCode's Playground (which btw, is a poor imitation of Bret 
Victor's ideas [9]), Swift 2 also features some new runtime 
reflection capabilities [10] [11].

[1]: 
https://msdn.microsoft.com/en-us/library/system.reflection.emit(v=vs.110).aspx
[2.1]: 
https://github.com/dotnet/corefx/blob/v1.0.0/src/System.Numerics.Vectors/src/System/Numerics/Vector.tt#L17
[2.2]: 
https://github.com/dotnet/corefx/blob/v1.0.0/src/System.Numerics.Vectors/src/System/Numerics/Vector.cs#L95
[3]: 
http://stackoverflow.com/questions/36416773/how-does-rust-implement-reflection
[4]: 
https://github.com/rust-lang/rust/blob/master/src/libcore/any.rs
[5]: 
https://github.com/apple/swift/blame/master/docs/Generics.rst#L85
[6]: 
https://github.com/apple/swift/blob/swift-DEVELOPMENT-SNAPSHOT-2016-08-15-a/stdlib/public/core/FloatingPoint.swift.gyb#L1485
[7]: https://msdn.microsoft.com/en-us/library/bb126445.aspx
[8]: http://stackoverflow.com/a/24072677
[9]: http://worrydream.com/LearnableProgramming/
[10]: 
https://developer.apple.com/library/tvos/documentation/Swift/Reference/Swift_Mirror_Structure/index.html
[11]: 
https://appventure.me/2015/10/24/swift-reflection-api-what-you-can-do/


More information about the Digitalmars-d mailing list