source/protocols.d(40, 34): Error: uninitialized variable 'value' cannot be returned from CTFE

ag0aep6g via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon May 16 04:13:52 PDT 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.



More information about the Digitalmars-d-learn mailing list