Thread overview
source/protocols.d(40,34): Error: uninitialized variable 'value' cannot be returned from CTFE
May 16, 2016
Stefan
May 16, 2016
ag0aep6g
May 16, 2016
Stefan
May 16, 2016
Hello, i try to port some go code to D
i get this error messages from my current code.

source/protocols.d(40,34): Error: uninitialized variable 'value' cannot be returned from CTFE
source/protocols.d(41,34): Error: uninitialized variable 'value' cannot be returned from CTFE
source/protocols.d(42,34): Error: uninitialized variable 'value' cannot be returned from CTFE
source/protocols.d(43,36): Error: uninitialized variable 'value' cannot be returned from CTFE
source/protocols.d(44,35): Error: uninitialized variable 'value' cannot be returned from CTFE
source/protocols.d(46,36): Error: uninitialized variable 'value' cannot be returned from CTFE
source/protocols.d(47,38): Error: uninitialized variable 'value' cannot be returned from CTFE
source/protocols.d(48,33): Error: uninitialized variable 'value' cannot be returned from CTFE
source/protocols.d(49,33): Error: uninitialized variable 'value' cannot be returned from CTFE
source/protocols.d(50,35): Error: uninitialized variable 'value' cannot be returned from CTFE
source/protocols.d(51,37): Error: uninitialized variable 'value' cannot be returned from CTFE
source/protocols.d(52,55): Error: uninitialized variable 'value' cannot be returned from CTFE

this ist the code in question
https://gist.github.com/erde74/5bd7d91070791142c929258fee8d887b

the go source https://github.com/jbenet/go-multiaddr/blob/master/protocols.go

i am a bit lost currently and don't know how to fix the error messages. A hint how to fix this would be create.

i am thinking about to wrap all the funtions into a class, does this make sense?

May 16, 2016
On 05/16/2016 11:24 AM, Stefan wrote:
> source/protocols.d(40,34): Error: uninitialized variable 'value' cannot
> be returned from CTFE

Ouch. That's not a good error message. There is no `value` on that line.

I've filed an issue: https://issues.dlang.org/show_bug.cgi?id=16030


> this ist the code in question
> https://gist.github.com/erde74/5bd7d91070791142c929258fee8d887b
>
> the go source
> https://github.com/jbenet/go-multiaddr/blob/master/protocols.go
>
> i am a bit lost currently and don't know how to fix the error messages.
> A hint how to fix this would be create.

The problem seems to be that nativeToLittleEndian cannot be evaluted at compile time. You can fill `protocols` at run time instead, using a static constructor:

----
Protocol[] Protocols;

static this()
{
    Protocols = [
        Protocol(P_IP4, 32, "ip4", CodeToVarint(P_IP4)),
        Protocol(P_TCP, 16, "tcp", CodeToVarint(P_TCP)),
        Protocol(P_UDP, 16, "udp", CodeToVarint(P_UDP)),
        Protocol(P_DCCP, 16, "dccp", CodeToVarint(P_DCCP)),
        Protocol(P_IP6, 128, "ip6", CodeToVarint(P_IP6)),
            // these require varint:
        Protocol(P_SCTP, 16, "sctp", CodeToVarint(P_SCTP)),
        Protocol(P_ONION, 80, "onion", CodeToVarint(P_ONION)),
        Protocol(P_UTP, 0, "utp", CodeToVarint(P_UTP)),
        Protocol(P_UDT, 0, "udt", CodeToVarint(P_UDT)),
        Protocol(P_HTTP, 0, "http", CodeToVarint(P_HTTP)),
        Protocol(P_HTTPS, 0, "https", CodeToVarint(P_HTTPS)),
        Protocol(P_IPFS, LengthPrefixedVarSize, "ipfs", CodeToVarint(P_IPFS)),
    ];
}
----

Also note the different syntax for struct values: `Protocol(...)`, not `{...}`.

By the way, by convention `Protocols` would be called `protocols` in D. A capitalized name indicates a type.

> i am thinking about to wrap all the funtions into a class, does this
> make sense?

Not as far as I can tell.

May 16, 2016
On Monday, 16 May 2016 at 11:13:52 UTC, ag0aep6g wrote:
> On 05/16/2016 11:24 AM, Stefan wrote:
>> source/protocols.d(40,34): Error: uninitialized variable 'value' cannot
>> be returned from CTFE
>
> Ouch. That's not a good error message. There is no `value` on that line.
>
> I've filed an issue: https://issues.dlang.org/show_bug.cgi?id=16030
> ----

thank you

> Protocol[] Protocols;
>
> static this()
> {
>     Protocols = [
>         Protocol(P_IP4, 32, "ip4", CodeToVarint(P_IP4)),
>         Protocol(P_TCP, 16, "tcp", CodeToVarint(P_TCP)),
>         Protocol(P_UDP, 16, "udp", CodeToVarint(P_UDP)),
>         Protocol(P_DCCP, 16, "dccp", CodeToVarint(P_DCCP)),
>         Protocol(P_IP6, 128, "ip6", CodeToVarint(P_IP6)),
>             // these require varint:
>         Protocol(P_SCTP, 16, "sctp", CodeToVarint(P_SCTP)),
>         Protocol(P_ONION, 80, "onion", CodeToVarint(P_ONION)),
>         Protocol(P_UTP, 0, "utp", CodeToVarint(P_UTP)),
>         Protocol(P_UDT, 0, "udt", CodeToVarint(P_UDT)),
>         Protocol(P_HTTP, 0, "http", CodeToVarint(P_HTTP)),
>         Protocol(P_HTTPS, 0, "https", CodeToVarint(P_HTTPS)),
>         Protocol(P_IPFS, LengthPrefixedVarSize, "ipfs", CodeToVarint(P_IPFS)),
>     ];
> }
> ----
>

forgot about "static this", works like a charm