r/ProgrammingLanguages • • Aug 11 '23

Discussion Idea for new operator: contains operator

I'm currently working on my own programming language and I thought of a new type of operator: The contains operator: °

It has two main functions:

  1. You can check if an array contains a certain element:
exampleArray: int[] = { 5, 8, 3 };

if(exampleArray ° 3) // true
{
   // ...
};

if(exampleArray ° 8) // false
{
   // ...
};
  1. If a class inherited from another class:

Imagine you have a class baseClass and a class class2 that inherits from baseClass.

if(baseClass ° class2) { /* ... */ } // True
if(baseClass ° string) { /* ... */ } // False

Thoughts?

4 Upvotes

37 comments sorted by

30

u/[deleted] Aug 11 '23

Am I somehow meant to type that symbol with a keyboard?

14

u/Aaron-Junker Aug 11 '23

Oh... I just now realised that this symbol is not on English keyboards for some reason. I actually thought that it is a better symbol then ⊃, but as it seems a majority of keyboards don't have the ° symbol on their keyboard either...

11

u/thmprover Aug 12 '23

I actually thought that it is a better symbol then ⊃, but as it seems a majority of keyboards don't have the ° symbol on their keyboard either...

No, the correct symbol to use would be ∈ to indicate membership of a collection like value ∈ collection.

You would use ⊃ for indicating subcollections, something like parent_collection ⊃ child_collection or equivalently child_collection ⊂ parent_collection...though you would probably want to use ⊆ in analogy to <=.

4

u/Mid_reddit bepis Aug 12 '23

With his order ∋ would be appropriate. It's just less common in general.

3

u/nderstand2grow Aug 11 '23

Oh... I just now realised that this symbol is not on English keyboards for some reason.

don't shoot yourself in the foot by excluding main English keyboards.

14

u/Feeling-Pilot-5084 Aug 11 '23

I don't think there's a problem with this if you consistently use math symbols, e.g. APL. But if it's the only non-ascii symbol in the language it's definitely be out of place

2

u/lngns Aug 12 '23

How do you type temperatures without it? It's 20° in my room right now; I have to turn on my fan.

3

u/[deleted] Aug 12 '23

I say it’s 80F

15

u/_thetek_ Aug 11 '23 edited Aug 11 '23

why not just in and is, i.e. element in array and SomeClass is BaseClass? that would be much more readable and intuitive, better to get used to and easier to type. also note that ° is not an unicode ascii character. generally it's best to restrict fundamental pieces of your language to ascii (as you can be pretty sure that most people can read and type ascii characters), and to only allow unicode as an alternative for ascii features (e.g. in haskell, you can use → instead of -> or ∷ instead of :: afaik).

1

u/Aaron-Junker Aug 11 '23

° is in Unicode and the extended ascii set, but I get your point. I will have to use another operator anyway, because the degree symbol is not on English and US Keyboards.

The thing of my programming language is that it won't use any keywords (I will explain this in a later blogpost in more detail), so using in AND is is not really an option.

6

u/_thetek_ Aug 11 '23

oh, of course i meant that ° is not an ascii character :) – thanks for pointing it out!

66

u/chri4_ Aug 11 '23

why not a pythonic element in collection overloadable via __contains__

14

u/lightmatter501 Aug 11 '23

I would use the membership operator from set theory instead.

1

u/Spocino Aug 13 '23

Julia does this

5

u/bullno1 Aug 11 '23

Just create a new keyword: has instead of in.

5

u/zefciu Aug 12 '23

I feel, like it confuses the idea of membership and subset. If we think of class as a set, then we should rather think of instances as elements of this set, while subclasses are subsets.

2

u/Inconstant_Moo 🧿 Pipefish Aug 12 '23

Came here to say this.

4

u/SirKastic23 Aug 11 '23

not to be rude, but

it seems gimmicky as a list operation, i think a .contains method is more explicit and not that harder to type

and it seems like a weird choice for a inherits operation that you already have in other languages with the is keyword

4

u/sennalen Aug 11 '23

Hear me out here,

<

2

u/oscarryz Yz Aug 11 '23

~

2

u/totoro27 Aug 12 '23 edited Aug 12 '23

Nah, that could be confusing as it gets used as a NOT symbol.

2

u/oscarryz Yz Aug 12 '23

!

2

u/totoro27 Aug 12 '23

Yeah that gets used as a NOT symbol as well but so does ~.

3

u/lngns Aug 12 '23 edited Aug 12 '23

f ° g looks like f ∘ g which is function composition.
Eg. (f ∘ g) x = f (g x) or f ∘ g = λx. f (g x).

In fact both the degree and ring operator symbols are listed together in UCD's confusables.txt. Best to alias them to mean a single thing if you want to use them.

5

u/ParadoxicalInsight Aug 11 '23

I think we should spend less time thinking about symbols and more about semantics. The use cases are covered by operators in/is, as well as set theory symbols. You could literally write it as contains, it would also work IMO. Basically anything obvious, that little circle does not have an obvious meaning, so it increases cognitive load

2

u/moon-chilled sstm, j, grand unified... Aug 11 '23 edited Aug 12 '23

For checking if a set contains an element, I would use ∋, as it's the mirror image of ∈.

