Asignment is method programming | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Asignment is method programming

So in ruby a.b = 5 Is the same as a.b=(5) I was wondering if there was a programming language where a = 5 Is equivelent to a.=(5) Obviously in that kind of a paradigm types would control what can be asigned to them i expect such a language would be strongly typed. Lets say a was a fraction type. The Fraction type could define .=() so that a = 5 Is equivelent to a = Fraction.new(5,1) But the language would still be the strongly typed. I feel that would be really cool

14th Sep 2020, 1:15 AM
Zachiah sawyer
Zachiah sawyer - avatar
3 Answers
+ 1
In C++ you can overload operators if at least one operand is of user defined type. I don't think I have fully understood your question but here is what I can have for demonstration purpose. https://code.sololearn.com/cxHps8pAIgX3/?ref=app User is a user defined data type and we are assigning a string to user object. = operator is overloaded such that when you assign a std::string to it changes name field of user object. I hope this is somewhat close to what you are asking :)
16th Sep 2020, 11:24 AM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
+ 1
Thanks so much i had no idea C++ had such cool capabilities. That was exactly what i was asking. Can amy other languages do that?
16th Sep 2020, 1:40 PM
Zachiah sawyer
Zachiah sawyer - avatar
0
I have seen people doing it in python. and on google I found that even ruby can do this! see the faqs : https://www.ruby-lang.org/en/documentation/faq/7/
16th Sep 2020, 2:05 PM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar