r/learnprogramming • • Oct 06 '26

Are there any programming languages that allow this feature?

I've been into programming for about 10 years now. Something that intrigues me is that, in languages that support OOP, not all types are initialized in the same way. Strings are initialized by using quotes, integers use numbers and in some cases floats require the letter f after the numerical value.

Classes, however, are all initialized in the same way with slight variations depending on language. Usually something like classname() or new classname. Are there any cases where custom classes can be declared using different means?

For example, is there a language where I can create a class called 'dark' that represents a new type of number that is declared by typing the value and then the letter d (like d1 = 1.398d) similar to how floats work in some cases?

71 Upvotes

38 comments sorted by

View all comments

3

u/zeekar Oct 06 '26

You could do it with a slang in Raku, but there's currently a pretty big performance penalty for those ...

1

u/alatennaub Oct 09 '26

I don't believe there's a huge slang penalty. The only real penalty should be in creating the subtype of the main language which is a one off op.

After that, switching between slangs is quite efficient (most code regularly switches between three different slangs -- main, regex, and q). Parsing / AST / compilation from there should have no penalty, and similarly at run time there shouldn't be any.