News for hackers. Stuff that matters.
Re:Swift 2.2 Released by fauigerzigerk on Tuesday March 22, 2016 @08:11AM (#11335860)
I'd like to hear more about that because I'm not sure I understand. Can you give an example for something you cannot do in C++ in a type safe way that you can do in C#, Java or Swift?
Re:Swift 2.2 Released by pkolaczk on Wednesday March 23, 2016 @06:20AM (#11343218)
I already gave one. You can't write a generic list implementation and make the compiler prove your code is sound (according to the rules of the C++ type system) for any valid type argument value.
Re:Swift 2.2 Released by pjmlp on Wednesday March 23, 2016 @07:38AM (#11343526)
You obviously need to learn about template metaprogramming, enable_if, type traits and SFINAE.

C++ templates are Turing complete, you can validate whatever you feel like at compile time.

Re:Swift 2.2 Released by pkolaczk on Wednesday March 23, 2016 @08:16AM (#11343714)
I'm absolutely aware of enable_if, type traits and SFINAE. But they do not address the concern I raised here. The things you mention come to play on template instantiation, after the concrete (non-generic) types are already known. The problem I mention is that with templates you cannot type check generic code. I can put whatever nonsense enable_ifs on my template declaration and the compiler does not check if they are satisfied by the implementation.

You can write the following C++ code and C++ allows it:

<template typename T> void foo(const T& arg) { arg.bar(); }

This is perfectly fine for a macro system. But this is not how truly generic type system would work. If templates were proper generics, the typechecker would immediately refuse this to compile saying something like: "Error: I can't prove T has the bar() member."

> C++ templates are Turing complete, you can validate whatever you feel like at compile time.

So please write a template metaprogram that validates if a given C++ class (input to the program, given as a template argument) matches a table schema in the database system (the table name and connection string are also given as template arguments). For fetching the schema, you have to connect to the DB, of course at compile time. If the schema does not match, the compilation should fail ;)

Re:Swift 2.2 Released by pjmlp on Wednesday March 23, 2016 @08:32AM (#11343816)
Because whoever wrote foo () was too lazy to make use of type traits and validate that T has a method called bar with zero arguments.

Do you want an example how to write it properly?

With or without the upcoming concepts TS already in GCC 6.0?

Also this example would require T to be an interface with a bar method in C#. Do you also want to the the variant with MI and pure abstract classes in C+?

Re:Swift 2.2 Released by pkolaczk on Wednesday March 23, 2016 @09:36AM (#11344227)
> Because whoever wrote foo () was too lazy to make use of type traits and validate that T has a method called bar with zero arguments.

In a proper generic type system, the compiler validates it, not the programmer. Even if you constrain the type using type traits, the compiler would not check if the implementation satisfies the constraints. I can put a type trait for existence of bar() and then another programmer may rename bar() to baz() forgetting to update the constraint and the code will still compile fine.

Re:Swift 2.2 Released by pjmlp on Wednesday March 23, 2016 @10:19AM (#11344665)
Except that reasoning fails with concepts, already available in GCC 6.0 and as a TS.
Re:Swift 2.2 Released by pkolaczk on Wednesday March 23, 2016 @11:23AM (#11345285)
So you're saying concepts will reject libraries that compile fine today?
Re:Swift 2.2 Released by pjmlp on Wednesday March 23, 2016 @01:04PM (#11346266)
They might, if used properly.

I do admit that I need to update myself with what GCC 6.0 allows vs the letter of the TS.

Re:Swift 2.2 Released by fauigerzigerk on Wednesday March 23, 2016 @05:45PM (#11348523)
I don't think concepts are even necessary to do what pkolaczk wants to do, which is (I think) to constrain the argument to a particular interface, say Bar:

  template <typename T> 
  void foo(const T& arg) {
    const Bar* b = arg;
    b->bar();
  }
or

  template <typename T> 
  void foo(const T& arg) {
    static_assert(is_base_of<Bar, T>::value, "T must be a Bar");
    arg.bar();
  }
I can't say I'm a huge fan of either syntax, but the compiler will perform static type checking as if you had written where T : Bar in C#.
Re:Swift 2.2 Released by pkolaczk on Wednesday March 23, 2016 @06:38PM (#11348840)
The body of the template won't be typechecked until the instantiation of the template. This is different than in C#. If I replaced arg.bar with arg.baz in C#, the generic type definition would not compile, even if I never use it. In C++ I can't write a template and have it typechecked for all possible input types satisfying the type constraints, in C# I can. So C++ type system is weaker than that of C#.
Re:Swift 2.2 Released by fauigerzigerk on Wednesday March 23, 2016 @07:12PM (#11349089)
I see what you mean, but uninstantiated also means unused. So we're not talking about anything that could possibly lead to runtime errors. It's not an issue with type safety.

It definitely has a downside for the development process, especially when it comes to writing libraries. But it is also to some degree inevitable if you want structural typing. Maybe there is a better way to do it though.