News for hackers. Stuff that matters.
Re:Ill-Advised C++ Rant, Part 2 (2016) by colanderman on Wednesday October 25, 2017 @04:06PM (#15553650)
> For instance F# is able to couple actual data files like CSV to symbols and types in the language

Of course that breaks once your CSV header names don't match allowable symbol names. Notice the example linked doesn't show accessing the "Adj Close" field. (Presumably there is a way to get at this in F#? There's no way to "escape" characters in symbols in C++ though.)

Sure, it speeds development, but first time a requirement comes down that says "foo_bar field must be named foo-bar", what should be a one-character change suddenly becomes something much more.

> why ? There are plenty of cases where this has proven quite useful.

"Useful" and "bad idea" are not mutually exclusive. A lot of things which are "useful" – i.e., which speed development or save keystrokes – have costs which hamper maintenance. (Think macros.)

RTTI generally implies that your program has logic to do something based on the type of a value, rather than on the meaning of a value. Say I have an object, BasketOfApples, with a field size_of_basket, which can be either an integer or a string. If it's an integer, it refers to the number of apples the basket can hold. If it's a string, it's either "small", "medium", or "large". OK so far.

But now I want my BasketOfApples to also be able to have a size defined as the weight in pounds it can hold. I can't use an integer again, that already has meaning! But I want a numeric type. So I'm forced to either choose a distinct numeric type (say float), or use an enum to distinguish the cases. The former forces me to make a suboptimal design decision. The latter is probably what I should have done in the first place.

So now my BasketOfApples has an enum field, type_of_basket_size, which determines the meaning of the basket_size value. Not only does type_of_basket_size do what RTTI failed to, it obviates the need for it, and is likely a bit more efficient to boot.

(BTW, you see this all the time with C++ constructors. You can't have two constructors which mean different things but accept the same types without using a dummy "tag" argument.)

Languages with tagged variant types (e.g. OCaml, and I presume by extension, F#) don't have this problem and don't need (or even supply) RTTI information, and they get along just fine. To me, using a feature that isn't available or missed in other "serious" languages, which incompletely accomplishes the task to which it's set, is a code smell.

BTW, I recognize that not everyone agrees with me on this point. But I've been coding like RTTI doesn't exist for decades in various languages and not once have I missed it.

> Besides, "types as first-class primitives" does not necessarily imply "run-time": in the C++ spirit it would most certainly be a compile-time only feature.

The OP's example (copying an object without knowing its type at compile time) specifically requires RTTI.

Re:Ill-Advised C++ Rant, Part 2 (2016) by stvc on Wednesday October 25, 2017 @07:31PM (#15555200)
> Of course that breaks once your CSV header names don't match allowable symbol names. Notice the example linked doesn't show accessing the "Adj Close" field.

In F# you can escape symbol names with double backticks. In the example linked, accessing the "Adj Close" field would be done like:

    firstrow.``Adj Close``
Re:Ill-Advised C++ Rant, Part 2 (2016) by lomnakkus on Thursday October 26, 2017 @04:32AM (#15557269)
> Languages with tagged variant types (e.g. OCaml, and I presume by extension, F#) don't have this problem and don't need (or even supply) RTTI information,

I'm not sure about F#, but I'm almost 100% certain that OCaml does have a runtime "tag" to distinguish cases (and indeed does so at runtime when e.g. pattern matching). That would qualify as a type of RTTI in my book.

(There really isn't way around this in general, except for certain trivially optimizable cases like one-constructor types, etc. But even in those cases runtimes may choose to not do the optimization just to have a uniform runtime representation.)

Re:Ill-Advised C++ Rant, Part 2 (2016) by colanderman on Thursday October 26, 2017 @10:53AM (#15559328)
OCaml has explictly tagged variant types (like Java enums). There is no untagged variant type, and all types must be known at compile type, therefore there is no means to match based on "type" of a value.

OCaml does have hidden RTTI, but it is not what you think it is. It exists only so the garbage collector can distinguish the various built-in data types, and is completely opaque to user code (save the Obj module). e.g. all structures have the same tag:

  # type foo = { a: int; b: string };;
  type foo = { a : int; b : string; }
  # Obj.tag (Obj.repr { a = 5; b = "foo" });;
  - : int = 0
  # type bar = { x: int };;
  type bar = { x : int; }
  # Obj.tag (Obj.repr { x = 1 });;
  - : int = 0
  # Obj.tag (Obj.repr 123);;
  - : int = 1000
  # Obj.tag (Obj.repr "hello");;
  - : int = 252
In particular, this tag is certainly not used for pattern matching of anything but explicit variants, where it is used to distinguish values, not types, and hence is not RTTI.

[1] https://realworldocaml.org/v1/en/html/memory-representation-...

[2] https://caml.inria.fr/pub/docs/oreilly-book/html/book-ora115...

Re:Ill-Advised C++ Rant, Part 2 (2016) by lomnakkus on Thursday October 26, 2017 @03:39PM (#15562269)
OK, so I'm probably just confused here, but: It's not particularly surprising that structures have the same tag, but sum types (aka variants) do. (As you note.)

That's certainly runtime-information about the types of values, but I suppose our disagreement may be about the exact meaning of RTTI. After looking at the Wikipedia entry, I see that my intuition doesn't align with the C++ 'definition' of RTTI, either so thanks for correcting that misconception. It actually looks more like Haskell's Typeable in that it (can apply)/(applies) to any type even primitive types. Of course Typeable is opt-in and stored separately from the values, so...

I'm still not sure I understand your objection, but maybe I just haven't read your OP closely enough and/or thought about it long enough.

Re:Ill-Advised C++ Rant, Part 2 (2016) by colanderman on Thursday October 26, 2017 @06:58PM (#15563985)
The information in a variant has nothing to do with the type. I can declare:

  type foo = Bar of int | Baz of int
And the two variant cases remain distinguishable, despite both having fields of the same type, and both being members of the same type. No information, runtime or otherwise, about the type, is involved. Therefore it is not by any definition RTTI. Pattern-matching is performed solely based on the explicit tag names.

Consider that OCaml variants are in effect no different from the standard C pattern of enum + union. And I think we can agree that C has no RTTI whatsoever.

Now if I could write:

  type foo = int | string
Then pattern-matching that would require RTTI. But OCaml doesn't support such things.
Re:Ill-Advised C++ Rant, Part 2 (2016) by lomnakkus on Thursday October 26, 2017 @07:44PM (#15564280)
You're just reiterating what we agree on. (I think I stated as much, but maybe not.)

EDIT: Just in case: We do agree that certain programs in dependently typed languages would require such reification of type information, yes? That is, we're talking about a run-time decision about choosing a type (or another type), yes?

Re:Ill-Advised C++ Rant, Part 2 (2016) by colanderman on Thursday October 26, 2017 @10:51PM (#15565329)
I'm talking about run-time introspection into the type of a value. That is, the ability to ask the question, "is this arbitrary value an integer or a string?" That is what the OP needs for his example (clone an object of unknown type). That is something you need RTTI for, and something that I claim is better served by explicit variant tags.

I don't know what you mean by "run-time decision about choosing a type (or another type)". Can you give an example of some code that does this?

I also don't know what you mean by dependently typed languages requiring RTTI. Dependent type systems, like HM and other type systems, only constrain programs, they do not enable any behavior. Any program valid in an arbitrary dependently-typed language is still valid if the typing system is removed, rendering the language untyped (and thus devoid of any RTTI). (Specifically, DT adds to HM the ability to write expressions whose type or validity depends on other values. It does not grant ability to introspect those types; that is out of the scope of a type system.) Example: Coq is dependently typed, but has absolutely no notion of RTTI.

Re:Ill-Advised C++ Rant, Part 2 (2016) by jcelerier on Friday October 27, 2017 @05:15AM (#15566822)
> Dependent type systems, like HM and other type systems, only constrain programs, they do not enable any behavior.

The question arises when you have languages that compile to native code: you want your constraints to still be valid when you load new types in your program at run-time.

eg

    obj = dlopen("libfoo.so")
    factory = /* insert some cast here */ dlsym(obj, "some_factory_function")
    a_value = factory.create_value()
    some_constrained_function(a_value)
Re:Ill-Advised C++ Rant, Part 2 (2016) by colanderman on Friday October 27, 2017 @08:26AM (#15567645)
What part of that example requires RTTI? Clearly the type of `factory` (and thus the type of values it produces, even if they vary with input values) is known at compile time, thanks to the cast. And confirming that the dynamically loaded code adheres to that type is a loader problem (which can be solved with metadata), not a language/runtime problem (which would require RTTI and the overhead of checking it).

The point of a static type system, either HM or a DT system, is to ensure that the types are either known at compile time, or derivable from some other runtime value (e.g. input to a function). This is for two reasons: safety, and the ability to elide RTTI. (Note that a type system forbids you from doing anything with a value with a dependent type until it finds its way into a conditional branch which simplifies its type to a non-dependent one.)

Re:Ill-Advised C++ Rant, Part 2 (2016) by jcelerier on Saturday October 28, 2017 @01:14PM (#15575845)
> thanks to the cast.

well, exactly: languages are able to do without the cast at all, just extracting type information from the runtime, which is much safer.

Re:Ill-Advised C++ Rant, Part 2 (2016) by colanderman on Saturday October 28, 2017 @03:37PM (#15576452)
You wrote the example, not me. I'm confused what your argument is.

Are you saying that dynamic loaders should check the type of dynamically loaded code against the type expected by the code loading it? I agree that's a good idea, but it doesn't require language support for general RTTI, just a type-aware shared-object format and dynamic loader. (Heck, C++ symbol names already incorporate function argument type signatures, so this isn't a huge stretch.)

Are you saying that a runtime shouldn't need to trust that dynamically-loaded code actually implements the interface it claims to? If so, that's an incredibly specific requirement that's really better served by sandboxing or an RPC-like interface (especially in C/C++).