7
$\begingroup$

Objective-C and C++ are both extensions of C (or at least started out to be). So for the purpose of this question I allow myself the slightly inaccurate assumption that both languages are pure supersets of C. They have this interesting property, that their syntax extensions added to plain C are disjoint, in the sense that any C++ language construct that is not valid C has no meaning in Objective-C and vice versa. This allows for the hybrid language Objective-C++ to exist.

I have a few questions about this:

  • Is my understanding stated above correct? Are they really completely disjoint?
  • Did this happen on purpose or by chance? I would assume by chance because it would be a weird feature to plan in from the beginning, but it also seems unlikely to simply have happenend by chance.
  • Are there any other languages that exhibit a similar phenomenon?
$\endgroup$
6
  • $\begingroup$ Having disjoint syntax doesn't necessarily mean the semantics are compatible. You would have to define what it means to call a C++ function with a value which only makes sense in Objective-C, and vice versa. Or of course you could forbid this entirely with the type system, but then that would presumably defeat the purpose. Point is, having non-overlapping syntax doesn't say much about whether two languages could work together as a hybrid. $\endgroup$
    – kaya3
    May 30, 2023 at 10:30
  • $\begingroup$ @kaya3 As I understand, objects in Obj-C are opaque C-pointers (like void* or similar), and Obj-C function can also take plain old C-structs as parameters, so they could at least communicate via the shared C constructs. So even if combining the language extensions would be forbidden, this is still valuable to bridge between C++ and Obj-C code. And you can have C++ objects as members of Obj-C classes (and vv I think) $\endgroup$
    – chrysante
    May 30, 2023 at 11:16
  • $\begingroup$ id can’t be converted directly to void* without a bit of work, because id is ARC’d and void* isn’t. However, you’re right about putting one type of class inside the other. I’ll also note that the two languages aren’t perfectly compatible, because C++ changes the meaning of auto. $\endgroup$
    – Bbrk24
    May 30, 2023 at 11:46
  • $\begingroup$ What is the basis of your assumption that the syntaxes are disjoint? Is this just something you have observed personally? $\endgroup$ May 30, 2023 at 12:44
  • 1
    $\begingroup$ You appear to be asking two separate questions here. One about objective-c++ and one about the more general phenomenen. It might improve your question to be specific about which question you really want answered, though I have tried to answer both. $\endgroup$ May 30, 2023 at 16:04

1 Answer 1

2
$\begingroup$

Polyglot programs & Dialecting

This does not really answer the intent of your question which is I think if there are extension languages that are deliberately disjoint from each other but there is something called a polyglot program where the same source file can be parsed correctly into two or more completely unrelated languages. Mostly this is more like programming golf. The only practical use I can think of is bootstrapping a dos or shell script to run something else. For example:

#!env python

If we talk about languages where independent extensions are part of the design then the rebol family (including red) is worth looking at. These languages have the concept of dialecting. Dialecting essentially means the language contains a number of mostly independent DSLs (dialects) each specialised for a particular function.

In Rebol one of these is a dialect (parse I think) which is used to define other dialects.

Prolog has a similar feature in that you can write definite clause grammers but its not as directly accessible.

I can't answer for the specific case of C++ and objective-C myself but this answer on SO suggests it was intentional:

you might want to read Apple's (sadly deleted, but archived) documentation on Objective-C++.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .