2x1=10

because numbers are people, too
Persönliches
Fotografie
Programmierung
    • Rust: adding arbitrary things

      I just start­ed learn­ing Rust some days ago and, com­ing from a C/C++/C# back­ground, imme­di­ate­ly stubbed my toe on the type sys­tem. The notion of traits, how­ev­er, is some­thing I real­ly like and allows for a stunt that is impos­si­ble in C# and ugly in C++: adding things, where by things I mean all the things.

      Add all the things!

      The key is to con­strain a gener­ic type para­me­ter with the Add<T> trait. In this exam­ple, I’m con­strain­ing a gener­ic type T1 to be Add<T2>, that is, to be able to add a T2 to itself. The result of such an oper­a­tion is obvi­ous­ly gener­ic as well, <T1 as Add<T2>>::Output (with Output being the type), allow­ing for the fol­low­ing code:

      fn add<T1, T2>(a:T1, b:T2) -> <T1 as Add<T2>>::Output
          where T1: Add<T2>
      {
          a+b
      }
      

      While this func­tion does noth­ing more than adding two val­ues of arbi­trary types (try that in C# with prim­i­tive types), the under­ly­ing mech­a­nism allows for inter­est­ing oper­a­tions such as vec­tor-matrix mul­ti­pli­ca­tion with­out hav­ing to resort to a con­crete type. A Complex would work as well as a prim­i­tive f32 (i.e. float), f64 (i.e. double), etc.
      You can do this in C++, but instead of hav­ing the com­pil­er check the traits (like rustc does), you rather have to hope that some­one imple­ment­ed that spe­cif­ic oper­a­tor over­load or else you’d be in tem­plate error mes­sage hell.

      Dezember 10th, 2015 GMT +2 von
      Markus
      2015-12-10T22:01:56+02:00 2015-12-10T22:05:00+02:00 · 0 Kommentare
      Generics
      Rust

      Hinterlasse einen Kommentar

      Hier klicken, um das Antworten abzubrechen.

    1. « newer
    2. 1
    3. …
    4. 5
    5. 6
    6. 7
    7. 8
    8. “Compiler crashed with code 1” on Mono" rel="next prefetch">9
    9. 10
    10. 11
    11. …
    12. 43
    13. “Compiler crashed with code 1” on Mono">older »
    • Kategorien

      • .NET
        • ASP.NET
        • Core
        • DNX
      • Allgemein
      • Android
      • Data Science
      • Embedded
      • FPGA
      • Humor
      • Image Processing
      • Kalman Filter
      • Machine Learning
        • Caffe
        • Hidden Markov Models
        • ML Summarized
        • Neural Networks
        • TensorFlow
      • Mapping
      • MATLAB
      • Robotik
      • Rust
      • Signal Processing
      • Tutorial
      • Version Control
    • Neueste Beiträge

      • Summarized: The E-Dimension — Why Machine Learning Doesn’t Work Well for Some Problems?
      • Use your conda environment in Jupyter Notebooks
      • Building OpenCV for Anaconda Python 3
      • Using TensorFlow’s Supervisor with TensorBoard summary groups
      • Getting an image into and out of TensorFlow
    • Kategorien

      .NET Allgemein Android ASP.NET Caffe Core Data Science DNX Embedded FPGA Hidden Markov Models Humor Image Processing Kalman Filter Machine Learning Mapping MATLAB ML Summarized Neural Networks Robotik Rust Signal Processing TensorFlow Tutorial Version Control
    • Tags

      .NET Accelerometer Anaconda Bitmap Bug Canvas CLR docker FPGA FRDM-KL25Z FRDM-KL26Z Freescale git Gyroscope Integration Drift Intent J-Link Linear Programming Linux Magnetometer Matlab Mono Naismith OpenCV Open Intents OpenSDA Optimization Pipistrello Player/Stage PWM Python Sensor Fusion Simulink Spartan 6 svn tensorflow Tilt Compensation TRIAD ubuntu Windows Xilinx Xilinx SDK ZedBoard ZYBO Zynq
    • Letzte Kommetare

      • Lecke Mio bei Frequency-variable PWM generator in Simulink
      • Vaibhav bei Use your conda environment in Jupyter Notebooks
      • newbee bei Frequency-variable PWM generator in Simulink
      • Markus bei Using TensorFlow’s Supervisor with TensorBoard summary groups
      • Toke bei Using TensorFlow’s Supervisor with TensorBoard summary groups
    • Blog durchsuchen

    • Dezember 2015
      M D M D F S S
      « Sep   Sep »
       123456
      78910111213
      14151617181920
      21222324252627
      28293031  
    • Self

      • Find me on GitHub
      • Google+
      • Me on Stack­Ex­change
      • Ye olde blog
    • Meta

      • Anmelden
      • Beitrags-Feed (RSS)
      • Kommentare als RSS
      • WordPress.org
    (Generiert in 0,263 Sekunden)

    Zurück nach oben.