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

Allow integer expressions and sizeof() as part of type notation #5427

Open
Papierkorb opened this issue Dec 21, 2017 · 5 comments
Open

Allow integer expressions and sizeof() as part of type notation #5427

Papierkorb opened this issue Dec 21, 2017 · 5 comments

Comments

@Papierkorb
Copy link
Contributor

Hi,

Next to #5426 I also came across that you can't do calculations inside type notations. You also can't use sizeof. sizeof on arbitrary types may be too hard, but it could be allowed for primitive types.

Depending on where I get different errors, including a compiler bug message:

def func
  StaticArray(UInt8, 4 * 3) # Error: expecting token ')', not '*'
  StaticArray(UInt8, sizeof(Void*)) # This works inside a function
end

# As part of type notation:
struct A
  # BUG: unknown node in TypeLookup: sizeof(::Pointer(Void)) SizeOf
  @x : StaticArray(UInt8, sizeof(Void*))

  def initialize(@x); end
end

lib N
  struct A
    x : StaticArray(UInt8, sizeof(Void*)) # Both give the same BUG error
    y : UInt8[sizeof(Void*)]
  end
end

Another edge case was using a constant with a calculation with sizeof():

SIZE = sizeof(Void*) * 2  # Error: invalid constant value
struct A
  @x : UInt8[SIZE] # Error is emitted from here

  def initialize(@x); end
end

Thanks

@jwoertink
Copy link
Contributor

Having a nice error message on that StaticArray use instead of syntax error could also close #4238

@asterite
Copy link
Member

asterite commented Jan 4, 2018

We can't allow sizeof here because for that you need to compute the size of a type, and for that we need to know its instance variables. A recursive/paradox problem.

I wouldn't like to support general expressions there too. Right now the only use case for integer types in generic types is StaticArray. It's a strange case. And using numbers or computed constants is enough for now.

@RX14
Copy link
Contributor

RX14 commented Jan 4, 2018

@asterite the request is for a special case to allow sizeof for primitive types only, like Pointer, and Int/Float types in this situation.

@bew
Copy link
Contributor

bew commented Sep 23, 2018

I just stepped on this issue, with the following code: (which is basically the same issue, but with non-primitive types)

struct Bar(T)
  @var = StaticArray(UInt8, instance_sizeof(T)).new 0_u8
end

class MyClass
  @field = 42
end

puts Bar(MyClass).new

Basically I want a type Bar where sizeof(Bar(T)) == instance_sizeof(T)
And AFAIK there's no other way to do this.. 😞

It should be quite possible, as instance_sizeof(MyClass) doesn't depend on other complicated types..

@straight-shoota
Copy link
Member

The discussion about arithmetic expressions continues in #11759

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

No branches or pull requests

7 participants