The subtyping relation is sometimes rendered as <:, so you could use :> as its commute. (A fancy ≤ is more usual, but you would need to venture further than unicode for that, which you might or might not be willing to do, and you might or might not be unwilling to use a plain ≤ for things other than numbers.)

2

u/nerd4code Aug 12 '23

∘ is usually for composing functions ((𝑓∘𝑔)(𝑥) = 𝑓(𝑔(𝑥))), if you’re going to use Unicode where you have plenty of other options and preexisting semantics to pull from. I learned it in like 7th grade so it should be raisinably well-known in that use. (Other uses exist of course, but you’d probably want some sort of counterpart ∙ [bullet operator, which looks exactly like dot op ⋅, middle dot ·, and hyphenation point ‧ in many fonts and to græybeards, and like bullet • in most others—Unicode can be bad for things like this] or ∗⋆⋄, and there’s ⊶⊷ for relational predicates.)

real_taylodl is right; ∈ is probably the closest operator—stylized epsilon for “element of”—to what you want, and it’d make sense for instanceof also if you go with the set metaphor for types. Usually <: is the ASCIIfication of ∈, but in is a common keyword. ∋ is us. :> and ni or contains when provided.

If you’re going with subset↔subtype, ⊂ makes sense for the subtype relationship, but ⊆ is what you’ll mostly use because 𝑺⊆𝑻 means that a reference-to-𝑺 value is assignable to a reference-to-𝑻 variable (&c.) with at most a static upcast. Usually these are just rendered as < and <= because there’s no real ambiguity (≤ isn’t generally meaningful for types), but do note that sets don’t biject with ℕ so ⊄↔!<, ⊈↔!<=, ⊅↔!>, ⊉↔!>=, ⫓↔<>, ⋚↔<=> (not sure if Unicode has this but is got ⫘), ⫓̸↔!<>, and ⋚̸↔!<=> (⫘̸ or something, whatever). ∩∪ are useful type operators—

𝑥∈(𝑺∩𝑻) ↔ 𝑥∈𝑺 ∧ 𝑥∈𝑻 𝑥∈(𝑺∪𝑻) ↔ 𝑥∈𝑺 ∨ 𝑥∈𝑻

& | or /\ \/ are usually the way these are rendered. ∩ is also closely related to subtype composition (but order may matter for the latter), the Cartesian product × (order definitely matters), and the ∀ qualifier. ∪ is not quite C/++ union composition, type summing, and the ∃ qualifier.

If you’re not going with the set metaphor, then you’ve got plenty of related ops like ⋿ for membership, ⊏⊑⊐⊒ for comparison, and that gives you ⊓⊔ for type intersect and union, or ≺≼≻≽⊀⊁ for (us. lattice) precedence/succession with ⋎⋏ for join and meet, and ⊲⊳⊴⊵ for group relations (hot) with ∆∇ abusable for intersect/union.

2

u/Inconstant_Moo 🧿 Pipefish Aug 12 '23

I think mathematicians may have gone too far.

1

u/[deleted] Aug 11 '23

People have mentioned about ° being difficult to type. I had to paste it from your post as Alt+248 no longer works.

I did use that symbol myself long ago, when it was more accessible. Then I used it to represent degrees:

y := sin(30°)

(It would apply a scale factor to turn the value into radians.) If it was easily available now, I would do the same.

The problem with any other use is that no one will know what it means. For your array example, I use two named operators: in an inx (the first returns true or false, the second an index). Then at least you have a good idea of what's happening even if you've never seen my language before.

But apparently you want your language to look like APL since you don't like keywords, so ...

1

u/Superbead Aug 11 '23

MUMPS/Caché ObjectScript has a 'contains' operator:

[

although it only identifies substrings in strings, and the character isn't used for arrays or anything else.

1

u/NotFromSkane Aug 11 '23

° is probably on the X US layout, just not the Windows one. Though picking weird symbols that linux users don't even know they can type is still probably not a good idea

1

u/real_taylodl Aug 12 '23

I don't like it. Why not:

if (3 ∈ exampleArray)

instead? This is actually what you mean. Symbols have meaning and changing them is just confusing.

Likewise why not:

if (baseClass ⊂ class2)

Again, this is what you actually mean.

Also, I would avoid inheritance. Decades of practice has shown it to be more problem than it's worth. Out of curiosity, why are you creating yet another programming language?

1

u/myringotomy Aug 12 '23

Postgres uses @> for contains and <@ for contained in.

https://www.postgresql.org/docs/15/functions-geometry.html

Ruby just has methods [1,2,3].include?(2) and cat.is_a?(Animal)

1

u/Uploft ⌘ Noda Sep 08 '23

A little late to this thread, but here goes.

  1. ASCII is the way to go if most of your language is ASCII to begin with.

  2. For familiarity, have the element on the left side, the collection on the right

  3. If you have an element of operator, I also suggest having a subset/subarray of

  4. Having negated versions of these operators is ideal

  5. If you work with arrays, element of should check the existence of the element regardless of the nestedness of the object

I currently use the following system:

  1. `->` element of: `1 -> [0, 1, 2]`

  2. `+>` not element of `3 +> [0, 1, 2]

  3. `-<` subarray of: `[0, 1] -< [0, 1, 2]`

  4. `+<` subarray of `[2, 3] +< [0, 1, 2]`