3

I'm new to creating Smart Contracts on EOS and I've followed a few tutorials (for example the official Hello World guide).

Would someone be able explain why all of the examples seem to use the namespace eosio?

Is it possible to use a different namespace?


#include <eosiolib/eosio.hpp>
#include <eosiolib/print.hpp>
using namespace eosio;

class hello : public eosio::contract {
  public:
      using contract::contract;

      /// @abi action 
      void hi( account_name user ) {
         print( "Hello, ", name{user} );
      }
};

EOSIO_ABI( hello, (hi) )

If it is possible to use a different namespace, what are some example situations in which you would use one?

If it isn't possible to use a different namespace, is there any reason why this needs to be in the code and couldn't be left for the compiler to add?

  • 1
    the eosiolib is under eosio namespace to avoid collisions and maintain good practices. it's just standard language-agnostic programming practice to namespace imports – confused00 Sep 2 '18 at 13:23
2

eosiolib uses the eosio namespace to avoid collisions when imported in a different scope--this is standard programming practice and is not specific to EOSIO. The using namespace <name> directive only brings that namespace in the current scope in order to avoid overusing the scope-resolution operator ::. How you actually use it in the end is just a styling distinction. You can write the same contract in many ways:

No using

#include <eosiolib/eosio.hpp>

class hello : public eosio::contract {
  public:
      using eosio::contract::contract;

      /// @abi action 
      void hi( account_name user ) {
         eosio::print( "Hello, ", eosio::name{user} );
      }
};

EOSIO_ABI( hello, (hi) )

With using namespace

#include <eosiolib/eosio.hpp>
using namespace eosio;

class hello : public contract {
  public:
      using contract::contract;

      /// @abi action 
      void hi( account_name user ) {
         print( "Hello, ", name{user} );
      }
};

EOSIO_ABI( hello, (hi) )

With using eosio::<name>

#include <eosiolib/eosio.hpp>

using eosio::contract;
using eosio::print;
using eosio::name;

class hello : public contract {
  public:
      using contract::contract;

      /// @abi action 
      void hi( account_name user ) {
         print( "Hello, ", name{user} );
      }
};

EOSIO_ABI( hello, (hi) )

I personally find the last one most readable as it is clear where every name is coming from, but it requires more typing than the second example.

0

I don't think you have to use the eosio namespace, but given that your contract will inherit from eosio::contract, it would be standard practice to also use the eosio namespace.

  • but the contract is not namespaced under eosio in OP's example. the using directive only brings the eosio namespace into the local scope so one can write contract instead of eosio::contract even if the example doesn't actually take advantage of this when inheriting – confused00 Sep 2 '18 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.