<p>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.</p>
<p>I have a few questions about this:</p>
<ul>
<li>Is my understanding stated above correct? Are they really completely disjoint?</li>
<li>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.</li>
<li>Are there any other languages that exhibit a similar phenomenon?</li>
</ul>

<language-design><c><c++><objective-c>
Disjoint language extensions (C++ and Objective-C)
CC BY-SA 4.0
2023-05-30T10:30:25.387Z
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.
kaya3
CC BY-SA 4.0
2023-05-30T11:16:52.347Z
@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)
chrysante
CC BY-SA 4.0
2023-05-30T11:46:19.773Z
`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`.
Bbrk24
CC BY-SA 4.0
2023-05-30T12:44:25.993Z
What is the basis of your assumption that the syntaxes are disjoint? Is this just something you have observed personally?
Bruce Adams
CC BY-SA 4.0
2023-05-30T12:49:34.580Z
@BruceAdams Yeah, pretty much. And the fact that they can coexist and interact in one file. But if that's not true in general that would make a great anwer :)
chrysante
CC BY-SA 4.0
2023-05-30T16:04:28.787Z
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.
Bruce Adams
<h2>Polyglot programs &amp; Dialecting</h2>
<p>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 <a href="https://en.wikipedia.org/wiki/Polyglot_(computing)" rel="nofollow noreferrer">polyglot</a> 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:</p>
<pre><code>#!env python
</code></pre>
<p>If we talk about languages where independent extensions are part of the design then the <a href="https://en.wikipedia.org/wiki/Rebol" rel="nofollow noreferrer">rebol</a> family (including <a href="https://en.wikipedia.org/wiki/Red_(programming_language)" rel="nofollow noreferrer">red</a>) is worth looking at.
These languages have the concept of <a href="https://en.wikibooks.org/wiki/Rebol_Programming/Language_Features/Dialects" rel="nofollow noreferrer">dialecting</a>.
Dialecting essentially means the language contains a number of mostly independent DSLs (dialects) each specialised for a particular function.</p>
<p>In Rebol one of these is a dialect (parse I think) which is used to define other dialects.</p>
<p>Prolog has a similar feature in that you can write <a href="https://www.metalevel.at/prolog/dcg" rel="nofollow noreferrer">definite clause grammers</a> but its not as directly accessible.</p>
<p>I can't answer for the specific case of C++ and objective-C myself but <a href="https://stackoverflow.com/a/3684159/1569204">this answer on SO</a> suggests it was intentional:</p>
<blockquote>
<p>you might want to read Apple's (sadly deleted, but archived) documentation on <a href="https://web.archive.org/web/20101203170217/http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocCPlusPlus.html" rel="nofollow noreferrer">Objective-C++</a>.</p>
</blockquote>

CC BY-SA 4.0
2023-05-30T15:57:43.117Z
2023-05-30T15:57:43.117Z
Bruce Adams
# Disjoint language extensions (C++ and Objective-C)
Posted by: username_0 on 2023-05-30
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?
**Tags:** language-design c c++ objective-c
> **Comments:**
> **username_1**: 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.  
> **username_0**: @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)  
> **username_2**: `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`.  
> **username_3**: What is the basis of your assumption that the syntaxes are disjoint? Is this just something you have observed personally?  
> **username_0**: @BruceAdams Yeah, pretty much. And the fact that they can coexist and interact in one file. But if that's not true in general that would make a great anwer :)  
> **username_3**: 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.  
---
## 1 answers
### Answer by username_3 on 2023-05-30. Score: 2
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++.
---
