Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9123767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:25:18+00:00 2026-06-17T06:25:18+00:00

I am using phantom types in the type-safe builder pattern to ensure methods are

  • 0

I am using phantom types in the type-safe builder pattern to ensure methods are called only once as in the following code sample

  sealed trait TBoolean
  sealed trait TTrue extends TBoolean
  sealed trait TFalse extends TBoolean

  class Builder[MethodCalled <: TBoolean] private() {

    def foo()(implicit ev: MethodCalled =:= TFalse): Builder[TTrue] = {
      new Builder[TTrue]
    }
  }

  object Builder {
    def apply() = new Builder[TFalse]()
  }

Builder().foo().foo() does not work as required, however I would like to set the error message to something user-readable. At the moment the message is

Multiple markers at this line
– not enough arguments for method foo: (implicit ev: =:=[W.TTrue,W.TFalse])W.Builder[W.TTrue]. Unspecified value parameter ev.
– Cannot prove that W.TTrue =:= W.TFalse.
– Cannot prove that W.TTrue =:= W.TFalse.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T06:25:19+00:00Added an answer on June 17, 2026 at 6:25 am

    Using the type parameters here is a bit of an overkill. Better just return a less capable type from the foo method:

    object Builder {
      trait CanFoo { def foo() : Builder }
      def apply(): Builder with CanFoo = new Builder with CanFoo {
        def foo() = new Builder {}
      }
    }
    trait Builder
    
    Builder().foo().foo() // value foo is not a member of Builder
    

    There is an annotation implicitNotFound which can be used to customise the error messages, but it needs to be defined with the type that is sought (=:=) not with the use site (foo), so that is a pretty useless construction…

    …unless you create your own replacement for =:=:

    import annotation.implicitNotFound
    
    object Called {
      implicit def same[A]: Called[A, A] = instance.asInstanceOf[Called[A, A]]
      private object instance extends Called[Any,Any]
    }
    @implicitNotFound(msg = "Cannot call this method twice") sealed trait Called[A, B]
    
    class Builder[Foo <: TBoolean] private() {
      def foo()(implicit ev: Called[Foo, TFalse]): Builder[TTrue] = {
        new Builder[TTrue]
      }
    }
    object Builder {
      def apply() = new Builder[TFalse]()
    }
    
    Builder().foo().foo()  // -> "error: Cannot call this method twice"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ocaml programmers can use so called 'phantom types' to enforce some constraints using the
Using this code, the following execution yields strange results: C 100 R W The
Using a populated Table Type as the source for a TSQL-Merge. I want to
Using System.Diagnostics.EventLog .NET type one can programmatically create logs into the Event Viewer application.
I would like to render individual HTML elements into PNGs using Phantom.JS. Does anyone
I have a problem with using Phantom do delete and copy recursively. I have
Using "repeatable read", it should be possible to produce a phantom read, but how?
I've started using Phantom JS on Windows, but I'm having a bit of difficulty
Using a CSS image sprite, I'm creating an 'interactive' image where hovering over certain
using this http://bl.ocks.org/950642 we can see how to add images to nodes, the question

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.