Hello guys

Hello guys,

Why should I use [Ref] attribute for const parameters? I read the doc http://docwiki.embarcadero.com/RADStudio/Seattle/en/Parameters_(Delphi)#Constant_Parameters however I was unable to understand the use cases of it.

Passing reference types as const make it to be passed by copy instead of by reference? I didn't understand why [Ref] is necessary to make sure the reference type will be passed by reference.

Can you help me? Thanks in advance :D


http://docwiki.embarcadero.com/RADStudio/Seattle/en/Parameters_(Delphi)

Comments

  1. Apparently const behaves differently on mobile compilers (not passed by reference) by default, so you can use the [ref] attribute to force it. I think the use case is to pass the const parameter by reference (avoid copying), as we're used to when using the win32 compiler.

    ReplyDelete
  2. Who said that ARC is so perfect that it should be everywhere? ;)

    ReplyDelete
  3. The more I learn about ARC, the less I like it.

    ReplyDelete
  4. In a nutshell, because short of changing parameters to explicit pointers, interop with some native APIs makes it necessary for a version of 'const' that always means the value will be passed by reference, not just when the compiler thinks this behaviour will be the most optimal one. It's nothing to do with ARC - before Delphi got [ref], FPC got constref for exactly the same reason.

    ReplyDelete
  5. The thing is: using const parameters doesn't automatically mean that variables get passed by reference. Const X:integer probably gets copied.

    The pass by ref is merely a common side effect of const arguments, but you cannot rely on it in all situations.

    Requiring [ref] is a good example of a leaky abstraction. C style programming is much more explicit and clear about this, at the cost of readability. It's easier to make mistakes with it. In Delphi, you just receive a "string". In c you need to think: ehh, is this a pointer to a character, or a pointer to a pointer to a character, which is worse than requiring a [ref] here and there to enforce a certain behavior.

    ReplyDelete
  6. Wouter van Nifterick I disagree, it's not a 'leaky abstraction' - it's sole purpose is interoperability, nothing else. By your argument the fact the 32 bit Windows compiler supports different calling conventions is a 'leaky abstraction' too.

    ReplyDelete
  7. If you're writing an asm body in your routine. Knowing whether you have a ref or a copy is a pretty big thing. Also on x64 and x86 the compiler may choose different strategies for a const parameter, which may be awkward in your code.
    If you are writing a generic routine, it's nice to know you're always getting a reference, it simplifies the routine a lot.

    Just 3 reasons why [ref] can be useful, I'm sure there are more.

    ReplyDelete
  8. Johan Bontes I have done a lot of asm/Delphi stuff under x86 and x64, also arm 32 and 64 bit with FPC, and I can't follow your argument very well... all is defined in diverse ABI protocols, depending on CPU and OS...

    ReplyDelete

Post a Comment