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

Array operators on enumerated arrays of numbers #4838

Open
hyperturtle opened this issue Feb 13, 2025 · 2 comments
Open

Array operators on enumerated arrays of numbers #4838

hyperturtle opened this issue Feb 13, 2025 · 2 comments

Comments

@hyperturtle
Copy link

hyperturtle commented Feb 13, 2025

Context

Please provide any relevant information about your setup. This is important in case the issue is not reproducible except for under certain conditions.

  • Operating System & Odin Version:
    both windows and osx

dev-2025-01-nightly:2aae4cf

  • Please paste odin report output:
        Odin:    dev-2025-01-nightly:2aae4cf
        OS:      Windows 11 Professional (version: 24H2), build 26100.2894
        CPU:     AMD Ryzen 9 3900X 12-Core Processor
        RAM:     32696 MiB
        Backend: LLVM 18.1.8

Expected Behavior

Expecting array operators to also work on enumerated arrays (i have a work around, but not sure if its ideal)

package main

import "core:fmt"

myEnum :: enum {
    A,B,C
}

main :: proc() {
    arrayA: [10]int
    // expected
    fmt.println(arrayA + 10)

    arrayB: [myEnum]int

    // fails to compile
    fmt.println(arrayB + 10)

    // workaround, but not sure if it has the same compiler advantages as above 
    fmt.println(arrayB + [myEnum]int{.A=10,.B=10,.C=10})
}

Current Behavior

Array operators work on normal array of numbers, but fails for enumerated array of numbers

Failure Information (for bugs)

I also found a related resolved issue #1042 so I'm assuming the semantics of array of numbers should carry forward to enumerated arrays of numbers.

Steps to Reproduce

  1. try to compile
  2. compilation error

Failure Logs

Error: Cannot convert untyped value '10' to '[myEnum]int' from 'untyped integer' 
	fmt.println(arrayB + 10) 
	                     ^^ 
@hyperturtle hyperturtle changed the title Array operators on enumerated arrays or numbers Array operators on enumerated arrays of numbers Feb 13, 2025
@gingerBill
Copy link
Member

This is mostly the case as enumerated arrays are not really meant to be treated this way in the first place.

@hyperturtle
Copy link
Author

Thanks for the reply! I'm really loving Odin so far, so I'll find a way for it to work for me. The reason I wanted to use enumerated arrays, is to be able to make sure I index into an array for only specific names. I know there's many ways I could've wrote this but here's what I had before I learned about enumerated arrays.

element :: enum {
    PHYSICAL,
    FIRE,
    COLD,
    LIGHTNING,
}

damnage_packet :: struct {
    amount: [len(element)]int,
    crit_chance: u8
}


apply_hit :: proc(u:^unit, h:damnage_packet) {
    amount := h.amount

    amount = amount*(100 - u.resists)/100
    if u8(rand.uint32() % 100) < h.crit_chance {
        amount = amount*150/100
    }
    total_damage := math.sum(amount[:])
    u.life -= total_damage
}

Being able to do array math on a vector has made the code easier to work with.

What I'm doing now

unit.resists[element.DARK] = 10

vs what would be possible with enumerated arrays (along with making sure I dont use other enums accidentally)

unit.resists[.COLD] = 10

If this example isn't convincing, feel free to close. I'll continue with the non-enumerated array version as I think it'll best suit what I need it to do

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants