News for hackers. Stuff that matters.
Re:Notes on Idris (2017) by weavie on Friday June 08, 2018 @07:19AM (#17264267)
I wasn't aware that C++ could do this. It's come on a long way since I last looked at it! How far can it take these dependent types?

Can you specify a function in the type signature that is called by the compiler that can determine the type of the parameter?

Something like :

    stringOrInt:: Bool -> Type
    stringOrInt True = String
    stringOrInt False = Int

    doSomething:: x: Bool -> StringOrInt x -> String
doSomething can then be called like :

    doSomething True "Ook"
    doSomething False 4
Re:Notes on Idris (2017) by greydius on Friday June 08, 2018 @08:56AM (#17264804)

    #include <iostream>

    template<bool B, typename T>
    std::string doSomething(T t);

    template<>
    std::string doSomething<true>(std::string s) {
      return "true!";
    }

    template<>
    std::string doSomething<false>(int i) {
      return "false!";
    }


    int main() {
      std::string foo {"foo"};

      auto s1 = doSomething<true>(foo);
      auto s2 = doSomething<false>(42);

      std::cout << s1 << ", " << s2 << std::endl;
  
      return 0;
    }