+ 1

How to create my own operator(something like +) in any programming language?

24th Feb 2018, 1:15 AM
Š”Š°Š½ŠøŠ» ŠšŠ¾ŠæысŠ¾Š²
Š”Š°Š½ŠøŠ» ŠšŠ¾ŠæысŠ¾Š² - avatar
2 Answers
+ 3
Kotlin code: // Provide this[index] access to the list for read access. operator fun get(index: Int) = list[index] // Provide this[index] access to the list for write access. operator fun set(index: Int, value: Int) { list[index] = value } C++ code: // Provide this[index] access for read/write. int& operator[](const int index) { Ā Ā Ā Ā return list[index]; } Python code: // Provide this[index] access to the list for read access. def __getitem__(self, index): return self.list[index] // Provide this[index] access to the list for write access. def __setitem__(self, index, value): self.list[index] = value
24th Feb 2018, 1:45 AM
John Wells
John Wells - avatar
+ 2
Ideally you'd use functions. i.e, to cube a number: function cbrt(num){ return Math.pow(num, 3); } console.log(cbrt(5)); //prints 125 to the console As for actual operators such as + or -, I don't think this is possible.
24th Feb 2018, 1:28 AM
Overene