Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Operator overloading [] [Feature Request] #23177

Open
2 tasks
LukyGuyLucky opened this issue Dec 16, 2024 · 1 comment
Open
2 tasks

Operator overloading [] [Feature Request] #23177

LukyGuyLucky opened this issue Dec 16, 2024 · 1 comment
Labels
Feature/Enhancement Request This issue is made to request a feature or an enhancement to an existing one.

Comments

@LukyGuyLucky
Copy link

LukyGuyLucky commented Dec 16, 2024

Describe the feature

V support simple operator overloading like +/-/*/\ ,but no index operator overloading [] which is quite useful and much convenient when a UDT type has elements.
Would this feature be considered to add in the language?

Use Case

It is preferrable to write iList[2]:=33 other than iList.at(2)=33

Proposed Solution

No response

Other Information

No response

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

Version used

0.4.8

Environment details (OS name and version, etc.)

Win 10,64bit

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

Huly®: V_0.6-21614

@LukyGuyLucky LukyGuyLucky changed the title Operator overloading [] Operator overloading [] [Feature Request] Dec 16, 2024
@felipensp felipensp added the Feature/Enhancement Request This issue is made to request a feature or an enhancement to an existing one. label Dec 16, 2024
@jorgeluismireles
Copy link

I didn't know operator overloading permit the result to be of different type of the arguments. Is a bug or with something in mind? Next code is valid... Guess what the programmer wanted to do:

type Byte = u8
type Short = u16
type String = string

pub fn (b1 Byte) + (b2 Byte) Short {
	return u16(b1) + u16(b2)
}
pub fn (b1 Byte) - (b2 Byte) bool {
	if b1 > b2 {
		return true
	}
	return false
}
pub fn (b1 Byte) % (b2 Byte) String {
	return '${b1}%${b2}'
}
pub fn (b1 Byte) / (b2 Byte) String {
	return '${f64(b1)/f64(b2)}'
}

fn main() {
	println('sum:  ${Byte(255) + Byte(1)}') // 256
	println('diff: ${Byte(255) - Byte(1)}') // 1
	println('mod:  ${Byte(255) % Byte(1)}') // 255%1
	println('div:  ${Byte(1) / Byte(3)}')   // 0.3333333333333333
}

A lot of power requires a lot of responsability...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature/Enhancement Request This issue is made to request a feature or an enhancement to an existing one.
Projects
None yet
Development

No branches or pull requests

3 participants